OM The Eternity
OM The Eternity

Reputation: 16204

$_GET variable cannot identify the first array element

$_GET variable cannot identify the first array element.:

I have passed an array in url which looks like this

http://www.example.com/form.php?action=buy_now&ft=prf&Arrayproducts_id[]=431&products_id[]=432&Arraycart[]=1&cart[]=3&Arrayid[]=431&id[]=432

but when i print the array "products_id" and "cart" by using print_r($_GET), it just display me

Array
(
    [0] => 432
)
Array
(
    [0] => 3
)

now as u can see the url it also contains the value "431" for products_id and "3" for cart I can see that due to the string "Array" appended to these they are not being accessed, So could someone suggest me how to fix this issue

EDIT as per Felix review

for($t=0;$t<4;$t++){

    $proid_30 .= "products_id[".$t."]=".$products_id."&";
        $bucket_30 .= "cart[".$t."]=".$_SESSION['qty_flex'][$t]."&";
        $idproid_30 .= "id[".$t."]=".$products_id."&";

        }
        $idproid_30.=" ";
$idproid_30 = str_replace("& ","",$idproid_30);
        echo "<script>window.location= '/print_ready_form.php?action=buy_now&ft=prf&".$proid_30.$bucket_30.$idproid_30."&osCsid=".$_GET['osCsid']."';</script>";

Upvotes: 0

Views: 451

Answers (4)

Felix Kling
Felix Kling

Reputation: 816462

Looks like you echo an array to create your URL. This does not work:

$a = array(1,2);
echo $a;

prints

Array

Can you show the PHP code that generates the URL?

Update:

Without more code I can only assume, but I think that $proid_30, $bucket_30 and $idproid_30 are initialized as arrays. Now when you append a string, they are casted to strings:

$a = array(1,2);
$a .= 'test';
echo $a;

prints

Arraytest

Use new variables to build the URL or initialize them as strings:

E.g.:

$product = '';
$cart = '';
$id = '';
for($t=0;$t<4;$t++){
    $product .= "products_id[".$t."]=".$products_id."&";
    $cart .= "cart[".$t."]=".$_SESSION['qty_flex'][$t]."&";
    $id .= "id[".$t."]=".$products_id."&";
}

Upvotes: 2

sjamesas
sjamesas

Reputation: 17

First product Id was named as "Arrayproducts_id". So you can get by printing "Arrayproducts_id"

Upvotes: 0

Russell Dias
Russell Dias

Reputation: 73292

Its only displaying the 432 for products_id because the only other products_id in the link is called Arrayproducts_id.

You will need to rename this back to products_id =)

Unless you want to call Arrayproducts_id ofcourse.

Upvotes: 0

Sergey Eremin
Sergey Eremin

Reputation: 11080

you can use variable names like "var[]" in a http form. do you need to use only get method? try post

Upvotes: 0

Related Questions