I would like to make a form with two selects ($a and $b) and a submit button which takes them to a link 'http://www.mywebsite.com/$a$b' when they click submit
for exemple if they chose in first select 'grade' and second '10' and then clicked submit it will take them to 'http://www.mywebsite.com/grade10'
That's my index.php , i didn't do it yet because i don't know how :P
<?php
error_reporting(E_ERROR | E_PARSE);
$a = $_POST['a'];
$b= $_POST['b'];
echo $a, $b;
?>
<form action="index.php" method="post">
<select name="a" title="a" id="a" >
<option> </option>
<option>a</option>
</select>
<select name="b" title="b" id="b" >
<option> </option>
<option>b</option>
</select>
<input type="submit" value="submit" />
</form>
Thank you :)
You don't technically need PHP for this, so you can cut out the middle-man of the POST request back to your server-side code and just go directly to the destination page via JavaScript. Maybe something like this:
<form action="index.php" method="post" id="redirector">
<select name="a" title="a" id="a" >
<option> </option>
<option>a</option>
</select>
<select name="b" title="b" id="b" >
<option> </option>
<option>b</option>
</select>
<input type="button" value="submit" />
</form>
<script type="text/javascript">
document.getElementById('redirector').onsubmit = function () {
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
window.location.href = 'http://www.mywebsite.com/' + a + b;
return false;
};
</script>
If they have JavaScript disabled, the form will still post back to the page, in which case the PHP code can still catch them and perform the redirect as well:
$a = $_POST["a"];
$b = $_POST["b"];
header("Location: http://www.mywebsite.com/" . $a . $a);
You'll probably want to add some error-checking logic to the values both in the JavaScript version and in the PHP version, in case users don't select something (if there's an empty option in the select, for example) or if a malicious user tries to post a bad value. Technically in the PHP version you're echoing back user input to the response, so you should look into things like cross-site scripting attacks and such. I'm not an expert on how to sanitize values like that in PHP, but it's worth researching.