[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing Mysql populated form data

Posted on 2008-11-19
8
Medium Priority
?
251 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:Dada44
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:vibrazy
ID: 22993606
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
 

Author Comment

by:Dada44
ID: 22993634
I get a blank page :(
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1000 total points
ID: 22993838
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Assisted Solution

by:obareey
obareey earned 1000 total points
ID: 22993883
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
 

Author Comment

by:Dada44
ID: 22993894
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 22993948
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
 
LVL 5

Expert Comment

by:vibrazy
ID: 22993963
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
 

Author Closing Comment

by:Dada44
ID: 31518189
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question