How to insert checkbox data into mysql?

Thread Solved

Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

How to insert checkbox data into mysql?

 
0
  #1
May 25th, 2009
Hi, I need help for this problem that i'm trying to solve for a while (i'm new in PHP). How do i submit the value into database?
Here's the code:

<form name="Form1" method="POST" action="checkbox.php">
<p><input type="checkbox" name="floor tile"> Floor Tiles</p>
<p><input type="checkbox" name="polished tile"> Polished Tiles</p>
<p><input type="checkbox" name="homogeneous tile"> Homogeneous Tiles</p>
<p><input type="checkbox" name="wall tile"> Wall Tiles</p>
<p><input type="submit" name="submit" value="Submit"></p>
</form>


<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);

/* show which options were selected */
foreach( $_POST as $key=>$val )
{
	if( $key != "Submit" ) {
	echo $key.' - '.$val.'<br />';
	}
}

/* check how many options were selected */
if( count( $_POST ) > 1 ) 
{
	echo '<p>You have selected too many options.</p>';
}
else 
{
	/* put your insert query here */
	$i<count($_POST['checkbox'] as $value)       <--line27
	$_POST['checkbox'][$i]
	$sql = mysql_query("INSERT (tile ) VALUES ('$value') INTO product_type");
}

if (!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
}

mysql_close($con)  

?>
The problem is its showing a parse error which is:
(Parse error: parse error in C:\wamp\www\test\checkbox.php on line 27)
I also need to be able to restrict the user to select not more than one choice. Any help would be greatly appreciated! Thanks!
Last edited by peter_budo; May 25th, 2009 at 3:37 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #2
May 25th, 2009
Me too a beginer in php,
i checked ur code but cant understand some parts of it comletely.
as i do php in php4 style and u in php5 style

First i dont understand why u are using checkboxes instead of radion button if u need only one result at a time.

Second why calling $_POST['checkbox'] instead of their name/id
i mean like
  1. $_POST['wall tile'], $_POST['floor tile']
etc.

did u tried 'echo' ing the $_POST['checkbox'] ?
does it give u the required output?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

Re: How to insert checkbox data into mysql?

 
0
  #3
May 25th, 2009
Thank you for your reply danishbacker!
Please go easy on me for asking what may be an obvious question for many.
The $_POST['checkbox'] doesn't give any output as its showing the same error message. Meanwhile, i have changed the checkboxes to radiobuttons. Thanks for the suggestion.

Between, can you show me some guidelines on inserting radiobutton value into database?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #4
May 25th, 2009
while using radio button
u need set values for each radio button to know which one is clicked
dont forget to give same name and id for all radio buttons

  1. <form id="f" name="f" method="post" action="test.php">
  2. <input type="radio" name="rad" id="rad" value="1" />
  3. <input type="radio" name="rad" id="rad" value="2" />
  4. <input type="radio" name="rad" id="rad" value="3" />
  5. <input name="" type="submit" value="Submit" />
  6. </form>
  7.  
  8. <?php //test.php
  9. if(isset($_POST['rad']))
  10. echo $_POST['rad']; //u can get selected item from here
  11. else
  12. echo "not set";
  13. ?>

It is easier to access dom elements either by name or id
Last edited by danishbacker; May 25th, 2009 at 7:00 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,618
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 469
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: How to insert checkbox data into mysql?

 
0
  #5
May 25th, 2009
It is called multivalued Request parameter. For multi-valued parameter you have to use array notation with name attribute of input form tag.

  1. <input type="checkbox" name="ar[]" value="Value1"/>
  2. <input type="checkbox" name="ar[]" value="Value2"/>
  3. <input type="checkbox" name="ar[]" value="Value3"/>

Hope this code will help you.

  1. <?php
  2. $cmd=$_POST["cmd"];
  3. $hb=$_POST["hb"];
  4. $sel[]=array("","","");
  5. if(isset($cmd)){
  6. for($i=0;$i<count($hb);$i++) {
  7. print "<br>Your selection is $hb[$i]";
  8. $sel[$hb[$i]-1]="checked";
  9. }
  10. }
  11. ?>
  12. <html>
  13. <head>
  14. <title>Check Boxes</title>
  15. </head>
  16. <body>
  17. <form name="form1" method="POST" action="sample1.php">
  18. <br>Select Hobbies
  19. <ul>
  20. <li>Playing Outdoor games<input type="checkbox" value="1" <?=$sel[0]?> name="hb[]"></li>
  21. <li>Watching Outdoor games<input type="checkbox" value="2" <?=$sel[1]?> name="hb[]"></li>
  22. <li>Reading Books<input type="checkbox" value="3" <?=$sel[2]?> name="hb[]"></li>
  23. </ul>
  24. <input type="submit" value="Add" name="cmd">
  25. </form>
  26. </body>
  27. </html>
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

Re: How to insert checkbox data into mysql?

 
0
  #6
May 25th, 2009
Originally Posted by danishbacker View Post
while using radio button
u need set values for each radio button to know which one is clicked
dont forget to give same name and id for all radio buttons

  1. <form id="f" name="f" method="post" action="test.php">
  2. <input type="radio" name="rad" id="rad" value="1" />
  3. <input type="radio" name="rad" id="rad" value="2" />
  4. <input type="radio" name="rad" id="rad" value="3" />
  5. <input name="" type="submit" value="Submit" />
  6. </form>
  7.  
  8. <?php //test.php
  9. if(isset($_POST['rad']))
  10. echo $_POST['rad']; //u can get selected item from here
  11. else
  12. echo "not set";
  13. ?>

It is easier to access dom elements either by name or id

Hi danish, i tried running your code, but both the echo statements didn't display anything. How do i insert the selected value into database?

p/s: what's dom?
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

Re: How to insert checkbox data into mysql?

 
0
  #7
May 25th, 2009
Originally Posted by adatapost View Post
It is called multivalued Request parameter. For multi-valued parameter you have to use array notation with name attribute of input form tag.

  1. <input type="checkbox" name="ar[]" value="Value1"/>
  2. <input type="checkbox" name="ar[]" value="Value2"/>
  3. <input type="checkbox" name="ar[]" value="Value3"/>

Hope this code will help you.

  1. <?php
  2. $cmd=$_POST["cmd"];
  3. $hb=$_POST["hb"];
  4. $sel[]=array("","","");
  5. if(isset($cmd)){
  6. for($i=0;$i<count($hb);$i++) {
  7. print "<br>Your selection is $hb[$i]";
  8. $sel[$hb[$i]-1]="checked";
  9. }
  10. }
  11. ?>
  12. <html>
  13. <head>
  14. <title>Check Boxes</title>
  15. </head>
  16. <body>
  17. <form name="form1" method="POST" action="sample1.php">
  18. <br>Select Hobbies
  19. <ul>
  20. <li>Playing Outdoor games<input type="checkbox" value="1" <?=$sel[0]?> name="hb[]"></li>
  21. <li>Watching Outdoor games<input type="checkbox" value="2" <?=$sel[1]?> name="hb[]"></li>
  22. <li>Reading Books<input type="checkbox" value="3" <?=$sel[2]?> name="hb[]"></li>
  23. </ul>
  24. <input type="submit" value="Add" name="cmd">
  25. </form>
  26. </body>
  27. </html>

Hi, thank you for the reply!

I executed the code, but it turned out with a notice telling (Notice: Undefined index: hb in C:\wamp\www\test\sample1.php on line3)

The form page seem to display this way:-

Select Hobbies
  • Playing Outdoor games [checkbox] name="hb[]">
  • Watching Outdoor games [checkbox] name="hb[]">
  • Reading Books [checkbox] name="hb[]">

[Add button]

But, when i removed this part: <?=$sel[2]?> from the code, the name="hb[]"> disappear as well from the form page and prints "Your selection is 3" and the same goes for the other radio buttons.

Hope i have explained clear enough for futher action. It may be a small matter, but i dont seem to have idea in solving it. Hear you soon. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #8
May 26th, 2009
Sorry, this is working perfect here

Check this i just uploaded a sample the file i was using here. http://peipians.com/tempDomains/test.php

try removing isset() and all those,
just echo $_POST['rad'];
see what u r getting

Also the one like Mr adatapost said is also working perfect
  1. <form id="f" name="f" method="post" action="test.php">
  2. <input type="radio" name="rad[]" id="rad[]" value="1" />
  3. <input type="radio" name="rad[]" id="rad[]" value="2" />
  4. <input type="radio" name="rad[]" id="rad[]" value="3" />
  5. <input name="" type="submit" value="Submit" />
  6. </form>
  7.  
  8. <?php
  9. $ch = $_POST['rad'];
  10. echo $ch[0];
  11. ?>
or
  1. echo $_POST['rad'][0];

the DOM stands for Document Object Model the following link will give u a small idea. Also dont forget to check w3schools
http://www.geekinterview.com/talk/6889-dom-meaning.html
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 77
Reputation: Tulsa is an unknown quantity at this point 
Solved Threads: 15
Tulsa's Avatar
Tulsa Tulsa is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #9
May 26th, 2009
hi
if u want multiple values selection than u can use multiple selected list box or check box with the name as arrname[] like this and
u can get this select value after posting the form in array

like $array=$_post['arrname'];
foreach($array as $val)
{
echo $val;
}

ok now u can get the value by selecting the user
and i view your first threat code
where u need to use $_post['floor tile'];
so u get the ur value but u have not set the value in check box element
<input type='checkbox' value="test" name= 'floor tile' id= 'floor tile' >

thanks
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

Re: How to insert checkbox data into mysql?

 
0
  #10
May 27th, 2009
Hi,
Its alright. I tried 'echo'ing $ch = $_POST['rad'];
echo $ch[0];
and it displayed the radio button value.
But i couldn't use the same variable to insert the value into database. Any help please. Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC