Jump to content

For loop to retrieve form data


RLJ

Recommended Posts

Hi all,

 

I have the following code that generates a table of results from a MySQL query:

$i=1;
        while($arr = mysql_fetch_array($result, MYSQL_NUM))
        {
            $table .= "<tr>" 
                              ."<td width='5px';><input type='checkbox' name='transcheck".$i."'></td>"
                              ."<td id='parent".$i."A'>".$arr[0]." ".$arr[1]."</td>"
                              ."<td id='parent".$i."B'>".$arr[2]."</td></tr>"
                              ."<tr style='display:none'><input type='hidden'; name='transemail".$i."'; value='".$arr[9]."'></tr>";
        $i++;
        }
        $table .= "</table>";
echo $table;

 

As you can see it generates a table containing (amongst other things):

checkboxes:      transcheck1,2........9...etc.

hidden inputs:    transemail1,2........9...etc.

 

This table is inside a form, so that the above gets posted to another php file. What I now want to do in this 2nd php file, is to retrieve all the checkboxes and hidden inputs and then to display the values of the hidden inputs, where the corresponding checkbox has been checked. So e.g. if transcheck1, transcheck3 and transcheck12 have been checked, then I want to display transemail1, transemail3 and transemail12.

 

I can see that this should be relatively straightforward, but I'm fairly new to this stuff, could someone pls help me out?

 

Thanks!

Link to comment
Share on other sites

I would define the checkboxes and emails as arrays using $i as the key:

."<td width='5px';><input type='checkbox' name='transcheck[".$i."]'></td>"
."<td id='parent".$i."A'>".$arr[0]." ".$arr[1]."</td>"
."<td id='parent".$i."B'>".$arr[2]."</td></tr>"
."<tr style='display:none'><input type='hidden'; name='transemail[".$i."]'; value='".$arr[9]."'></tr>";

 

then you can use a simple foreach loop:

foreach ($_POST['transcheck'] as $key => $value) {
  /* We don't have to check the $value since checkboxes are
      only in POST if the user actually checked them */
  echo $_POST['transemail'][$key];
}

 

of course, you probably should not be putting email addresses on the form. Hidden fields are only hidden on the rendered page. If you view the source, you will see them. Bots love this stuff. It also leaves you open to XSS injection. You should really be using the unique ID from the database and then retrieve the email addresses from the database in the destination script.

Link to comment
Share on other sites

First, why are you making this hard on yourself (and us). Don't use the numeric index of the database results - use the field name. It makes it so much easier to understand the code. Plus, why would you create a new table row for a hidden input???

 

Also, you don't need the hidden fields. You just need to create the checkboxes correctly.

 

1. Create the checkbox names as an array

2. Set the value of the checkbox as the value you are currently assigning to the hidden input.

 

Only the checked checkboxes and their values will be posted to the processing page. So, you can simply iterrate through all of the POSTED values from the checkboxes using the array you created in the checkbox names

 

Form page:

$i = 0;
while($arr = mysql_fetch_array($result, MYSQL_NUM))
{
    $table .= "<tr>\n";
            . "  <td width='5px';><input type='checkbox' name='transcheck[]' value='{$arr[9]}'></td>\n"
            . "  <td id='parent{$i}A'>{$arr[0]} {$arr[1]}</td>\n"
            . "  <td id='parent{$i}B'>{$arr[2]}</td>\n"
            . "</tr>\n"
}
$table .= "</table>";

echo $table;

 

Processing page:

$checkedList = '';
foreach($_POST['transcheck'] as $value)
{
    $checkedList .= "<li>{$value}</li>\n";
}

echo "The following values were checked:<br>\n";
echo "<ul>{$checkedList}</ul>\n";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.