简体   繁体   中英

Form firing javascript onsubmit but not passing values from form POST

I am using the form action POST to submit a form and javascript onsubmit to redirect a user to the thank you page. The POST was working prior to adding the javascript code, and once that was implemented the form is redirecting correctly but not calling up the POST page to process the values anymore.

<form action="https://www.website.com/leads.php" method="post" id="bm-form" class="form-style" novalidate="novalidate">
    <!--form fields here-->
<div>
    <input class="btn theme-btn" value="Process" type="submit">
</div>
</form>

Javascript

<script type="text/javascript">
window.onload=function() {
  document.getElementById("bm-form").onsubmit=function() {
    window.location.replace("https://www.website.com/thank-you.html");
    return false;
  }
}
</script>

The JavaScript is cancelling the normal behaviour of the form submission and telling the browser to load a different page instead.

To redirect to a thank-you page, have https://www.website.com/leads.php return a 302 status and a Location HTTP response header. Don't involve JavaScript.

You should do the redirect from inside the php code (leads.php) server side not from javascript. The javascript logic you have added it overwrites the form submit and post is never done, just the redirect is.

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