...

View Full Version : How do I Pass A Bunch of Same Data to PHP using $_POST?



dealmaker
12-30-2005, 10:32 PM
Hi,
I am sending a bunch of IDs to php using $_POST, what's the best way to do that? There is only one $_POST['id'].

Many thanks.

Element
12-30-2005, 10:39 PM
Hi,
I am sending a bunch of IDs to php using $_POST, what's the best way to do that? There is only one $_POST['id'].

Many thanks.


<input type="text" name="id" value="1|2|3|4|5">



if(isset($_POST['id']) && strpos("|", $_POST['id'])) {
$IDarray = explode("|", $_POST['id']);
}
foreach($IDarray as $key => $val) {
echo "<b>".$key."</b>: ".$val."<br>\n";
}

Velox Letum
12-30-2005, 11:40 PM
Or you could make an array out of it.


<input type="text" name="id[]" value="1">
<input type="text" name="id[]" value="2">
<input type="text" name="id[]" value="3">


foreach($_POST['id'] as $key => $val) {
echo "<b>".$key."</b>: ".$val."<br>\n";
}

dumpfi
12-31-2005, 12:03 AM
If the number of ids is fixed, you can simply do:

<input type="text" name="id1" value="1">
<input type="text" name="id2" value="2">
<input type="text" name="id3" value="3">
...
and use them as $_POST['id1'], $_POST['id2'], $_POST['id3'], ...

dumpfi

Element
12-31-2005, 12:19 AM
Now that I look at this, what are you using it for? The best way to do it really is based on what you are doing. If you are making a PM sort of meethod, using ID's too who the information is being sent to or w/e it be best to use my method, though change the values in this sort of way:


<input type="text" name="id_array" value="1, 2, 3, 4, 5">

and then explode by ", "

If you want to select a id, one by one, and then do other option for each id it be best to use multiple ID fields.

I don't suggest using dumpfi's method thouh. Its not that great because you could run foreach() on it because it would also run through any other data in the form. It best to creat a multi-dimension array with name="id[]"

dealmaker
12-31-2005, 01:07 AM
what will the URL string looks like if I use that ID array? For javascript version, I also need to assemble the url string for the POST action.


Or you could make an array out of it.


<input type="text" name="id[]" value="1">
<input type="text" name="id[]" value="2">
<input type="text" name="id[]" value="3">


foreach($_POST['id'] as $key => $val) {
echo "<b>".$key."</b>: ".$val."<br>\n";
}

fci
12-31-2005, 01:20 AM
what will the URL string looks like if I use that ID array?

the result would be equivalent to:


// this is $_POST['id']
array(
[0] => "1",
[1] => "2",
[2] => "3",
);


For javascript version, I also need to assemble the url string for the POST action.
not entirely sure what this means, maybe you mean for a GET string ?
if so just put this in your query string which will produce similar results as above:

somefile.php?action=blah&id[]=1&id[]=2&id[]=3

and..

$action = '';
foreach($your_arr as $v)
$action .= '&id[]='.$v;

Also.. maybe you could give us a little more context to whatever your problem is, i.e., code?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum