简体   繁体   中英

Form submit without going to the target page

I have question regarding form submit. I have this simple form on processFabrication.php to submit all the variables then process it to the database in another page called processExceededQty.php

Co, in processFabrication.php , I have

 echo "<form action='processExceededQty.php' method='post'>";

When I click submit it goes to the processExceededQty.php .

What I am aiming to do is,

  1. When user click submit, display confirmation yes/no with popup
  2. After user click yes with the confirmation window, stay in processFabrication.php but methods in processExceededQty.php is still executed.
  3. When user click no on the popup, go back and don't do form action

Any help would be greatly appreciated.

Something like this in your javascript :

function doSubmit(){

if (confirm('Are you sure you want to submit?')) {
    // yes
    return true;
} else {
    // Do nothing!
    return false
}
}

add the onsubmit to your form in html :

<form action='processExceededQty.php' method='post' onsubmit='doSumit()'>

Addition to @N0M3 answer. Include below script in your <head>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

and slight change in your function

function doSubmit(){

if (confirm('Are you sure you want to submit?')) {
    // yes
     $(this).submit(function(e){
        e.preventDefault();
     });

    jQuery.ajax({
        url : 'processExceededQty.php',
        data : {'username':username}, // where first 'username' is your field name, and second one is your field's value
        method: 'post',
        success: function(data) {
             // data is variable which has return data from `processExceededQty.php` 
             // do whatever you want with data
        }
    });    


} else {
    // Do nothing!
    return false;
}
}

Use function on the "onsubmit" of the form like below,

<form action='processExceededQty.php' method='post' onsubmit='reutrn show_pop_up()'>

And on script you ca write following

<script>
   function show_pop_up()
   {
      // show pop up
      if(op_up_return =="Yes")
          return true;
      else
          return false;
   }
</script>

Hope this will help.

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