PHP Form Redirect Based Off of Drop Down Selection

2019-08-09 04:42发布

问题:

This relates to a previous question I had asked about filling out a php form and using those answers to populate a page. When finished filling out the form, I want to determine what page it goes to after you submit the form by a dropdown you choose while filling out the form.

For example, you type in:

Your Name

Email

Phone Number

Then from a drop down you'd pick "Page 3".

Then hit submit.

I would need it then to redirect you to "Page 3" because that's the one you chose in the drop down. I know this should be simple, I just can't seem to figure out how to do it. Thank you in advance to all that help!

回答1:

In the script your form submits to:

if ($_POST['dropdown-name'] == 'Page 3') {
    header("Location: page3.php");
} else if ($_POST['dropdown-name'] == 'Page 4') {
    header("Location: page4.php");
} // ... etc

~

$pages = array('Page 1' => 'page1.php', 'Page 2' => 'page2.php', 'Page 3' => 'page3.php');

if (array_key_exists($_POST['dropdown-name'], $pages)) {
    header("Location: " . $pages[$_POST['dropdown-name']]);
} else {
    echo "Error processing form"; // submitted form value wasn't in your array, perhaps a hack attempt
}


回答2:

Example

<script type="text/javascript">
function OnChangeForm()
{
    var e = document.getElementById("changeFormAct");
    var strType = e.options[e.selectedIndex].value;
    if(strType == '1')
    {
        document.myform.action ="football.html";   // here myform is the name of theform
    }
    else
    if(strType == '2')
    {
        document.myform.action ="cricket.html";
    }
    else
    if(strType == '3')
    {
        document.myform.action ="hockey.html";
    }
    else
    if(strType == '4')
    {
        document.myform.action ="tennis.html";
    }
    return true;
}
</script>

<select onchange="OnChangeForm" id="changeFormAct">
    <option value="1">Football</option>
    <option value="2">Cricket</option>
    <option value="3">Hockey</option>
    <option value="4">Tennis</option>
</select>