siabanie Posted August 27, 2011 Share Posted August 27, 2011 Hi guys, I have some confusion here - I am trying to create a check box where when you tick the box but forget to fill in some other fields - How can you make sure the box stays ticked when we are taken back to the application page? Here is snippets of the code: .. $error = 0; $errormsg = ""; .. if( !isset($_POST['noemail']) && (empty($_POST['email1']) || !check_text($_POST['email1'])) ) { $error = 1; $errormsg .= "Please enter your requested email address 1<br>"; $errornum[3] = 1; } if( !isset($_POST['noemail']) && (empty($_POST['email2']) || !check_text($_POST['email2'])) ) { $error = 1; $errormsg .= "Please enter your requested email address 2<br>"; $errornum[3] = 1; }.. The Form .. .. <tr> <td><input name="noemail" type="checkbox" value="noemail"> Do not want to get the E-mail address </td> </tr> <tr> <td nowrap>First Choice <input name="email1" type="text" <?php if ($_POST['action'] == "register") { echo 'value="'.$_POST['email1'].'"'; } ?>> </td> </tr> <tr> <td nowrap>Second Choice <input name="email2" type="text" <?php if ($_POST['action'] == "register") { echo 'value="'.$_POST['email2'].'"'; } ?>> </td> </tr> I tired used session e.g: <td><input name="noemail" type="checkbox" value="noemail"<?php echo ($_SESSION["noemail"]=='noemail' ? ' checked="checked"' : '');?>> but it is still did not work - can anyone please assist me what I need to edit or did wrong. Thanks. Link to comment https://forums.phpfreaks.com/topic/245800-check-box-stay-ticked/ Share on other sites More sharing options...
voip03 Posted August 27, 2011 Share Posted August 27, 2011 <html> <head> <title>Checkboxes</title> <?php $ch1 = 'unchecked'; $ch2 = 'unchecked'; if (isset($_POST['Submit'])) { if (isset($_POST['ch1'])) { $ch1 = $_POST['ch1']; if ($ch1 == 'net') { $ch1 = 'checked'; } } if (isset($_POST['ch2'])) { $ch2 = $_POST['ch2']; if ($ch2 == 'word') { $ch2 = 'checked'; } } } ?> </head> <body> <FORM NAME ="form" METHOD ="POST" ACTION ="#"> <Input type = 'Checkbox' Name ='ch1' value ="net" <?PHP print $ch1; ?>>PHP for the Beginner <P> <Input type = 'Checkbox' Name ='ch2' value="word" <?PHP print $ch2; ?>>Web Design for the Beginner <P> <INPUT TYPE = "Submit" Name = "Submit" VALUE = "Choose your books"> </FORM> </body> </html> Link to comment https://forums.phpfreaks.com/topic/245800-check-box-stay-ticked/#findComment-1262561 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.