The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


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