Dynamic Redirect

I am trying to create a form that uses a dynamic type of redirect. I was hoping for a php or javascript solution (if it even exists)

For example:

I will have two radio buttons (one is YES and the second one is NO). I will also have a submit button labeled NEXT.

If YES is pressed and the Next Button is selected the user would go to Page 1.

If NO is pressed and the Next Button is selected the user would go to Page 2.

Thanks in advance experts
bmw040Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

XemorphCommented:
Try this:
<html>
<head>
 <script type="text/javascript">
   function redirect(){
     var yes = document.getElementById('yes').checked;
  
     if(yes) window.location="page1.html";
     else window.location="page2.html";
 
   }
 </script>
</head>
<body>
   <input type="radio" id="yes" /> Yes <br />
   <input type="radio" id="no"  /> No <br />
   <input type="button" value="NEXT" onclick="redirect()" />
</body>
</html>

Open in new window

0
XemorphCommented:
Sorry, one error in my code.  add the name attribute to both radio buttons.  Make them the same.
0
Ray PaseurCommented:
You may want to process other information in the form, and to do that you may want to run a script on the server-side, instead of on the client-side in JavaScript.  If that is the case, this code will show you the way to do the dynamic redirect.

best regards, ~Ray
<?php // RAY_radio_redirect.php
error_reporting(E_ALL);
 
 
// GUIDANCE COPIED FROM THE OP
// If YES is pressed and the Next Button is selected the user would go to Page 1.
// If NO  is pressed and the Next Button is selected the user would go to Page 2.
 
 
// IF ANYTHING IS POSTED
if (!empty($_POST["my_radio"]))
{
 
 
//
// VALIDATE THE CONTENTS OF $_POST AND UPDATE A DATA BASE, ETC.
//
 
 
// SET THE DEFAULT (ERROR) PAGE
    $page = 'error.html';
 
// DETERMINE WHERE TO REDIRECT
    if ($_POST["my_radio"] == 'y') $page = 'page1.html';
    if ($_POST["my_radio"] == 'n') $page = 'page2.html';
 
// REDIRECT AND EXIT
    header("Location: $page");
    exit;
    
} // END OF PHP - PUT UP THE FORM WITH THE RADIO BUTTONS
?>
 
<form method="post">
<input type="radio" name="my_radio" value="y" />YES (PAGE 1)<br />
<input type="radio" name="my_radio" value="n" />NO (PAGE 2)<br />
<input type="submit" />
</form>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
profyaCommented:

<html>
<head>
 <script type="text/javascript">
   function GoToUrl(){
		var userOptions=document.forms[0].elements['userOption'];
		if (userOptions[0].checked)
			window.location=userOptions[0].value;
		else
			window.location=userOptions[1].value;
   }
 </script>
</head>
<body>
	<form>
   <input name="userOption" type="radio" value="www.domain1.com" checked/><label for="userOptions">www.domain1.com</label>
   <input name="userOption" type="radio" value="www.domain2.com"  /><label for="userOptions">www.domain2.com</label>
   <input type="button" value="Go" onclick="GoToUrl()" />
   </form>
</body>
</html>

Open in new window

0
bmw040Author Commented:
Thanks for the solution!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.