Jump to content

PHP Form redirect


cchhita

Recommended Posts

Hi There,

I have a problem with a form redirect I am trying to achieve.

I am trying to redirect a user to a certain page depending on what they select from a dropdown menu.  This seems quite straight forward, but I am trying to past a value that the user enters into the page as well, this is where I am having the problem.

 

Here is my HTML Form:


<form action="jump.php" method="post">
  <p>
  <!-- Zip Code -->
  ZIP code:
  <input type="text" size="5" name="zip" id="zip" onBlur="updateCityState();"/>
<select name=url>
<option value="http://www.mydomain.com/forms/form1.php">Option1</option> 
<option value="http://www.mydomain.com/forms/form2.php">Option2</option> 
<option value="http://www.mydomain.com/forms/form3.php">Option3</option> 
</select>
<Input Name="submitButton" Value="Submit" Type="Submit"/></p>
</form>

 

Jump.php code

 

<?php
$sZipCode = $_POST['zip'];
$url = $_POST["url"];
header("Location: $url");
?> 

 

I have tested the code, and I am able to send the Zip Code from the HTML form to the Jump.php page without any problems.  But when the jump.php executes, it does not carry the Zip Code Value to the page, but the redirection works correctly and goes to the right page.

 

Can someone please tell me where the error in the code is?

Or am I missing something?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/133612-php-form-redirect/
Share on other sites

<select name=url>
<option value="http://www.mydomain.com/forms/form1.php">Option1</option> 
<option value="http://www.mydomain.com/forms/form2.php">Option2</option> 
<option value="http://www.mydomain.com/forms/form3.php">Option3</option> 
</select>

 

Try rewriting this as:

<select>
<option name=url value="http://www.mydomain.com/forms/form1.php">Option1</option> 
<option name=url value="http://www.mydomain.com/forms/form2.php">Option2</option> 
<option name=url value="http://www.mydomain.com/forms/form3.php">Option3</option> 
</select>

 

Im really not sure if this will make a diffrence but I am somewhat curios about it. :P

Link to comment
https://forums.phpfreaks.com/topic/133612-php-form-redirect/#findComment-695326
Share on other sites

As far as I am aware, you aren't able to pass $_POST arguments between pages that aren't directly in use for the form. The easiest way would be as mentioned above. Add it to the url and access it via $_GET later on. You can do this like this:

 

$sZipCode = $_POST['zip']; 
$url = $_POST['url']; 
$parsedUrl = $url.'?zip='.$sZipCode; 
header("Location: $url");

 

You can then access it on the next page like this:

$zip = $_GET['zip']; 

Link to comment
https://forums.phpfreaks.com/topic/133612-php-form-redirect/#findComment-695387
Share on other sites

Hi There,

Thanks for the input everyone!

Xangelo, I have taken your code and make one little change. 

 

Here it is:

$sZipCode = $_POST['zip']; 
$url = $_POST['url']; 
$parsedUrl = $url.'?zip='.$sZipCode; 
header("Location: $parsedUrl");

 

When I tried just URL, it would not work, when I plugged in $parsedURL I got the result I wanted.

 

I tried to extend this example, but not am having a problem.

I want to pass 3 variables to the page, i.e. ZIp, City and State.

 

I thought I could do this simply by concatenating the extra variables from my first form into the jump form, and passing those variables as a GET into my third form, but this does not work.

 

Here is my code now:

 

<?php

$sZipCode = $_POST['zip']; 
$sCity = $_POST['city']; 
$sState = $_POST['state']; 
$url = $_POST['url']; 
$parsedUrl = $url.'?zip='.$sZipCode.'?city='.$sCity.'?state='.$sState; 
header("Location: $parsedUrl");
?> 

 

When I pass these values to my form, in the Zipcode box, I get all the variables in one String, i.e. 60601?city= Chicago?state= Illinois

 

How do I pass the variables to the correct boxes in my form?

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/133612-php-form-redirect/#findComment-695531
Share on other sites

Sorry about that cchhita.. I meant to put down $parsedUrl, but I was just a little distracted at the time I suppose.

 

You're on the right way to adding more variables, but you need to alter it ever so slightly:

<?php

$sZipCode = $_POST['zip']; 
$sCity = $_POST['city']; 
$sState = $_POST['state']; 
$url = $_POST['url']; 
$parsedUrl = $url.'?zip='.$sZipCode.'&city='.$sCity.'&state='.$sState; 
header("Location: $parsedUrl");
?> 

That should work fine for you. Only one questionmark should appear in the url, adding more variables means they are separated by an ampersand (&).

Link to comment
https://forums.phpfreaks.com/topic/133612-php-form-redirect/#findComment-695539
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.