Passing Mysql populated form data

Dear all,

The following code generates a list of checkboxes with a send button.

From send.php (or PHP SELF)  I want to get the value of the checked and sent checkboxes ..How can I do that?

Thanks in advance!  

<?php
      include 'connection.php'; 
 
$querya = "SELECT * FROM areas";
$resulta = mysql_query($querya) or die('Error, query failed');
 
              while( $row = mysql_fetch_assoc($resulta) )
              {
 
 
                  $aID = $row['aID'];
                  $name = $row['name'];
 
 
?>
       <html>          
                 <form name='areas' method='post' action='send.php'>
              
                    <input type='checkbox' name="<?php echo $name;?>" value="<?php echo $aID;?>"> <?php echo $name;?>
                  <input type="submit" value="Send" name="submit"><br /></form>
                  
                  </html>
                  
            <?php
 
              }
 
?>

Open in new window

Dada44Asked:
Who is Participating?
 
Beverley PortlockConnect With a Mentor Commented:
Your checkboxes need to be array members so change line 19 to look like this

<input type='checkbox' name="<?php echo $name."[$aID]";?>" value="<?php echo $aID;?>"> <?php echo $name;?>
 

so that you get something that looks like this

<input type='checkbox' name='check[1]' value='1' />
<input type='checkbox' name='check[3]' value='3' />
<input type='checkbox' name='check[19]' value='19' />

Then when the form is submitted you can process them with a foreach

$cb = $_POST['check'];  // or $_GET if that's what you use
foreach( $cb as $anId ) {
    // process the record with ID '$anId'
}

Only checked boxes will be in the array to process

0
 
vibrazyCommented:
Try this

<?php
      include 'connection.php'; 
 
$querya = "SELECT * FROM areas";
$resulta = mysql_query($querya) or die('Error, query failed');
 
              while( $row = mysql_fetch_assoc($resulta) )
              {
 
 
                  $aID = $row['aID'];
                  $name = $row['name'];
 
 					
					if(isset($_POST[$name ]))
					{
						//check box $name posted
						echo $_POST[$name ]."<br/>";
					}
              }
 
?>

Open in new window

0
 
Dada44Author Commented:
I get a blank page :(
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
obareeyConnect With a Mentor Commented:
your code creates many many forms. try like that and then try vibrazy's code.
<?php
      include 'connection.php'; 
 
$querya = "SELECT * FROM areas";
$resulta = mysql_query($querya) or die('Error, query failed');
?>
       <html>          
                 <form name='areas' method='post' action='send.php'>
<?php
 
              while( $row = mysql_fetch_assoc($resulta) )
              {
 
 
                  $aID = $row['aID'];
                  $name = $row['name'];
 
 
?>
                    <input type='checkbox' name="<?php echo $name;?>" value="<?php echo $aID;?>"> <?php echo $name;?>
                
            <?php
 
              }
 
?>
<input type="submit" value="Send" name="submit"><br /></form>
                  
                  </html>

Open in new window

0
 
Dada44Author Commented:
Sounds great but in <input type='checkbox' name='check[1]' value='1' />
 -check- changes with each row, remember it is populated with:
<input type='checkbox' name="<?php echo $name;?>"
.
.
How can I do then $cb = $_POST['check']; ??
0
 
Beverley PortlockCommented:
The line

<input type='checkbox' name="<?php echo $name;?>"

is what is causing the problem. You have this in a loop and retrieving rows with a unique ID $aID so you will be generating a series of checkboxes. Their name needs to an array which is why I suggested you change line 19 to look like this

<input type='checkbox' name="<?php echo $name."[$aID]";?>" value="<?php echo $aID;?>"> <?php echo $name;?>

Notice that the name is now an array entry.


"How can I do then $cb = $_POST['check']; ??"

In your code that processes the form, add the above code. It will create an array $cb whose keys and values will be the aID of the records you want to process.
0
 
vibrazyCommented:
My previous code had a typo.

Try the below

//submit.php
<?php
      include 'connection.php'; 
 
$querya = "SELECT * FROM areas";
$resulta = mysql_query($querya) or die('Error, query failed');
 
              while( $row = mysql_fetch_assoc($resulta) )
              {
 
 
                  $aID = $row['aID'];
                  $name = $row['name'];
 
                                        
                                        if(isset($_POST[$name]))
                                        {
                                                //check box $name posted
                                                echo $_POST[$name ]."<br/>";
                                        }
              }
 
?>

Open in new window

0
 
Dada44Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.