Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用javascript提交两个表单,但失败_Php_Javascript_Ajax_Forms - Fatal编程技术网

Php 使用javascript提交两个表单,但失败

Php 使用javascript提交两个表单,但失败,php,javascript,ajax,forms,Php,Javascript,Ajax,Forms,我一直在尝试使用javascript提交两个表单,但失败了。我已经搜索了stackoverflow,但是没有一个javascript是有效的 我也试过这个 这是我的密码。。。我的整个页面很长,所以我刚刚在这里粘贴了两个表单代码。。。 表格1(regForm)的每个字段都是必填字段。。表格2(frmAdd)包含一段时间 环我想这两种形式是不同的。。。因此,使用javascript的任何解决方案或使用这些表单的任何替代方案 <script> submitForms = funct

我一直在尝试使用javascript提交两个表单,但失败了。我已经搜索了stackoverflow,但是没有一个javascript是有效的

我也试过这个

这是我的密码。。。我的整个页面很长,所以我刚刚在这里粘贴了两个表单代码。。。 表格1(regForm)的每个字段都是必填字段。。表格2(frmAdd)包含一段时间 环我想这两种形式是不同的。。。因此,使用javascript的任何解决方案或使用这些表单的任何替代方案

  <script>
  submitForms = function(){
    document.forms["regForm"].submit();
    document.forms["frmAdd"].submit();
}
  </script>

      <form action="userdata.php" method="post" name="regForm" id="regForm" >

      <h2><em>Personal Details : </em></h2></br>

        <table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
            <tr>
                <td width="22%">NAME<span class="required"><font color="#CC0000">*</font></span></td> 
                <td width="78%"> 
              <input name="full_name" type="text" id="full_name" size="40" value="<? echo $row_settings['full_name']; ?>" class="required"></td>
            </tr>
          <tr> 
               <td>FATHER'S NAME<span class="required"><font color="#CC0000">*</font></span></td> 
               <td> 
              <input name="f_name" type="text" id="f_name" size="40" value="<? echo $row_settings['f_name']; ?>" class="required"></td>
          </tr>
          <tr>
                <td>MOTHER'S NAME<span class="required"><font color="#CC0000">*</font></span></td> 
                <td> 
              <input name="m_name" type="text" id="m_name" size="40" value="<? echo $row_settings['m_name']; ?>" class="required"></td>
            </tr>
          <tr> 
               <td>NATIONALITY<span class="required"><font color="#CC0000">*</font></span></td> 
               <td> 
              <input name="nationality" type="text" id="nationality" size="40" value="<? echo $row_settings['nationality']; ?>" class="required"></td>
          </tr>
          <tr>
                <td>RELIGION<span class="required"><font color="#CC0000">*</font></span></td> 
                <td> 
              <input name="religion" type="text" id="religion" size="40" value="<? echo $row_settings['religion']; ?>" class="required"></td>
          </tr>
        </table></br>

        <p align="center">
            <!-- previous button   <input name="doSave" type="submit" id="doSave" value="Submit">  -->
        </p>
    </form> 

        <form action="userdata.php" name="frmAdd" method="post">

<table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
  <tr>
    <td width="5"> <div align="center">NO</div></td>
    <td width="91"> <div align="center">Employer's NAME</div></td>
    <td width="160"> <div align="center">COUNTRY</div></td>
    <td width="198"> <div align="center">POSITION</div></td>
    <td width="70"> <div align="center">FROM</div></td>
    <td width="70"> <div align="center">TO</div></td>
    <td width="70"> <div align="center">SALARY</div></td>
    <td width="70"> <div align="center">REASONS FOR LEAVING</div></td>
  </tr>

  <?php for($i=1;$i<=4;$i++) { ?>

  <tr>
    <th width="5"> <div align="center"><? echo $i . "."; ?></div></th>
    <td><input type="text" name="emp_name<?=$i;?>" size="25"></td>
    <td><input type="text" name="emp_country<?=$i;?>" size="10"></td>
    <td><input type="text" name="emp_pos<?=$i;?>" size="10"></td>
    <td><input type="text" name="emp_frm<?=$i;?>" size="5"></td>
    <td><input type="text" name="emp_to<?=$i;?>" size="5"></td>
    <td><input type="text" name="emp_sal<?=$i;?>" size="5"></td>
    <td><input type="text" name="emp_lev<?=$i;?>" size="25"></td>
  </tr>
  <?php } ?>
  </table>
  </br>
    <!-- previous button <input type="submit" name="doHis" value="Save"> -->
  <input type="hidden" name="hdlfrm" value="<?=$i;?>">

  <input type="button" value="Click Me!" onclick="submitForms()" />

submitForms=函数(){
document.forms[“regForm”].submit();
文件。表格[“frmAdd”]。提交();
}
个人资料:
姓名*
您不能在一页上提交两个不同的表格。你有比赛条件


如果您想同时提交两个表单,可以将它们合并并在服务器上处理,也可以使用Ajax提交它们

我认为最简单的方法是安装jQuery并使用
find
方法,然后
序列化整个内容,并使用jQuery
post
get
ajax
方法发送。

缺少“标记”,有些地方的代码不正确。
如果您只需将代码缩短到相关部分,然后将其发布,这将更加容易。
所以我不得不为你这么做

您应该测试以下代码。只要这不起作用,就不需要编写更多的“表单输入”

test.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript"> 
    submitForms = function(){
    document.forms["regForm"].submit();
    window.setTimeout(submitB,1000)
    } 

    function submitB() {
    document.forms["frmAdd"].submit();
    }
    </script>
    <title>Emergency Table - Update</title>
</head>

<body>
 <form action="writea.php" method="post" name="regForm" id="regForm" >
    <h2><em>Personal Details : </em></h2></br>
    <table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
        <tr>
            <td width="22%">NAME<span class="required"><font color="#CC0000">*</font></span></td> 
            <td width="78%"> 
             <input name="full_name" type="text" id="full_name" size="40" value="Romeo Delta" class="required"></td>
        </tr>
    </table></br>
 </form> 

 <form action="writeb.php" name="frmAdd" method="post">
  <table width="80%" border="0" cellpadding="3" cellspacing="3" class="forms">
  <tr>
    <td width="5"> <div align="center">TEST</div></td>
  </tr>
  </table>
  </br>
  <input type="hidden" name="hdlfrm" value="1000">
  <input type="button" value="Click Me!" onclick="submitForms()" />
 </form>

</body>
</html>

submitForms=函数(){
document.forms[“regForm”].submit();
window.setTimeout(submitB,1000)
} 
函数submitB(){
文件。表格[“frmAdd”]。提交();
}
紧急表格-更新
个人资料:
姓名*
试验
writea.php

<?php
  if (isset($_POST['full_name'])) { $hdl = $_POST['full_name']; } else { $hdl = "Err."; }
  $fp = fopen('dataa.txt', 'a+');
  fwrite($fp, "Hello from write A : ".$hdl."\r\n");
  fclose($fp);
?>

writeb.php

<?php
  if (isset($_POST['hdlfrm'])) { $hdl = $_POST['hdlfrm']; } else { $hdl = "Err."; }
  $fp = fopen('datab.txt', 'a+');
  fwrite($fp, "Hello from write B : ".$hdl."\r\n");
  fclose($fp);
?>

提交后,应删除dataa.txt和datab.txt的内容

dataa.txt
你好,来自write A:Romeo Delta

datab.txt

Hello from write B:1000

如果您在页面中执行普通表单post,第一次提交操作将完成并重定向到该页面,第二次表单post将不会完成。我的解决方案最好使用ajax post方法并提交这两个表单,希望这对您有所帮助。

找到了解决方案。。。使用此jquery和jquery表单js

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>

<script>

$(document).ready(function() { 

        $('#form1').ajaxForm(function() { 
        });
        $('#form2').ajaxForm(function() { 
                alert("Your Submitted Information has been Saved !");
                window.location = "userdata.php";    
                return true;

        });
        $("#submitEverything").click(function(){
        mySubmitFunction();
        return false;
});

}); 

function mySubmitFunction() {
        $("#form2").submit();
        $("#form1").submit();
}

</script>

$(文档).ready(函数(){
$('#form1').ajaxForm(函数(){
});
$('#form2').ajaxForm(函数(){
警报(“您提交的信息已保存!”);
window.location=“userdata.php”;
返回true;
});
$(“#submitEverything”)。单击(函数(){
mySubmitFunction();
返回false;
});
}); 
函数mySubmitFunction(){
$(“#表格2”).submit();
$(“#表格1”).submit();
}
userdata.php是粘贴此代码的页面
并且必须在表单中使用两种不同的操作(如form1 action=“page1”和form2 action=“page2”)


那我该怎么办@重新思考你在做什么。为什么这个方法不起作用?或document.getElementById('otherForm').submit();这些方法可以在一页上使用,对吗?你的两个都很好,我说我刚刚从我的大页面上复制粘贴了。。。但是现在修复了。。。你现在有什么解决办法吗@moskito-x:你看到我的答案了吗?