Link to home
Start Free TrialLog in
Avatar of napsternova
napsternovaFlag for United States of America

asked on

Form Submission decides URL

I have a PHP form with a few fields that will be entered into my database when I click Submit but I also have 2 radio buttons one YES and one NO.  These ask if more info is needed, so if YES is selected I need to be directed to the MoreInfo.php page if NO is selected I need to go to NewEntry.php page.  How do I do this?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or, you could just

if ($_POST['your_radio'] == "Yes") {
   include "MoreInfo.php";
} else {
   include "NewEntry.php";
}
Use javascript.  On the form tag, add
  onsubmit="setaction()")

Then create a function called set action that looks for the selected radio button.  Then modify the form action based on that. While you are at it, set return values.  If it turns out that a radio button was not selected, alert the user and return false.  The false value will prevent the form submission.  Return true if everything checks out ok.
I think something along these lines...

It has to be adapted a little for your html.  
<script type=text/javascript
//repace myformname and radioname with appropriate values for your 
//html tag names

function get_radio_value()
{
for (var i=0; i < document.myformname.radioname.length; i++)
   {
   if (document.myformname.radioname[i].checked)
      {
         var rad_val = document.myformname.radioname[i].value;
      }
   }
return rad_val();
}

function setaction() {
var radval = get_radio_value();
if (radval == 'YES') 
   {
      document.myformname.action = 'MoreInfo.php';
   } else {
      document.myformname.action = 'NewEntry.php';
   } 
}
</script>

Open in new window

I think I would use a single PHP action script, and would not do any redirect.  It will be easier to debug your code this way.  

You have to process the <form> data, so you would do that first.  Then the script would test the radio button to decide what HTML needed to be generated next.

I like yodercm's suggestion to use include() to load the scripts that generate the different follow-on pages.
Avatar of napsternova

ASKER

OK so based on the responses I should probably not be haveing an issue with this, but I am.  I'm going to try and make it easier on myself and instead of 2 radio buttons how about 1 checkbox?  So the database is set to default N.  The checkbox if checked should pass Y.  So if the box is checked and I hit Submit I should go to MoreInfo.php.  Now I made a slim version of my form.  I just want it to default to NewEntry.php  and when checked MoreInfo.php  Here is my checkbox code.
<input type="checkbox" name="Check" value="Y"<?php if ($Check == "Y") { echo " checked"; } ?> />
Bad spelling, sorry

OK so based on the responses I should probably not be having an issue with this, but I am.  I'm going to try and make it easier on myself and instead of 2 radio buttons how about 1 checkbox?  So the database is set to default N.  The checkbox if checked should pass Y.  So if the box is checked and I hit Submit I should go to MoreInfo.php.  Now I made a slim version of my form.  I just want it to default to NewEntry.php  and when checked MoreInfo.php  Here is my checkbox code.
<input type="checkbox" name="Check" value="Y"<?php if ($Check == "Y") { echo " checked"; } ?> />

By making a very simple application I was better able to deconstruct the code.  Thanks, works like a charm.