简体   繁体   中英

how to save the values of two forms with one submit button in php

hi i have a three forms and one submit button which is located outside of all three forms and i want to save the values of all the three forms using php into mysql database can anyone help me in showing that how can it be done i am using jquery to submit the form values here is my jquery

  <script>
    $(document).ready(function() {
        $("#submit").click(function(event) {
              alert('send 1st form');
            $.post($("#form1").attr("action"), $("#form1").serialize(),
              function() {
              alert('send 2nd form');
            $.post($("#form2").attr("action"), $("#form2").serialize(),
              function() {
              alert('send 3th form');
                $.post($("#form3").attr("action"), $("#form3").serialize(),
                  function() {
                    alert('Both forms submitted');
                  });
              });
          });
          event.preventDefault();
      });

    });
    </script>

as i posted a query before this helped me in submitting the forms and now i want to save the values into my database

her is my php code

if(isset($_POST["submit"])){

    $gen1 = mysql_query("SELECT MAX(accode) AS maxcode FROM quotmain LIMIT 0,1 ") or die(mysql_error());
    if (mysql_num_rows($gen1) > 0) {
        $vvouch = mysql_fetch_assoc($gen1);
        $qvouch = $vvouch["maxcode"] + 1;
    } else{
        $qvouch = '1';
    }

    $a = 1;
    $vouchdt  = mydt($_POST["vouchdt"]); 
    $name     = $_POST["cmbparty"];
    $refno    = $_POST["refno"];
    $attn     = $_POST["attn"];
    $subj     = $_POST["subject"];
    $msg      = $_POST["messsage"];
    $rem      = $_POST["rem"];
    $count    = $_POST["items"];
    $h        = $_POST["h"]; 
    $status   = $_POST["cmbstatus"];

    mysql_query("DELETE FROM quotdtl WHERE vouchno='$qvouch'") or die(mysql_error());

    for ($i = 1; $i <= $count; $i++) {
        $ord     = $_POST['ord_' . $i];
        $srno    = $_POST['srno_' . $i];
        $descrip = $_POST['descrip_' . $i];
        $unit    = $_POST['unit_' . $i];
        $rate = $_POST['rate_' . $i];

        mysql_query("INSERT INTO `quotdtl`(vouchno, orderby, srno, `descrip`, unit, rate)
                            VALUES('$qvouch', '$ord', '$srno', '$descrip', '$unit', '$rate')") or die(mysql_error());
    }

     mysql_query("DELETE FROM quotnotes WHERE vouchno='$qvouch'") or die(mysql_error());

    for ($i = 1; $i <= $h; $i++) {
        $notes = $_POST['notes' . $i];

        mysql_query("INSERT INTO `quotnotes`(vouchno, `notes`)
                            VALUES('$qvouch', '$notes')") or die(mysql_error());
    }   

    if ($addflag == 1) {
        mysql_query("INSERT INTO `quotmain`(vouchno, `vouchdt`, `name`, refno, `attn`, `subject`, `message`, `rem1`, `status`, `username`)
                            VALUES($qvouch, '$vouchdt', '$name', '$refno', '$attn', '$subj', '$msg', '$rem', '$status','$_SESSION[username]')") or die(mysql_error());

        insmess();
        header("refresh: 1; quotation.php?mode=true");
     } else {
            $mvouch = $_POST["vouch"];
             mysql_query("UPDATE `quotmain` SET `vouchdt`='$vouchdt', `name`='$name', refno='$refno', `attn`='$attn', `subject`='$subj',
                                 `message`='$msg', `rem1`='$rem', `username`='$_SESSION[username]' WHERE vouchno = '$mvouch' ") or die(mysql_error());
             updmess();
             header("refresh: 1; menu.php?action=quotation");
     }

}

Short answer: you cant. Your browser will always only send one form.

Long answer: You can create an equivalent for each input element of one form in the other form () and, onSubmit, set the value of the hidden fields to those of the non-hidden ones:

onsumbmit="$('#foobar_hidden').val( $('#foobar').val() ); return 1";

Try changing the HTML for your submit button to be

<button type="button" id="submit">Submit</button>

This will stop your browser from trying to send a single form when the submit button is hit, and will only do what you put in the JavaScript.

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