Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How do I Pass A Bunch of Same Data to PHP using $_POST?

    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.

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by dealmaker
    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.
    Code:
    <input type="text" name="id" value="1|2|3|4|5">
    PHP Code:
    if(isset($_POST['id']) && strpos("|"$_POST['id'])) {
      
    $IDarray explode("|"$_POST['id']);
    }
    foreach(
    $IDarray as $key => $val) {
      echo 
    "<b>".$key."</b>: ".$val."<br>\n";


  • #3
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or you could make an array out of it.

    Code:
    <input type="text" name="id[]" value="1">
    <input type="text" name="id[]" value="2">
    <input type="text" name="id[]" value="3">
    PHP Code:
    foreach($_POST['id'] as $key => $val) {
      echo 
    "<b>".$key."</b>: ".$val."<br>\n";

    "$question = ( to() ) ? be() : ~be();"

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    If the number of ids is fixed, you can simply do:
    Code:
    <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

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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:

    Code:
    <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[]"

  • #6
    Regular Coder
    Join Date
    Jul 2005
    Posts
    342
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    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.

    Quote Originally Posted by Velox Letum
    Or you could make an array out of it.

    Code:
    <input type="text" name="id[]" value="1">
    <input type="text" name="id[]" value="2">
    <input type="text" name="id[]" value="3">
    PHP Code:
    foreach($_POST['id'] as $key => $val) {
      echo 
    "<b>".$key."</b>: ".$val."<br>\n";

    Last edited by dealmaker; 12-31-2005 at 02:17 AM.

  • #7
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dealmaker
    what will the URL string looks like if I use that ID array?
    the result would be equivalent to:
    Code:
    // 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:
    Code:
    somefile.php?action=blah&id[]=1&id[]=2&id[]=3
    and..
    Code:
    $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?
    Last edited by fci; 12-31-2005 at 02:24 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •