Reputation: 53
I am trying to create a form which, when submitted, redirects the user to a specific URL which contains the content of the submission at the end of the URL.
So, for example, a simple form like this:
<form method="post">
<input type="text" name="tracking">
<input type="submit" name="submit" value="submit">
</form>
When the user types "abc" as the tracking number and clicks 'submit' they would be redirected to:
https://www.specificurl.com/abc
My question is, is this possible and if so, how can it be done?
This is what I have so far...
On the form page:
<form action="redirect_form.php" id="#form" method="post" name="#form">
<label>Enter your tracking code:</label>
<input id="tracking" name="tracking" placeholder='Enter your tracking code' type='text'>
<input id='btn' name="submit" type='submit' value='Submit'>
<?php include "include/redirect.php"; ?>
</form>
included in the redirect.php file:
<?php
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$name = $_POST['tracking'];
if($tracking)
{
// To redirect form on a particular page
header("Location:https://specificurl.com/$tracking");
}
else{
?><span><?php echo "Please enter tracking number.";?></span> <?php
}
}
?>
Upvotes: 0
Views: 32
Reputation: 809
Probably JavaScript will be enough here:
<input type="text" id="tracking" name="tracking">
<input type="button" name="submit" value="submit" onclick="window.location.replace('https://www.specificurl.com/'+tracking.value);">
Upvotes: 1
Reputation: 11
you should use the GET method for this. With javascript you can then extract the text input and manipulate the url to which the user is redirected to. Hope that helps you further.
Upvotes: 0