javascript - How to submit a form and then, redirect the user to a different page, previously selected on the form?
I'm trying to find a way to submit a form
and then, redirect the user to a different page, previously selected on the form
. How can I do it? (I'm using PHP to execute the form)
This is my current code.
<select data-select-name id="select-45ba" name="select" class="u-border-1 u-border-grey-30 u-input u-input-rectangle u-radius-50 u-white u-input-15">
<option value="QTD">QTD</option>
<option value="page.com">1 - R$32</option>
<option value="page.com">2 - R$64</option>
<option value="page.com">3 - R$96</option>
<option value="page.com">4 - R$128</option>
<option value="page.com">5 - R$160</option>
</select>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('select[data-select-name]').onchange = changeEventHandler;
}, false);
function changeEventHandler(event) {
window.location.href = this.options[this.selectedIndex].value;
}
</script>
Answer
Solution:
Your PHP code will look like this:
header( $header, $replace, $http_response_code );
See
https://code.tutsplus.com/tutorials/how-to-redirect-with-php--cms-34680
However, this is only needed if you absolutely want to send a request to the server after all for some reason before redirecting. An alternative approach would be to redirect from the client-side, by adding the
onchange="yourfunction(this);"
property to your select tag and implement it like
function yourfunction(element) {
window.location.href = element.value;
}
Source