Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 5 of 5

Thread: Cycling through POST values in PHP

  1. #1

    Cycling through POST values in PHP

    I have a form with several checkboxes which are created and named dynamically. When i submit that form, how do i find out what elements exaclty were submitted and what are their values ?

    For example i could have the following checkboxes:

    checkbox6
    checkbox16
    checkbox1
    checkbox5

    or, some other time they might be something like this:

    checkbox3
    checkbox6
    checkbox7
    checkbox14
    checkbox8
    checkbox5
    checkbox90
    checkbox71

  2. #2
    602
    posts
    Backend Specialist
    PHP Code:
    $numbers = array();
    foreach(
    $_POST as $k => $v) {
      if(
    substr($k0strlen('checkbox')) == 'checkbox')) {
        
    $numbers[] = substr($kstrlen('checkbox'), (strlen($k) - strlen('checkbox'));
      }

    If you can't follow the code, let me know. hth.

  3. #3
    i tried something like this:

    PHP Code:
    foreach($_POST as $value) {
    echo 
    $value."<br>";

    but all i get is a bunch of values "on" ... but i need ALL the checkbox names, even those that were not selected.

  4. #4
    Checkboxes which are not selected are not sent back to the server. It's just part of the HTML spec. You'll never know which checkboxes weren't checked unless you know what all the fields were to begin with. So, you need to be able to get the same data used to render the checkboxes in the first place so you can compare that with what you got back. You could also make a hidden input field for each checkbox with a similar name (maybe ending with an underscore or something like that), which you could then compare against.

    PHP (any server side language for that matter) can tell you the name/id of the HTML input and it's value but it can't tell you the kind of HTML input (i.e. hidden vs. text vs. select, etc.).

    To loop through the values sent back to the server, use $_POST for form tags with method="post", $_GET for variables passed via the URL, and $_REQUEST for both post and get (and cookie) all combined together.

    PHP Code:
    foreach ($_REQUEST as $name => $value) {
      echo 
    "HTML input '" $name "' has value '" $value "'";

    Make sure your checkbox input tags have the proper value you want to get back.
    Code:
    <input type="checkbox" name="checkbox1" value="value you want checkbox 1 to represent" />
    <input type="checkbox" name="checkbox2" value="value you want checkbox 2 to represent" />

  5. #5
    wow thanks a lot guys... I accomplished what i was trying to do.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012