Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP form validation for multiple radio buttons 2

Status
Not open for further replies.

toby1985

Technical User
Jan 22, 2009
6
GB
Hello,

I'm new to PHP and am having problems getting PHP to validate a HTML form that contains multiple radio buttons and text fields.

Basically, the form has 7 sections, each pertaining to an MP3 track. In each section the user has to select one of four radio buttons (poor, average, good, excellent) and also enter comments in a text field. Note that the user doesn't have to enter feedback on EVERY section (track). Even if he/she chooses ONE radio button on ONE track (with no comment), that is acceptable.

In a nutshell, the PHP file has to determine whether ONE or MORE radio button/s are selected, or NONE at all. This then prompts it to confirm that the form is OK (and then processes it), or give an error message. Below is a sample of the HTML code for the radio buttons, and the PHP code I'm currently using which doesn't work:

HTML:
<input type="radio" name="Track1" value="poor">
<input type="radio" name="Track1" value="average">
<input type="radio" name="Track1" value="good">
<input type="radio" name="Track1" value="excellent">

PHP:
<?php
$to = 'myemail@domain.com';
$Track1 = $_POST['Track1'];
$Track1Cmnt = $_POST['Track1Cmnt'];
$Track2 = $_POST['Track2'];
$Track2Cmnt = $_POST['Track2Cmnt'];
$Track3 = $_POST['Track3'];
$Track3Cmnt = $_POST['Track3Cmnt'];
$Track4 = $_POST['Track4'];
$Track4Cmnt = $_POST['Track4Cmnt'];
$Track5 = $_POST['Track5'];
$Track5Cmnt = $_POST['Track5Cmnt'];
$Track6 = $_POST['Track6'];
$Track6Cmnt = $_POST['Track6Cmnt'];
$Track7 = $_POST['Track7'];
$Track7Cmnt = $_POST['Track7Cmnt'];
if (empty($Track1) || empty($Track1Cmnt) || empty($Track2) || empty($Track2Cmnt) || empty($Track3) || empty($Track3Cmnt) || empty($Track4) || empty($Track4Cmnt) || empty($Track5) || empty($Track5Cmnt) || empty($Track6) || empty($Track6Cmnt) || empty($Track7) || empty($Track7Cmnt)) {
echo "<font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Submission error. Please enter some user input before pressing 'Submit'.</font>";
echo "<br><br><a href='javascript:history.back(1);'><font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Back to main page</font></a>";
}
else {
echo "<font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Thank you for sending your opinion/s on the selected track/s.</font>";
echo "<br><br><br><br><a href='soundbooth.htm' target='_parent'><font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Back to main page</font></a>";
mail($to, $subject, $msg");
}
?>

I have a feeling that the processing part at the end (mail($to, etc.)) is wrong as well... any tips and advice will be greatly appreciated. Thanks in advance for your help!

Toby
 
Hi Toby,

sorry, it's already late here so I couldn't take a look at your problem in depth.

What I just saw is your "if-statement"

Code:
if (empty($Track1) || empty($Track1Cmnt) || empty($Track2) || empty($Track2Cmnt) || empty($Track3) || empty($Track3Cmnt) || empty($Track4) || empty($Track4Cmnt) || empty($Track5) || empty($Track5Cmnt) || empty($Track6) || empty($Track6Cmnt) || empty($Track7) || empty($Track7Cmnt))

This results in, that the user has to fill in ALL fields.
You should change the || to && (however this still wouldn't be the exact result you expect, but it's a bit closer ;))

Regarding your mail function, well, where do you set the value for $subject and $msg?
 
Hi Preachie,

thanks for the quick response!

I'm such a novice at PHP... hopefully I'll pick it up as I go along :)

OK, I've changed the || to && in the 'if' statement, but am probably still very far from a solution. Re. the mail function, I suppose I should define $subject as $subject = "Soundbooth Feedback".

As for the $msg, this should be the result of the submitted form data sent to the email address, something on the lines of:

Track1 = "good" Track1Cmnt = "Drums very faint, otherwise OK." Track2 = "average" Track2Cmnt = "Bassline too repetitive"
...and so on.

I've done a lot of Googling on this problem, visited many PHP forums for help, but am still getting nowhere. I really appreciate your help, though!

Toby
 
As preachie points out, your IF statement makes it so that every radio button must be checked and every textbox must be filled in order to proceed. If any of them are not checked or filled in then you get the error.

Additionally, assigning the values of the POST array as you are doing is sure to produce undefined index notices when something is not filled in or one of the radio button groups has none of the radio buttons selected.

Code:
Notice: Undefined index: TrackX in ....


You should check to see what has been submitted before assigning them to variables.

All radio button groups need to have an option checked, but not all textboxes need to be filled in. Or they only need to be filled in if a radio button has been checked?






----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi Vacunita,

thanks for the reply. Basically, the form should be submitted + processed even if only ONE radio button has been checked. Likewise, the user can enter a comment into just ONE text field, not selecting any radios, the point being that the PHP script should never accept a 'blank' form where NOTHING has been input by the user.

I've now changed the '||' symbol to '&&' in the IF statement as Preachie suggested... this has definitely improved the situation but I need to know one more thing:

How can I get the checked radio button Name/Value pair(s) (eg. Track1 = "good") to be incorporated into the message sent to the recipient email?

I hope I have made the problem clearer now... as for the 'undefined index notices', I have no idea what these are, can you elaborate?

Thanks for the help anyway. Please bear with me, I'm new to this and appreciate your patience :)

Toby
 
O.k I see. So the only time it should not move on is when the entire form is empty.


Having the && is the first part. This check's to see if everything is empty, then it issues the error. So far so good.


How can I get the checked radio button Name/Value pair(s) (eg. Track1 = "good") to be incorporated into the message sent to the recipient email?


You'll have to grab them from the variables and concatenate them into your message variable.

Code:
$msg="";
$msg.="Track1=" . $Track1 . "<br>";
$msg.="Track2=" . $Track2 . "<br>";
...

[green]\\You can add the comments the same way:[/green]

$msg.="Comment for Track 1: " . $Track1Cmnt . "<br>";
$msg.=Comment for Track 2: " . $Track2Cmnt . "</br>";
...

As for the Notice I mentioned, that just happens when you try to use a variable that does not exist.

Most web servers turn the display of errors and notices off, but its still something you should account for.

For example if notihng is selected in the radio button group fro Track1, then the variable $_POST['Track1'] will not exists. When you try to do $track1=$_POST['Track1']; a Notice will be issued.

Now a notice does not stop execution of the script, but if you are relying on the vari9able further down, it may cause trouble.

I know this is not the case, but imagine at one point you need to perform a division using the variable, if it doesn't have anything it will default to 0, and that's when the script will catastrophically fail.

Which is why before assigning them to variables you should check they exist.

In other words:

Code:
$Track1 = (isset($_POST['Track1'])) ? '$_POST['Track1'] : '';
$Track1Cmnt = (isset($_POST['Track1Cmnt'])) ?  $_POST['Track1Cmnt'] : '';

$Track2 = (isset($_POST['Track2'])) ? '$_POST['Track2'] : '';
$Track2Cmnt = (isset($_POST['Track2Cmnt'])) ?  $_POST['Track2Cmnt'] : '';

...
This checks to see whether $_POST['Track1'] is set using the isset() function if it is it assigns the value to your variable, otherwise, it just makes the variable and empty string.

That way it can be used in your If statement down the line.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi Vacunita,

that's great! Many thanks for solving the "msg" issue. It all makes sense now :) Also thanks for the code and explanations re. the index notice, I see what you mean. I've come across the isset() function on other boards, etc, but haven't used it yet. Was sure it was something to do with validation though.

Here's another little problem:

The 'Name' textfield on my form is called 'UserName' and when the user submits the form, this reaches the PHP page as $_POST['Username'] .

So I've done this:

$Username = $_POST['Username'];
$headers = "From: $Username";

and in the mail() part, I've got:

mail($to, $subject, $message, $headers);

But it's not sending the "From: $Username" (in $headers) to the "From" field in the recipient's message/inbox. The recipient's "From" field says:

From: "domainXXXXXXX" <domainXXXXXXX@fhlinux164.hostcom.com>

When they sould be getting the Username. Maybe the host is over-riding the "From" issued by the PHP page?

Thanks again for all your time and help!

Toby
 
Your host could be forcing the form field for the emails. Because there's nothing wrong with your $headers variable.

Unless of course $_POST['Username'] is empty which could of course make the mail function use whatever default is set by the server.

Are you sure your textbox name starts with a capital U? variables in PHP are case sensitive, so $_POST['Username'] is not the same as $_POST['username'];




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 

Hi Vacunita,

some bad news and some very GOOD news :) ... first, let me address your points:

a) IF the host is overriding the "From" field, is it possible to get around this?

b) $_POST['Username'] can never be an empty field (it's mandatory).

c) I've checked again, and the caps/l-cases match up exactly throughout.

Moving on... the bad news first. :/

When i use the isset() code you gave me, i get a whitescreen error saying:

Parse error: syntax error, unexpected T_STRING in (.....filename).php on line 8

which is the line where the isset() code starts.

So I went back to my original

$Track1 = $_POST['Track1']; - w/out the isset()

and it worked just fine, no more errors.

... but don't worry, I've finally got the result I need (thanks to you!) ...now for the really GOOD news :))

Your 'concatenation' code has perfectly solved the problem. Now even if the "From" field doesn't give Username, I can get it in the body of the message. I don't care, as long as the recipient can see who sent him that feedback!!

$msg="";
$msg.="From=" . $Username . "<br>";

Here's the recipient messg body:
--------------------------------------------------
From=Jimmy Jim<br>Track1=average<br>Comment for Track 1:You can do better than this!<br>Track2=good<br>Comment for Track 2:nice groove and harmonics, work on it<br>....
--------------------------------------------------

That's acceptable.. so everything's OK now :) Many thanks for all your help... you're a champ!

Toby
 
Code:
]
$Track1 = (isset($_POST['Track1'])) ? [red]>>[/red]'[red]<<[/red]$_POST['Track1'] : '';
]

Sorry, I have an extra quote that should not be there, the one marked in red.


a) IF the host is overriding the "From" field, is it possible to get around this?
I don't know. There might be.
b) $_POST['Username'] can never be an empty field (it's mandatory).

That's all well an good, but do you have some other verification mechanism in place to make sure its filled in? Just saying its required does not mean the user will fill it in.

c) I've checked again, and the caps/l-cases match up exactly throughout.

Then the server may be overriding the from value.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi Vacunita,

I'm hoping to find a way to insert Username into the email's "From" field... needs some looking into.

You said:
"That's all well an good, but do you have some other verification mechanism in place to make sure its filled in? Just saying its required does not mean the user will fill it in."

I did the "Username" verification code before I came here with the 'just one input' problem :) The form doesn't process without it. Here's the code I've used:

Code:
elseif ($Username == "") {
echo "<font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"#000000\">Error: please enter a user name.</font>";
}

So all's well in the end... and yeah, I'm going to re-use the correct isset() code u gave me!

Many thanks for everything,

T
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top