PHP radio button validation - can't get it working... :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gagocs
Forum Newbie
Posts: 3
Joined: Mon Sep 22, 2008 6:22 am

PHP radio button validation - can't get it working... :(

Post by gagocs »

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>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: PHP radio button validation - can't get it working... :(

Post by Stryks »

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

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>
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.
gagocs
Forum Newbie
Posts: 3
Joined: Mon Sep 22, 2008 6:22 am

Re: PHP radio button validation - can't get it working... :(

Post by gagocs »

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!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP radio button validation - can't get it working... :(

Post by califdon »

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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: PHP radio button validation - can't get it working... :(

Post by Stryks »

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

Code: Select all

 tags please) and you might get lucky with some more precise assistance.
gagocs
Forum Newbie
Posts: 3
Joined: Mon Sep 22, 2008 6:22 am

Re: PHP radio button validation - can't get it working... :(

Post by gagocs »

> 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!!
Post Reply