I want to make a form and redirect user to a specific url mentionned in the select value.
This is my form :
<form>
<select name="url" >
<option value="http://www.google.com/">Google</option>
<option value="http://www.bing.com/">Bing</option>
</select>
<input name="name" type="text" placeholder="Name" />
<input name="age" type="text" placeholder="Age" />
<button type="submit">Go</button>
</form>
For example, if the user click on "Go" when "Page2" is selected, he will be redirected to page "http://www.example.com/page2" ("Name" and "Age" values will be transferred to this page).
Just like this code :
<select name="jumpit" onchange="document.location.href=this.value">
<option selected value="">Make a Selection</option>
<option value="http://www.google.com/">Google</option>
<option value="http://www.bing.com">Bing</option>
</select>
except that i need the redirection to be done after the submit button click not onchange select.
That's all. Thanks !
Here you are, also it was tested and working:
HTML
<form action="some_file.php" method="post">
<select name="url">
<option value="http://www.google.com/">Google</option>
<option value="http://www.bing.com/">Bing</option>
</select>
<input name="name" type="text" placeholder="Name" />
<input name="age" type="text" placeholder="Age" />
<button type="submit">Go</button>
</form>
some_file.php
if (isset($_POST['url'])) {
$url = $_POST['url']."?name=".urlencode($_POST['name'])."&age=".urlencode($_POST['age']);
header("Location: ".$url);
exit;
}
Then you can access them with GET
, ie. $_GET['name']
will be the same, as you entered into name
input field.
You can try following code, it will help you :-
Change submit button with these :-
<button type="submit" name="submit">Go</button>
Add following code where your form submit :-
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$age = $_POST['age'];
$url = $_POST['url']."?name=".$name."&age=".$age;
header("Location: ".$url);
}
You can access both value using $_GET in next page.
Try this code:
<form>
<select name="url" id="url" >
<option value="http://www.example.com/page1">Page1</option>
<option value="http://www.example.com/page2">Page2</option>
<option value="http://www.example.com/page3">Page3</option>
</select>
<input name="name" id="name" type="text" placeholder="Name" />
<input name="age" id="age" type="text" placeholder="Age" />
<input type="button" onClick="submitForm()" />
</form>
<script>
function submitForm()
{
var url = $('#url').val();
var name = $('#name').val();
var age = $('#age').val();
var urlToSend = url + "?name=" + name + "&age="+age;
location.replace(urlToSend);
}
</script>