Hi, I've been trying to have this one work, and it was working fine with checkboxes, but not with radio buttons. What do I need to do differently?
if(isset($_POST['submitted'])) {
$errors = array();
if (empty($_POST['date1']) && empty($_POST['date2']) && empty($_POST['date3']) && empty($_POST['date4'])) {
$errors[] = 'You did not select when you would like to attend.';
}
<form action="workshop_form.php" method="post" name="WorkshopForm">
<p>I would like to attend on:</p>
<input type="radio" name="dates" value="date1" /> 25 Sep 2008<br />
<input type="radio" name="dates" value="date2" /> 26 Sep 2008<br />
<input type="radio" name="dates" value="date3" /> 27 Sep 2008<br />
<input type="radio" name="dates" value="date4" /> 28 Sep 2008</p>
<input name="submitted" type="hidden" value="true" />
<input type="submit" value="Submit" />
</form>
PHP radio button validation - can't get it working... :(
Moderator: General Moderators
Re: PHP radio button validation - can't get it working... :(
You might have gotten a little confused in your coding. date1 - date4 are the values returned, not the POST variable containing the value. Dates would be the POST key you'd need to check.
Something more like ...
You'll notice I lost the hidden field and used a different method to detect the form. I recommend using this method instead, if for no other reason than because it means you don't need to clutter forms with hidden tags or use GET parameters to get the job done.
Hope this helps.
Something more like ...
Code: Select all
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['dates'])) echo "You chose " . $_POST['dates'];
else echo "Apparently, you dont want to attend.";
}
?>
<form action="" method="post" name="WorkshopForm">
<p>I would like to attend on:</p>
<input type="radio" name="dates" value="date1" /> 25 Sep 2008<br />
<input type="radio" name="dates" value="date2" /> 26 Sep 2008<br />
<input type="radio" name="dates" value="date3" /> 27 Sep 2008<br />
<input type="radio" name="dates" value="date4" /> 28 Sep 2008</p>
<input type="submit" value="Submit" />
</form>Hope this helps.
Re: PHP radio button validation - can't get it working... :(
Hi Stryks,
Trouble is that I'm hacking into someone else's longer code that works as is (and I'm a completely beginner to php), therefore I wouldn't like to change any other bit but these radio buttons - otherwise I won't know how to fix if anything else is broken.
It worked like this fine for the checkboxes and all I'd like to do is to change these options to radio buttons...
if(isset($_POST['submitted'])) {
$errors = array();
if (empty($_POST['info']) && empty($_POST['read']) && empty($_POST['comment'])) {
$errors[] = 'You did not select what you'd like to do.';
}
if (empty($errors)) {
$admin_email = "admin@domain.com";
$body .= "<strong>I would like to: </strong>";
if (!empty($_POST['info'])) $body .= "get info, ";
if (!empty($_POST['read'])) $body .= "read more, ";
if (!empty($_POST['comment'])) $body .= "or leave comments, ";
<p>I would like to:</p>
<input type="checkbox" name="info" /> Get more info<br />
<input type="checkbox" name="read" /> Read more on this issue<br />
<input type="checkbox" name="comment" /> Leave a comment<br />
<input name="submitted" type="hidden" value="true" />
<input type="submit" value="Submit" />
I realise that it's not standard to ask for troubleshooting before reading all the tutorials, but I've already spent more time than my boss would like to figure out this tiny bit of code... (I'm normally not doing any programming, but our programmer has just left for good)
So any help would be appreciated!
Trouble is that I'm hacking into someone else's longer code that works as is (and I'm a completely beginner to php), therefore I wouldn't like to change any other bit but these radio buttons - otherwise I won't know how to fix if anything else is broken.
It worked like this fine for the checkboxes and all I'd like to do is to change these options to radio buttons...
if(isset($_POST['submitted'])) {
$errors = array();
if (empty($_POST['info']) && empty($_POST['read']) && empty($_POST['comment'])) {
$errors[] = 'You did not select what you'd like to do.';
}
if (empty($errors)) {
$admin_email = "admin@domain.com";
$body .= "<strong>I would like to: </strong>";
if (!empty($_POST['info'])) $body .= "get info, ";
if (!empty($_POST['read'])) $body .= "read more, ";
if (!empty($_POST['comment'])) $body .= "or leave comments, ";
<p>I would like to:</p>
<input type="checkbox" name="info" /> Get more info<br />
<input type="checkbox" name="read" /> Read more on this issue<br />
<input type="checkbox" name="comment" /> Leave a comment<br />
<input name="submitted" type="hidden" value="true" />
<input type="submit" value="Submit" />
I realise that it's not standard to ask for troubleshooting before reading all the tutorials, but I've already spent more time than my boss would like to figure out this tiny bit of code... (I'm normally not doing any programming, but our programmer has just left for good)
So any help would be appreciated!
Re: PHP radio button validation - can't get it working... :(
Radio Buttons work quite differently than Check Boxes. Among other things, only one Radio Button in a group can be selected. The only way I know to help someone who isn't a PHP programmer to modify someone else's PHP script is to rewrite it for them, and you may find it difficult to find anyone here who is willing to spend the time to do that. We basically try to help PHP developers who have run into a specific problem, not rewrite other people's scripts.
Re: PHP radio button validation - can't get it working... :(
So that last block of code you posted ... that is the code as it stands??
If so, there is no reason you cant just implement the solution provided above.
If not, please post back your full page of code (using the
If so, there is no reason you cant just implement the solution provided above.
If not, please post back your full page of code (using the
Code: Select all
tags please) and you might get lucky with some more precise assistance.Re: PHP radio button validation - can't get it working... :(
> If so, there is no reason you cant just implement the solution provided above.
You were right Stryks, it worked!
Many thanks for all your help!!
You were right Stryks, it worked!
Many thanks for all your help!!