Solved

Form Submission decides URL

Posted on 2010-09-16
8
261 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:napsternova
8 Comments
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 33698238
Hi napternova,

In the PHP that processes the form, you will check the value of the selected radio button and then set a redirect based on that value:

if ($_POST['your_radio'] == "Yes") {
   $redirect_to = "MoreInfo.php";
} else {
   $redirect_to = "NewEntry.php";
}

header("Location: $redirect_to");
0
 
LVL 27

Expert Comment

by:yodercm
ID: 33698259
Or, you could just

if ($_POST['your_radio'] == "Yes") {
   include "MoreInfo.php";
} else {
   include "NewEntry.php";
}
0
 
LVL 9

Expert Comment

by:rfportilla
ID: 33698268
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.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 9

Expert Comment

by:rfportilla
ID: 33698302
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

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33701060
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.
0
 

Author Comment

by:napsternova
ID: 33702051
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"; } ?> />
0
 

Author Comment

by:napsternova
ID: 33702061
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"; } ?> />

0
 

Author Closing Comment

by:napsternova
ID: 33702703
By making a very simple application I was better able to deconstruct the code.  Thanks, works like a charm.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress Pagination Function Not working ? 7 39
Why is my wamp get_include_path() wrong? 2 21
XML extra information 8 29
JQuery Search Filter 2 28
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question