Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    problem with checkbox value

    I have a PHP file that looks like the following:
    Code:
    <?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.

  • #2
    New Coder
    Join Date
    May 2009
    Location
    Las Vegas
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?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".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •