开发者

I need to have a form be filled out, go to a page chosen in a drop down then use the other info from the form to populate the chosen page

开发者 https://www.devze.com 2023-02-06 23:27 出处:网络
I have asked two previous questions for taking info put into a form and using it to populate a page, and one to redirect you to a page when you submit the form based off of what you choose in the form

I have asked two previous questions for taking info put into a form and using it to populate a page, and one to redirect you to a page when you submit the form based off of what you choose in the forms drop down. Now I need these two things to both work for the same form.

The code for taking the info from the form and using it to populae the next page is:

<?php
$firstname = $_GET['personsName'];
echo "My Name is" .$firstname;
?>

And the form would look like this:

<form action="letter.php" method="get">
<input type="text" name="personsName"></input>
<input type="submit" value="submit">
</form>

The code for selecting a page is:

$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
}

I need for both of these to work for the same form, I just haven't been able to figure it out. What I tried was:

<form action="<?=$pages?>" method="POST">
<input type="text" name="name" /><br />
<select name="letter">

<option value="Page 1">
Page 1
</option>

<option value="Page 2">
Page 2
</option>

<option value="Page 3">
Page 3
</option>

</select>
<input type="submit" value"Print" />
</form>

This is obviously wrong. I need this form to take the info put into it, redirect to the page chosen, then populate the page with the other info inputted into the form. The problem is I have no idea how to get the answers I already have together. Thank you to all that help!

Current code is this:

<script type="text/javascript">
$(function UpdateFormAction(){
alert('Launched event handler');
var form = document.getElementById('MyForm');
var list = document.getElementById('PageList');
alert('List item numer ' + list.selectedIndex);
var desiredAction = list.options[list.selectedIndex].value
alert('Desired action set to ' + desiredAction);
form.action = desiredAction;
});
</script>

Form is:

<form id="MyForm" action="letter.php" method="POST">
<input type="text" name="name" /><br />
<select id="PageList" name="letter" onchange="UpdateFormAction();">

<option value="letter.php">Page 1</option>
<option value="letter2.php">Page 2</option>
<开发者_JS百科option value="letter3.php">Page 3</option>
</select>
<input type="submit" value"Print" />
</form>


You're losing the data sent to the server when you do the redirect.

Basically, you've got 3 options:

  • Store the data on the client using a cookie or similar (A lot of overhead for something simple)
  • Store the data server-side in a session or similar (even more overhead)
  • Pass the data through the URL you're redirecting to...

So something like:

if (array_key_exists($_POST['dropdown-name'], $pages)) {
    header("Location: " . $pages[$_POST['dropdown-name']] . "?personsName=" urlencode($_POST['name']));
} else {
    //Blah
}

Which seems to be the best answer for your simple example. If there's a LOT of data or it's more complex, consider using one of the other 2 option above

NB: On the form you've called the variable name, on the pae receiving it, you've called it personsName - note the mapping from one to the other in the URL.

Javascript solution (tested):

<html>
    <head>
        <title>SO Demo</title>
    </head>
    <body>

    <script type="text/javascript">
        function UpdateFormAction(){
            var form = document.getElementById('MyForm');
            var list = document.getElementById('PageList');
            var desiredAction = list.options[list.selectedIndex].value
            form.action = desiredAction;
            document.getElementById('Target').innerHTML = 'Form action set to: ' + desiredAction;
        }
    </script>

    <form id="MyForm" action="letter.php" method="POST">
        Name: <input type="text" name="name" /><br />
        Page: <select id="PageList" name="letter" onchange="UpdateFormAction();">
            <option value="letter.php">Page 1</option>
            <option value="letter2.php">Page 2</option>
            <option value="letter3.php">Page 3</option>
        </select>
        <input type="submit" value="Print" /><br/>
        <span id="Target">Form action unmodified</span>
    </form>
    </body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消