Jump to content
newbees

Checkbox Problem

Recommended Posts

Hi Sir/Ma'am,Good day!I made a checkbox and I use jquery on displaying one textbox if the user check the Yes checkbox but if the user check the no the textbox will be hide. It works fine now but when the checkbox has vale from the database and which is no the textbox was displayed so that I made a if condition that only if the checkbox is Yes or '' but I noticed that when I check the Yes the textbox was not display because from the database the value of textbox is No.

$(document).ready(function() {$(".ticket").change(function () {    //check if its checked. If checked move inside and check for others value       if (this.checked && this.value === "Yes") {        //add a text box next to it        $("#destination_data").show();    }    else if (this.checked && this.value === "No") {        //remove if unchecked        $("#destination_data").hide();    }   });});if($ticket == ''){?>       #destination_data {display: none; display:}<?php}?> <tr>        <td class="row_leaveform">Travel Tickets:</td>         <td class="row_leavedata">            <input type="checkbox" class="ticket" name="ticket" value="Yes" <?php if($ticket=="Yes") echo 'checked="checked"'; ?>>Yes               <input type="checkbox" class="ticket" name="ticket" value="No" <?php if($ticket=="No") echo 'checked="checked"'; ?>>No          </td>    </tr>     <tr id="destination_data">        <?php        if($ticket=="Yes" || $ticket=='')        {        ?>        <td class="row_leaveform">Destination </td>        <td class="row_leavedata"><b>Doha</b> to     <input type="text" name="destination" id="destination" value="<?php echo  $nationality; ?>" size="31"></td>        <?php        }        ?>    </tr>
it works fine when $ticket == '';but when in terms of update and the $ticket has value of Noin this code the destination was not display but when I try to check the Yes the destination was not display. I want it to display once I click the Yes if need to update my data.if my remove the if condition even the value of ticket is No the destination was display :(I want to happen is if$ticket == '' or $ticket == 'Yes' the destination display$ticket == 'No' destination not display but when I click the Yes the destination should be displayThank you.

Share this post


Link to post
Share on other sites

It is set in stone when page loads by php to show or hide input, showing the input when checking yes won't make any difference when $ticket is no because as on page loads no inputs exist to show.You need the inputs to be present all the time if 'yes', '' or 'no' but when 'no' hide the inputs with display: none; either using css as as you have

<?phpif($ticket == 'no'){?>       #destination_data {display: none;}<?php}?>
or inline
<?php$hide_show='';        if($ticket=="No")        {        $hide_show='style="display:none"';        }        ?><tr id="destination_data" <?php echo $hide_show; ?>>        <td class="row_leaveform">Destination </td>        <td class="row_leavedata"><b>Doha</b> to     <input type="text" name="destination" id="destination" value="<?php echo  $nationality; ?>" size="31"></td>            </tr>
If you no ticket produces blank input then you need if else to reflect that
<?php$hide_show='';        if($ticket=="No")        {        $hide_show='style="display:none"';        }        ?><tr id="destination_data" <?php echo $hide_show; ?>>        <td class="row_leaveform">Destination </td>        <td class="row_leavedata"><b>Doha</b> to     <?phpif($ticket=="Yes" || $ticket==''){?><input type="text" name="destination" id="destination" value="<?php echo  $nationality; ?>" size="31"><?php}else{?><input type="text" name="destination" id="destination" value="" size="31"><?php}?></td>            </tr>
In all cases the input exist and is availble to be shown when required Edited by dsonesuk

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...