Jump to content

Check box stay ticked


siabanie

Recommended Posts

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

<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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.