If Yes is selected for radio button, a second field is required in HTML

hardhitter06

Registered User.
Local time
Today, 02:45
Joined
Dec 21, 2006
Messages
600
Hi All,

Using Dreamweaver CS4 to design.

I have a radio button "ApprovalRequiredYesNo"...with of course the options of Yes and No.

If a user selects Yes, I need the html form to require the user to fill out an "Approver" and "ApproverEmail" fields.

From Googling around, I think I have to user a JavaScript, but I am new to HTML coding and don't know Java that well and was wondering if someone could show me how I would code this, or give me a similar example of a radio button selection resulting in another field being required.

Thank you in advance!
 
you could do it with javascript though you would need a fall back, in case the user has javascript disabled. The easiest way is to include the field anyway and just have something like - if you answered 'yes' above. please fill out this field. Of course, this places a trust on your users but the only other way to do it is javascript and as i said, you would need this as a fallback anyway. So i would start with this and then add to it when you know a bit of javascript!! hope this helps!!
 
Hi All,

Using Dreamweaver CS4 to design.

I have a radio button "ApprovalRequiredYesNo"...with of course the options of Yes and No.

If a user selects Yes, I need the html form to require the user to fill out an "Approver" and "ApproverEmail" fields.

From Googling around, I think I have to user a JavaScript, but I am new to HTML coding and don't know Java that well and was wondering if someone could show me how I would code this, or give me a similar example of a radio button selection resulting in another field being required.

Thank you in advance!
hardhitter,

I use PHP and integrated javascript to kick a user back if a field is not the way I want it. this code may be able to help you out...
PHP:
<?PHP

if ($_POST["type"] == "order") {
	if (! $_POST["Company"]) {
		echo '<script language="javascript">alert("Please fill in all required fields...");history.back();</script>';
		exit;
							 }
							   }
if (! $_POST["Name"] || ! $_POST["LName"] || ! $_POST["email"] || ! $_POST["phone"])
{
echo '<script language="javascript">alert("Please fill in all required fields...");history.back();</script>';
}
else
{
	if (strpos($_POST["email"], "@")) {
		//DO NOTHING
									 }
	else {
			echo '<script language="javascript">alert("Please provide a valid email address...");history.back();</script>';
			exit;
		 }
	if (strlen($_POST["phone"]) != 12)
		{
			echo '<script language="javascript">alert("Please provide a valid phone number...");history.back();</script>';
			exit;
		}
	else
		{
			for ($i = 0; $i <= 11; $i++) {
				if ($i == 3 || $i == 7) {
					if (substr($_POST["phone"], $i, 1) == "-") {
						//DO NOTHING
															   }
					else {
						echo '<script language="javascript">alert("Please provide a valid phone number...");history.back();</script>';
						exit;
						 }
						 				}
				else {
					if (is_numeric(substr($_POST["phone"], $i, 1))) {
						//DO NOTHING
																	}
					else {
						echo '<script language="javascript">alert("Please provide a valid phone number...");history.back();</script>';
						exit;
						 }
					 }
					 					  }
		}
?>
basically what this does is serves like an input mask in Access, for all the fields i have in the form. more than likely, the form you have is a $POST form in PHP. in the HTML code, check the method attribute in the form< tag to confirm this. if you do make use of this method, element references in PHP use the name attribute, so something like this maybe would work for you...
PHP:
<form name="myformname" method="post" action="myPHPscript.PHP" etc, etc...><element /> <element /> etc etc...</form>
a portion of your PHP submission script would be something like:
PHP:
if ($_post["myradiobutton"] == 1) {
   if (! $_post["approver"]) {
      echo '<script language="javascript">alert("You must provide an approver...");history.back();</script>';
      exit; }
}
 
Last edited:

Users who are viewing this thread

Back
Top Bottom