Jump to content

Recommended Posts

Hi all...

I'm having a bit of a hard time with something that seems like it should be simple. Here goes:

I would like to use a form to have a user input a zip code that would (upon clicking submit) be inserted into the middle of a URL that has constants on both sides and then they should be directed to that URL.

I've got definitions for both sides of the URL set as "const1 and const2"...

I've got my form, the action on which I've only gotten close with

ACTION="$const1$userzip$const2" METHOD="post"

The field in the form is named "userzip"...

So the current result is to direct me to the new url minus the userzip.

make sense?

Here's the code:

[code]
// driving direction link
$const1 = "http://maps.google.com/maps?saddr=";
$const2 = "&daddr=27%20Main%20St%20+90210&hl=en";
echo "<table align='center' style='background-color:#eeeeee;'>
<tr>
                    <td align='center'>Need Directions?<br>Just put your zip code in the box below.</p></td>
</tr>
<tr>
<td align='center'>
<form name='form' action='$const1$userzip$const2' method='post'>
  <input type='text' size='11' name='userzip' value='Your Zip Code'>
  <input type='submit' name='submit' value='Get Directions'>
</form></td>
</tr>
</table>";
[/code]

Thanks, in advance!
Link to comment
https://forums.phpfreaks.com/topic/31011-solved-form-submit-value-go-to-url/
Share on other sites

for this, you can either post back to the form, read the variables in and then use header("location: $newurl");

or you can have the form use an onclick in javascript - then read the field and assemble the URL and then send you there.
[quote author=drifter link=topic=119011.msg486803#msg486803 date=1166388415]
for this, you can either post back to the form, read the variables in and then use header("location: $newurl");

or you can have the form use an onclick in javascript - then read the field and assemble the URL and then send you there.

[/quote]

Post back to the form? I'm not sure what you mean.

I would definitely like to stay away from javascript. If a user doesn't have it turned on, that puts the screws to that...
Hmmmm.. I was actually just about to post this question.  I think I could manage to do it using js, but it would be better to stay away from that if possible.  but can you do it w/o javascript, and without refreshing/redirecting the page prior to the end page link?
on a page called redirect1.php
[code]
<table align='center' style='background-color:#eeeeee;'>
<tr>
                    <td align='center'>Need Directions?<br>Just put your zip code in the box below.</p></td>
</tr>
<tr>
<td align='center'>
<form name='form' action='redirect2.php' method='post'>
  <input type='text' size='11' name='userzip' value='Your Zip Code'>
  <input type='submit' name='submit' value='Get Directions'>
<input type="hidden" value="http://maps.google.com/maps?saddr=" name="const1">
<input type="hidden" value="&daddr=27%20Main%20St%20+90210&hl=en" name="const2">
</form></td>
</tr>
</table>
[/code]
and then on a page called redirect2.php

[code]
<?php
ob_start();
$zip = $_POST['userzip'];
$const1 = $_POST['const1'];
$const2 = $_POST['const2'];
header("Location: ".$const1."".$zip."".$const2);
ob_end_flush()
?>
[/code]
Well, if you are going to redirect, i would just leave javascript out of redirect2.php and just go with a simple meta refresh:

redirect2.php
[code]<?
$zip = $_POST['userzip'];
$const1 = $_POST['const1'];
$const2 = $_POST['const2'];
echo "<meta http-equiv=\"refresh\" content=\"0;url=$const1$zip$const2\">";
?>[/code]
Excellent.... Thanks to both of you (JP128 & Submerged)!

JP128: I used your advice for two files. The first file with the form and the hidden values was perfect. In fact, I was thinking about hidden values but am not that proficient yet to know exactly what to do with them. The second file kept giving me a parse error.

Submerged: I used your meta-refresh in the second file and it worked perfectly.

There's our solution. The JP128/Submerged method.

Thanks, again!
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.