Jump to content

Recommended Posts

i am having issues checking which value is checked in a form.  here is my code for it:

 

<input type="radio" name="accountActivation" id="no" value="no" checked="
        <? 
	if (isset($register_errors)) {
		if (($_POST['accountActivation']) == "no") { 
			echo "checked"; 
		}
	}
	?>
        " />

 

when i first open the page, no is checked.  why is this, when there should be no errors, and the radio should not even be checked in the first place?

Link to comment
https://forums.phpfreaks.com/topic/245691-echoing-checked-value/
Share on other sites

All radio buttons can be "unchecked".  With radio buttons I don't think you use checked=.  You just use CHECKED.  If there is no CHECKED in the input tag, the radio button will be unchecked.

 

<input type="radio" name="accountActivation" id="no" value="no" 
        <? 
	if (isset($register_errors)) {
		if (($_POST['accountActivation']) == "no") { 
			echo "checked"; 
		}
	}
	?>
        " />

i think that the problem is though, that the code doesn't even work.  i check it, submit the form, and i come back to that field, and it is not checked.  here is my updated code:

 

<?php 
if(isset($_POST[errors]) && isset($_POST[noIdentity])) { 
	echo 'checked=yes'; 
} 
?>

if($sent != true) { 
echo"
<div class='form'><form action='' method='post'>
<p class='form'>User ID: <br /><input name='memberID' type='hidden' value='<?php echo $row[id]; ?>' />
  Your User ID will be logged for identity purposes. 
    <br /><input type='checkbox' name='noIdentity' id='noIdentity' value='noIdentity' 
    <?php 
if(isset($_POST[errors]) && isset($_POST[noIdentity])) { 
	echo 'checked=yes'; 
} 
?>
    <label for='noIdentity'>
    Check here if you would rather send information anonymously.</label></p>

<p class='form'><label for='otherEmail'>E-Mail (If the e-mail address is different from the one that you registered with, please enter it if you would like a response)<span style='color: red'>:</span></label><br /><input type='text' size='60' class='form'  name='otherEmail' value='$_POST[otherEmail]' /></p>

<p class='form'><label for='subject'>Subject:</label>  <span style='font-weight: bolder; color: red;'>*</span><br /><input  name='subject' type='text' class='form' id='subject' value='$_POST[subject]' size='60' /></p>

<p class='form'><label for='message'>Message:</label> <span style='font-weight: bolder; color: red;'>*</span> <br /><textarea name='message' cols='42' rows='9' class='form' id='message' value='$_POST[message]'></textarea></p>

<p class='form'><label for='phone'>Phone:</label>  <br /><input  name='phone' type='text' class='form' id='phone' value='$_POST[phone]' size='60' /></p>

<p class='form'><label for='websote'>Website URL (list the URL of the site that you had issues with):</label>  <br /><input  name='website' type='text' class='form' id='website' value='$_POST[website]' size='60' /></p>

<table><tr><td width='134'><p class='form' style='margin-top: 3px;'>AntiSpam Protection: <span style='font-weight: bolder; color: red;'>*</span>  </td>
<td width='120'><label for='antiSpam'>What is 7+3?</label></p></td><td><input type='text' name='antiSpam' class='captcha' size='6' maxlength='5' value='$_POST[antiSpam]'></td></tr></table>

<div align='center'>
<p>
<div style='line-height:1.25em;'>
  <strong><img src='http://mysite.com/Images/lock_icn.gif' alt='' width='14' height='16' /> Privacy Notice</strong>:   Content sent in this form will remain confidential and will not be sold   to third parties.  We respect your privacy and promise not to send you   spam.  We hate spam too! Do <strong>NOT</strong> send personal information, such as your social security number, through this form.</p>
</div>

<input type='submit' value='Send my Message!'  class='formsubmit' name='submit' /> 
        </form>
	<p>All fields marked with <span style='font-weight: bolder; color: red;'>*</span> are required.</p>
	<?php 
	";
}
	?>

Are you sure you only want to check the box is there are errors - isset($_POST[errors])?

 

Have you checked the two $_POST values that your logic is testing so that you know they have values that would cause your code to do what you expect?

Are you sure you only want to check the box is there are errors - isset($_POST[errors])?

 

no, also, if they had already checked the box.

 

here is the code that I'm testing on a separate page:

 

<form id="form1" name="form1" method="post" action="">
  <?php
  $errors=true;
echo "<input type='checkbox' name='noIdentity' id='noIdentity' value='noIdentity'";
if(isset($errors) && isset($POST['noIdentity'])) { 
	echo 'checked=checked'; 
}  else {echo 'bad'; }
echo "/>";
?>
  <input type="submit" name="submit" id="submit" value="Submit" />
</form>

no, also, if they had already checked the box.

 

^^^ That's not what your logic is doing. Your logic says if $_POST[errors] is set AND $_POST[noIdentity] is set, i.e. both of them must be set to produce a TRUE value.

 

In the last code you posted, you have $POST['noIdentity']. It should be $_POST['noIdentity']

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.