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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Beverley PortlockCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

obareeyCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.