I want to validate my radio buttons so that if no radio button is selected, an alert message will appear on the page.
Now I know how to do this in javascript which is below:
if ( ( document.sessionform.sessionNo[0].checked == false )
&& ( document.sessionform.sessionNo[1].checked == false )
&& ( document.sessionform.sessionNo[2].checked == false )
&& ( document.sessionform.sessionNo[3].checked == false )
&& ( document.sessionform.sessionNo[4].checked == false ) )
{
alert ( "Please select the Number of Sessions you require" );
}
But I don’t want to do it in javascript, I want to do it in php. I tried to include the code above in the php tag but that did not work. How can I do this validation in php? Also I want to display it below where the radio buttons are displayed.
What on earth made you think the javascript code would work in a php script? Talk about trying to fit a square peg in a round hole !!
Anyway, all you have to do in php is check that at least one of the values of each radio button group has been sent.
There is a ship load of php examples on google.
I’m reluctant to spend more time on this because in the past you have copy and pasted your requests for help on multiple web sites (codingforums being 1) and so as I am writing this post, someone on another website might have already posted the solution for you.
No there is only 2 sites I use now which is this one and dreamincode as them 2 forums I have had replies from. Before I used to post a question in multiple sites but instead I post a question per one site. I will use this site to get hel on a question and then when this is figured out if I have another question I will use dreamincode and I keep alternating so that I don’t keep asking help in one forum site. Thanks anyway I will do further research in google :).
I don’t know why I thoght javascript code would work in php lol
Whether you post for help on multiple websites is up to you (and there is no rule against it and many do it) but if you get caught out you risk putting people off helping you because they would prefer to not spend time posting information/help you most likely have already received elsewhere.
Pssst…just between you and me (and anyone else reading this thread ;)) perhaps try to disguise your posts on multiple sites so that they are not as obvious - just some food for thought
Just do this in your form handler, and all will be revealed:
var_dump($_POST);
So if someone selects on of the options in sessionNo its value should appear in the POST array as:
var_dump($_POST['sessionNo']);
So, the absence of $_POST[‘sessionNo’] is a sign you can feed back to the user that they must pick a value.
if( !isset($_POST['sessionNo'])){
// oops they forgot
// send back with a message
exit();
}else{
// process the number, but just make sure it actually IS a number and not an attack by typecasting
// the var to a number
$sessionNo = (int)$_POST['sessionNo']
// so sessionNo will now either be typecast to a 0 or a number
if( $sessionNo > 4 ){
// something was wrong with the number submitted!
// send away
exit();
}
}
There is a bit more info which ought to get you going in the right direction … there are other ways of doing this check.