Hello frz, i want to ask a question on checkbox selection. Tell me how to select checkboxes based on the data stored in mysql database. Suppose we have to select checkboxes in a registration form when the user edits his profile. Suppose if there are some checkboxes for hobbies. Then how can we select checkboxes in a form?
4 replies to this topic
#1
Posted 11 August 2011 - 01:32 AM
|
|
|
#2
Posted 11 August 2011 - 04:34 AM
<input type="checkbox" name="hobbies[]" value="football" <?php echo($likeFootball ? 'checked="checked"' : ''); ?> />Football<br />When the variable $likeFootball is a boolean from the database that said if he had checked this before
#3
Posted 11 August 2011 - 06:06 AM
thanx my frd for replying me and to support me. But my problem is still not solved. I have given my two files here. You plz run this script and remove my error. The problem is that if the user selects all the three checkboxes then when form is returned back to itself then all the three checkboxes are selected but when user doesn't select all three checkboxes then they are not selected. Here are my files -
formdesg.php file -
formcode.php file -
This script is actually a form validation script on server side. When form doesn't pass validation rule then it is returned back to design file.
Plz remove the problem regarding checkboxes.
formdesg.php file -
<html>
<head>
<title>PHP Form Validation</title>
</head>
<?php
$msg=isset($_REQUEST["msg"])?$_REQUEST["msg"]:"";
$uname=isset($_REQUEST["uname"])?$_REQUEST["uname"]:"";
$fname=isset($_REQUEST["fname"])?$_REQUEST["fname"]:"";
$lname=isset($_REQUEST["lname"])?$_REQUEST["fname"]:"";
$gender=isset($_REQUEST["gender"])?$_REQUEST["gender"]:"";
$hobbies=$_REQUEST["hobbies"]?explode(", ",$_REQUEST["hobbies"]):"";
$day=isset($_REQUEST["day"])?$_REQUEST["day"]:"";
$month=isset($_REQUEST["month"])?$_REQUEST["month"]:"";
$year=isset($_REQUEST["year"])?$_REQUEST["year"]:"";
$qual=isset($_REQUEST["qual"])?$_REQUEST["qual"]:"";
$mobileno=isset($_REQUEST["mobileno"])?$_REQUEST["mobileno"]:"";
$email=isset($_REQUEST["email"])?$_REQUEST["email"]:"";
$address=isset($_REQUEST["address"])?$_REQUEST["address"]:"";
echo "<div style='color:red'>$msg</div>";
?>
<body>
<form name="form1" method="post" action="FormCheck.php">
<input type="hidden" name="mode" value="validate">
<table style="position:absolute; left:350px; top:10px" width="50%" border="0" align="center">
<tr>
<th><div align="right">Username:</div></th>
<td><input type="text" name="txtuname" size="30" value="<?=$uname?>"></td>
</tr>
<tr>
<th><div align="right">Password:</div></th>
<td><input type="password" name="txtpass" size="30"></td>
</tr>
<tr>
<th><div align="right">Re-password:</div></th>
<td><input type="password" name="txtrpass" size="30"></td>
</tr>
<tr>
<th><div align="right">Firstname:</div></th>
<td><input type="text" name="txtfname" size="30" value="<?=$fname?>"></td>
</tr>
<tr>
<th><div align="right">Lastname:</div></th>
<td><input type="text" name="txtlname" size="30" value="<?=$lname?>"></td>
</tr>
<tr>
<th><div align="right">Gender:</div></th>
<td><label for="male"><input name="gender" id="male" type="radio" value="Male" <?php if($gender=="Male") echo "checked" ?>>Male</label><br>
<label for="female"><input name="gender" id="female" type="radio" value="Female" <?php if($gender=="Female") echo "checked" ?>>Female</td>
</tr>
<tr>
<th><div align="right">Hobbies:</div></th>
<td>
<label for="reading"><input type="checkbox" id="reading" name="hobbies[]" value="Reading" <?php if($hobbies[0]=="Reading") echo "checked" ?>>Reading</label>
<br>
<label for="writing"><input type="checkbox" id="writing" name="hobbies[]" value="Writing" <?php if($hobbies[1]=="Writing") echo "checked" ?>>Writing</label>
<br>
<label for="playing"><input type="checkbox" id="playing" name="hobbies[]" value="Playing" <?php if($hobbies[2]=="Playing") echo "checked" ?>>Playing</label>
</td>
</tr>
<tr>
<th><div align="right">DOB:</div></th>
<td><select name="cmbday">
<option value="DD">DD</option>
<?php
for($i=1;$i<=31;$i++)
{
$selected="";
if($day==$i)
{
$selected="selected='selected'";
}
echo "<option value='$i' $selected >$i</option>";
}
?>
</select>
<select name="cmbmonth">
<option value="MM">MM</option>
<?php
for($i=1;$i<=12;$i++)
{
$selected="";
if($month==$i)
{
$selected="selected='selected'";
}
echo "<option value='$i' $selected >$i</option>";
}
?>
</select>
<select name="cmbyear">
<option value="YY">YY</option>
<?php
for($i=2000;$i>=1950;$i--)
{
$selected="";
if($year==$i)
{
$selected="selected='selected'";
}
echo "<option value='$i' $selected>$i</option>";
}
?>
</select></td>
</tr>
<tr>
<th><div align="right">Qualification:</div></th>
<td><select name="cmbqual" style="width:150px">
<option value="null">Select your degree...</option>
<optgroup label="Arts">
<option value="BA" <?php if($qual=="BA") echo "selected"?>>BA</option>
<option value="MA" <?php if($qual=="MA") echo "selected"?>>MA</option>
</optgroup>
<optgroup label="Science">
<option value="BSc" <?php if($qual=="BSc") echo "selected"?>>BSc</option>
<option value="MSc" <?php if($qual=="MSc") echo "selected"?>>MSc</option>
</optgroup>
<optgroup label="Computer">
<option value="BCA" <?php if($qual=="BCA") echo "selected"?>>BCA</option>
<option value="MCA" <?php if($qual=="MCA") echo "selected"?>>MCA</option>
</optgroup>
<optgroup label="Engineering">
<option value="BTech" <?php if($qual=="BTech") echo "selected"?>>BTech</option>
<option value="MTech" <?php if($qual=="MTech") echo "selected"?>>MTech</option>
</optgroup>
</select></td>
</tr>
<tr>
<th><div align="right">Address:</div></th>
<td><textarea name="taaddress" cols="25" rows="10"><?=$address?></textarea></td>
</tr>
<tr>
<th><div align="right">Mobile No: </div></th>
<td><input type="text" name="txtmobile" size="30" value="<?=$mobileno?>"></td>
</tr>
<tr>
<th><div align="right">Email ID: </div></th>
<td><input type="text" name="txtemail" size="30" value="<?=$email?>"></td>
</tr>
<tr>
<th><div align="right">
<input type="submit" name="btnsubmit" value="Submit">
</div></th>
<td><input type="reset" value="Clear"></td>
</tr>
</table>
</form>
</body>
</html>
formcode.php file -
<?php
$uname=$_REQUEST["txtuname"];
$password=$_REQUEST["txtpass"];
$rpassword=$_REQUEST["txtrpass"];
$fname=trim($_REQUEST["txtfname"]);
$lname=trim($_REQUEST["txtlname"]);
$gender=$_REQUEST["gender"];
$hobbies=implode(", ",$_REQUEST["hobbies"]);
$day=$_REQUEST["cmbday"];
$month=$_REQUEST["cmbmonth"];
$year=$_REQUEST["cmbyear"];
$qual=$_REQUEST["cmbqual"];
$address=$_REQUEST["taaddress"];
$mobileno=$_REQUEST["txtmobile"];
$email=$_REQUEST["txtemail"];
$msg="";
$formStatus="OK";
if(strlen($uname)<10)
{
$formStatus="notOK";
$msg.="Username must be at least 10 characters long.<br>";
}
if(!ctype_alnum($uname))
{
$formStatus="notOK";
$msg.="Username must be in alphanumeric characters.<br>";
}
if(strlen($password)<1)
{
$formStatus="notOK";
$msg.="Password can not be left blank.<br>";
}
if($password!=$rpassword)
{
$formStatus="notOK";
$msg.="Password does not match.<br>";
}
if(strlen($fname)<1||strlen($lname)<1)
{
$formStatus="notOK";
$msg.="Name can not be left blank.<br>";
}
if(!ctype_alpha($fname)||!ctype_alpha($lname))
{
$formStatus="notOK";
$msg.="Name must be in alphabets.<br>";
}
if(strlen($gender)<1)
{
$formStatus="notOK";
$msg.="Please select your gender.<br>";
}
if(strlen($hobbies)<1)
{
$formStatus="notOK";
$msg.="Please select your hobby.<br>";
}
if($day=="DD"||$month=="MM"||$year=="YY")
{
$formStatus="notOK";
$msg.="Please select your DOB.<br>";
}
if(!checkdate($month,$day,$year))
{
$formStatus="notOK";
$msg.="DOB is not valid.<br>";
}
if($qual=="null")
{
$formStatus="notOK";
$msg.="Please select your qualification.<br>";
}
if(strlen($address)<1)
{
$formStatus="notOK";
$msg.="Please enter your address.<br>";
}
if(strlen($mobileno)!=10||!(is_numeric($mobileno)))
{
$formStatus="notOK";
$msg.="Please enter valid mobile number.<br>";
}
if(!preg_match("/^[a-z0-9_]+@[a-z0-9]+\.[a-z]{2,3}$/",$email))
{
$formStatus="notOK";
$msg.="Please enter valid email ID.<br>";
}
if($formStatus=="OK")
{
echo "Username is $uname";
echo "<br>Password is $password";
echo "<br>Repeat-password is $rpassword";
echo "<br>Name is $fname $lname";
echo "<br>Gender is $gender";
$hobbies=implode(", ",$_REQUEST["hobbies"]);
echo "<br>DOB is $day/$month/$year";
echo "<br>Qualification is $qual";
echo "<br>Address is ".nl2br($address);
echo "<br>Mobile no. is $mobileno";
echo "<br>Email ID is $email";
}
else
{
header("location:FormDesg.php?msg=$msg&uname=$uname&fname=$fname&lname=$lname&gender=$gender&hobbies=$hobbies&qual=$qual&day=$day&month=$month&year=$year&address=$address&mobileno=$mobileno&email=$email");
}
?>
This script is actually a form validation script on server side. When form doesn't pass validation rule then it is returned back to design file.
Plz remove the problem regarding checkboxes.
#4
Posted 11 August 2011 - 06:21 AM
This is because if Reading isn't selected, but Writing is, the variable $hobbies[0] = "Written" not $hobbies[1] = "Written"
Use
---------- Post added at 06:21 AM ---------- Previous post was at 06:19 AM ----------
Or you could use
But I think the in_array option is the best
Use
<?php if(in_array("Reading", $hobbies)) echo "checked" ?>
Insted---------- Post added at 06:21 AM ---------- Previous post was at 06:19 AM ----------
Or you could use
<label for="reading"><input type="checkbox" id="reading" name="hobbies[0]" value="Reading" <?php if($hobbies[0]=="Reading") echo "checked" ?>>Reading</label> <br> <label for="writing"><input type="checkbox" id="writing" name="hobbies[1]" value="Writing" <?php if($hobbies[1]=="Writing") echo "checked" ?>>Writing</label> <br> <label for="playing"><input type="checkbox" id="playing" name="hobbies[2]" value="Playing" <?php if($hobbies[2]=="Playing") echo "checked" ?>>Playing</label>
But I think the in_array option is the best
#5
Posted 11 August 2011 - 07:28 AM
thanx my dear frd. God bless u. You really did a great job. I had been facing this very problem for last 4 - 5 months. Finally i am able to remove my error just because of you. The array fun. in_array() is the keystone. I am really very glad. If i'll face some other problems in PHP development then plz stay with me. I will be very thankful to you. Thanx again.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









