简体   繁体   中英

How to redirect to a specific URL baed on the data submitted?

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

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);">

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM