I have a problem to solve which im not sure if it can be solved in the way that i have in mind. It's about a form with checkboxes. The purpose of this form is to allow users to add or remove their favorite brands from the database by checking/unchecking the form checkboxes. The challege that i face now is that there may already be brands selected in the past from the user. And i would like the page when it loads to check in the database and automatically check the brands in the form which where found in the database. I checked to see if it was possible with jquery , but i got stuck. Here is my code:
/* == SQL CODE == */
CREATE TABLE IF NOT EXISTS `favoriteBrands` (
`pkFavoriteBrand` int(5) NOT NULL AUTO_INCREMENT,
`fkCompanyID` int(5) NOT NULL,
`Brands` varchar(30) NOT NULL,
PRIMARY KEY (`pkFavorietemerken`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=81 ;
INSERT INTO `favoriteBrands` (`pkFavoriteBrand`, `fkCompanyID`, `Brands`) VALUES
(80, 7, 'Replay'),
(79, 7, 'Pepe Jeans'),
(71, 12, 'Nike'),
(70, 12, 'Le Coq Sportif'),
/* == SQL CODE == */
/* == PHP CODE == */
<?php
$find_fav_brands = " SELECT Brands FROM favoriteBrands WHERE fkCompanyID=$pany_id";
$get_brand = mysql_query($find_fav_brands) or die ("No brand found");
while($row = mysql_fetch_assoc($get_brand )){
$show_brand = $row["Brands"];
echo"<p>$show_brand</p>";
}?>
<form name="form" method="post" action="ready.php" >
<ul>
<li><input type="checkbox" name="merk[]" value="Adidas"/>Adidas</li>
<li><input type="checkbox" name="merk[]" value="Airforce"/>Airforce</li>
<li><input type="checkbox" name="merk[]" value="Armani"/>Armani</li>
<li><input type="checkbox" name="merk[]" value="Asics"/>Asics</li>
</ul>
</form>
/* == PHP CODE == */
I have a problem to solve which im not sure if it can be solved in the way that i have in mind. It's about a form with checkboxes. The purpose of this form is to allow users to add or remove their favorite brands from the database by checking/unchecking the form checkboxes. The challege that i face now is that there may already be brands selected in the past from the user. And i would like the page when it loads to check in the database and automatically check the brands in the form which where found in the database. I checked to see if it was possible with jquery , but i got stuck. Here is my code:
/* == SQL CODE == */
CREATE TABLE IF NOT EXISTS `favoriteBrands` (
`pkFavoriteBrand` int(5) NOT NULL AUTO_INCREMENT,
`fkCompanyID` int(5) NOT NULL,
`Brands` varchar(30) NOT NULL,
PRIMARY KEY (`pkFavorietemerken`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=81 ;
INSERT INTO `favoriteBrands` (`pkFavoriteBrand`, `fkCompanyID`, `Brands`) VALUES
(80, 7, 'Replay'),
(79, 7, 'Pepe Jeans'),
(71, 12, 'Nike'),
(70, 12, 'Le Coq Sportif'),
/* == SQL CODE == */
/* == PHP CODE == */
<?php
$find_fav_brands = " SELECT Brands FROM favoriteBrands WHERE fkCompanyID=$pany_id";
$get_brand = mysql_query($find_fav_brands) or die ("No brand found");
while($row = mysql_fetch_assoc($get_brand )){
$show_brand = $row["Brands"];
echo"<p>$show_brand</p>";
}?>
<form name="form" method="post" action="ready.php" >
<ul>
<li><input type="checkbox" name="merk[]" value="Adidas"/>Adidas</li>
<li><input type="checkbox" name="merk[]" value="Airforce"/>Airforce</li>
<li><input type="checkbox" name="merk[]" value="Armani"/>Armani</li>
<li><input type="checkbox" name="merk[]" value="Asics"/>Asics</li>
</ul>
</form>
/* == PHP CODE == */
Share
Improve this question
edited Aug 25, 2012 at 14:47
the_boy_za
asked Mar 4, 2012 at 3:38
the_boy_zathe_boy_za
2871 gold badge8 silver badges21 bronze badges
2
-
No need for jQuery if you're dynamically generating the checkboxes, just add the
checked="checked"
attribute the the checkboxes that represent the user's favorite brands. Source: w3schools./tags/att_input_checked.asp – Telmo Marques Commented Mar 4, 2012 at 3:44 - @TomS Yeah i know but that's the problem i'm not generating the checkboxes dynamically. The form is static. – the_boy_za Commented Mar 4, 2012 at 5:27
2 Answers
Reset to default 4<?php
$find_fav_brands = " SELECT Brands FROM favoriteBrands WHERE fkCompanyID=$pany_id";
$get_brand = mysql_query($find_fav_brands) or die ("No brand found");
$brands = array();
while($row = mysql_fetch_assoc($get_brand )){
$brands[]=$row["Brands"];
$show_brand = $row["Brands"];
echo"<p>$show_brand</p>";
}
?>
<form name="form" method="post" action="ready.php">
<ul>
<li><input type="checkbox" name="merk[]" value="Adidas"<?php echo in_array("Adidas",$brands)?" checked="checked"":""; ?> />Adidas</li>
<li><input type="checkbox" name="merk[]" value="Airforce"<?php echo in_array("Airforce",$brands)?" checked="checked"":""; ?> />Airforce</li>
<li><input type="checkbox" name="merk[]" value="Armani"<?php echo in_array("Armani",$brands)?" checked="checked"":""; ?> />Armani</li>
<li><input type="checkbox" name="merk[]" value="Asics"<?php echo in_array("Asics",$brands)?" checked="checked"":""; ?> />Asics</li>
</ul>
</form>
1, I strongly do not suggest you to use 'string' as checkbox value. Try using IDs. It's much safer and easier to maintain your code.
2, Since you are using <li>
as your input container, run a loop to do so with PHP makes it much easier, like while or for/foreach.
3, Your database only restored 'checked' values, but not unchecked. It is impossible to generate a plete list from it with all checkbox options. So, here are 2 solutions:
3.1 Either save all options in your table structure, for example: brand1=0, brand2=1, brand3=1
etc. So you can foreach all your results and print 'checked=checked'
when brandX != 1
;
3.2 Or, make an array of all possible options, and loop them, and put checked value if the data exists.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745268725a4619614.html
评论列表(0条)