OK, now when you submit the form it will get validated by js and redirected.
There's a fall back with PHP as well if you want it.
PHP Code:
<?php
error_reporting (E_ALL ^ E_NOTICE);
if ($_POST['redirect']) { if (isset($_POST['merchant']) && !empty($_POST['merchant'])) { $directTo = $_POST['merchant']; header("location:".$directTo); } else { echo "Sorry, you did not make a selection."; } }
?>
<html> <head> <script> function validate(form) { if (document.form.merchant.value == "") { alert("You did not make a selection"); return false; } var directTo = document.form.merchant.value; window.location=directTo; } </script>
That is helpful, thanks, but it doesn't quite work, yet.
I copy pasted this
Quote:
<html>
<head>
<script>
function validate(form) {
if (document.form.merchant.value == "") {
alert("You did not make a selection");
return false;
}
var directTo = document.form.merchant.value;
window.location=directTo;
}
</script>
in an empty file and saved it. Opened the file with my browser (Firefox). Selected amazon. Clicked submit, and what it does is reset the selected option to "Please select" i.e. the default, rather than redirect to amazon.com
The "You did not make a selection" functionality, however, works.
Aaarrrggghhh... I HATE JAVASCRIPT!
I don't know why the javascript isn't working, I've checked on various sources and it appears to be the right syntax.
The PHP works, I can guarantee that. As not everyone will have javascript enabled, it would be wise to have the PHP fall back anyway.
I tried a load of things after posting that. I too realised it only worked outside of a function. I find it weird and incredibly frustrating, but javascript is my worst enemy at the best of times!
and then they don't even need to press the submit button (so you could hide the button if JavaScript is enabled but leave it there so that the value can be submitted to a server side script to do the redirect when JavaScript is disabled).
function validate(){
var selO = document.getElementById('merchant');
if(selO.selectedIndex == 0){
alert('You have not made a selection');
} else {
window.location.href = selO.value;
}
return false;
}
function validate(){
var selO = document.getElementById('merchant');
if(selO.selectedIndex == 0){
alert('You have not made a selection');
} else {
window.location.href = selO.value;
}
return false;
}
Thanks, I tried with and w/o return false, and there is no improvement.
and then they don't even need to press the submit button (so you could hide the button if JavaScript is enabled but leave it there so that the value can be submitted to a server side script to do the redirect when JavaScript is disabled).