...

View Full Version : problem with checkbox value



Daxini
05-05-2009, 03:12 AM
I have a PHP file that looks like the following:

<?php
session_start();
$error_message = array();
if ($_POST['submit'] != '')
{ foreach($_POST as $key=>$value)
{ $_SESSION[$key]=$value; }
if($_SESSION[licence]=="Yes")
{if($_SESSION[licencetype]=="")
error_message[] = 'Please enter your Licence Type';}
if (count($error_message) == 0)
include("somefile.php");
}
if($_POST['submit'] == '' || count($error_message) > 0)
{ if (count($error_message) > 0)
foreach ($error_message as $value) echo "<li>$value</li>";
?>
<html><form method="POST" name="reg" >
<label for="transport" class="required">What transport will you be utilizing?/label>
<input type="checkbox" id="transport1" name="transport1" tabindex="1" title="public vehicle" <?php echo "value=".(isset($_SESSION[transport1]) ? "\"Yes\"": "\"No\""); echo "checked=".($_SESSION[transport1]=="Yes" ? true: false); ?>> Public Transport
<input type="checkbox" id="transport2" name="transport2" tabindex="2" title="private vehicle" <?php echo "value=".isset($_SESSION[transport1]) ? "\"Yes\"": "\"No\""; echo "checked=".($_SESSION[transport1]=="Yes" ? true: false); ?>> Private Vehicle<br />
<label for="licence" class="required">Do you hold a current driver's licence?</label>
<select id="licence" name="licence" tabindex="3" title="licence" >
<option value="No" <?php if ($_SESSION['licence'] == 'No') echo "selected=\"selected\"";?> >No</option>
<option value="Yes" <?php if ($_SESSION['licence'] == 'Yes') echo "selected=\"selected\"";?> >Yes</option>
</select><br />
<label for="licencetype" class="required">What type?</label>
<input type="text" name="licencetype" id="licencetype" tabindex="4" title="licence type" value="<?php echo $_SESSION['licencetype']; ?>" ><br />
<input type="submit" value="Continue" id="submit" name="submit" tabindex="5"></form></html><?php } ?>

what i am trying to do is that when the user clicks SUBMIT, my PHP code is executed and do some validations. If there is no error on page then it include another page but if there is error on page then the form should show error and retain & show all the values selected or entered on the form.
I am getting problem with checkbox. I am checking that if checkbox is set then value=Yes otherwise value=No and if value=Yes then atrribute checked=checked.
I want to submit the checkbox value yes/no in database and checkbox should show and retain the value when any error occurs on page. Problem is that once i checked checkbox value is set to "Yes" and when form is submitted and an error occurs then checkbox is displayed checked but when i unchecked the checkbox, value of checkbox is not set to "No" so when i submit again is displayed checked.

discoboomer
05-05-2009, 04:23 AM
<?php if (!(strcmp($_SESSION[transport1],"yes"))) {echo "checked=\"checked\"";} ?>

Would this code help you out? I have only tried it with a 1 instead of a "yes".



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum