开发者

php-get data from form through get method

test.php?t=xxx,y=sss

This is how the user inputs data to my script.

How can I get this d开发者_Python百科ata as an array inside test.php, and remove the commas?


$temp = explode(',', $_GET['t']);
$t = $temp[0];
$y = substr($temp[1],2);

will get you what you want from the url you gave, however @Tudor Constantin has the best solution.

Edit

This will loop through all however many fields you have. They will finish up in $result in the correct order.

$temp = explode(',', $_GET['t']);
foreach($temp as $var){
    if(strpos($var, '=')){
        $exp = explode('=', $var);
        $result[] = $exp[1];
    } else $result[] = $var;
}

then var_dump $result gives this result

array
    0 => string 'xxx' (length=3)
    1 => string 'sss' (length=3)
    2 => string 'ttt' (length=3)
    3 => string 'uuu' (length=3)

Edit 2
Or you could try this:-

Say URL is "test.php?t=xxx,y=sss,z=ttt,a=uuu"

$temp = explode(',', $_GET['t']);
$key = array_keys($_GET);
$temp = array_reverse($temp);
$result[$key[0]] = array_pop($temp);
array_reverse($temp);
foreach($temp as $var){
    $exp = explode('=', $var);
    $result[$exp[0]] = $exp[1];
}

Then var_dump($result); will give you:-

array
 't' => string 'xxx' (length=3)
 'a' => string 'uuu' (length=3)
 'z' => string 'ttt' (length=3)
 'y' => string 'sss' (length=3)


By accessing $_GET['t'], you will get the value 'xxx,y=sss' - I think you have to change the URL to something like

test.php?t=xxx&y=sss

So you will be able to access $_GET['t'] and get the value 'xxx' and $_GET['y'] having value 'sss'

You could also send all the parameter values to one variable, separated by '|' for example - then split by that char:

URL would be test.php?array=xxx|sss|ddd|rrr and in test.php you will do:

$arr = explode('|', $_GET['array']);

This way in the $arr variable you will have an array of the values sent (no matter how many are there)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜