Radio button validation

How to validate radio button in PHP

Page 1 of 1

5 Replies - 72648 Views - Last Post: 20 February 2008 - 09:09 AM Rate Topic: -----

#1 david4u  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 26-April 06

Radio button validation

Posted 25 May 2006 - 09:35 AM

Can anyone explain how I can validate this radio button PHP script.

<script language='javascript'>
  function show_errors(msg)
  {
	if (document.getElementById("errors").value != "")
	{
	  msg= document.getElementById("errors").value;
	  alert(msg);
	}//end if
  }
  

  
</script>


	<table width="400" border="0">
	  <tr>
		<td><input type="radio" name="q<?php echo $ID; ?>" value="SA">
		SA</td>
		<td><input type="radio" name="q<?php echo $ID; ?>" value="A">
		A</td>
		<td><input type="radio" name="q<?php echo $ID; ?>" value="N">
		N </td>
		<td><input type="radio" name="q<?php echo $ID; ?>" value="D">
		D</td>
		<td><input type="radio" name="q<?php echo $ID; ?>" value="SD">
		SD</td>
		<td><input type="radio" name="q<?php echo $ID; ?>" value="N/A">
		NA </td>
	  </tr>
	</table>



How can I validate the radio buttons that the users will be selecting. Right now if the user did not select anything, the script get submitted with empty answer in the database.

But I want the asnwers to be validated and checked before they submit it and should not allow submission without selecting any option.

Can anyone help pls.


[mod edit] added code tags.

Is This A Good Question/Topic? 0
  • +

Replies To: Radio button validation

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Radio button validation

Posted 25 May 2006 - 09:47 AM

You could just simply check if the form variable is empty.

//Note that $_REQUEST doesn't care where the variable comes from.
//So therefore qSOMETHING could come from GET, POST, or COOKIES.
if(isset($_REQUEST['qSOMETHING']) AND !empty($_REQUEST['qSOMETHING'])) {
   //database insert code
}

Was This Post Helpful? 1

#3 david4u  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 26-April 06

Re: Radio button validation

Posted 25 May 2006 - 09:56 AM

How can I apply that to this sample code above.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: Radio button validation

Posted 25 May 2006 - 10:04 AM

Please have a look at the rules. We aren't allowed to give out code unless you've shown some effort.
Was This Post Helpful? 0
  • +
  • -

#5 david4u  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 26-April 06

Re: Radio button validation

Posted 25 May 2006 - 10:14 AM

I have done it now.

Thanks. It is working.
Was This Post Helpful? 0
  • +
  • -

#6 krazymike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-February 08

Re: Radio button validation

Posted 20 February 2008 - 09:09 AM

I have a similar problem, but my form has a radio buttons, Yes and No. If the user chooses no, I want an error screen. A yes gets a thank you page. As it is, no matter what the user gets the error.

<td bgcolor="#000000"><font color=red>* </font>Are you registered on the website?</td>
			  <td bgcolor="#000000"><label>
				<input name="registered" type="radio" value="Yes">
				Yes</label>
				  <label>
				  <input name="registered" type="radio" value="No">
			  No</label></td>


$registered = $_REQUEST['registered'];
if ($registered == 'No'){
header( "Location: http://deadcell.clanservers.com/error.htm" );}
else{


I'm sure the solution is a simple one, but I'm pretty new to PHP. Please help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1