【问题标题】:php - sending 2 forms at once to two different pages [duplicate]php - 一次将 2 个表单发送到两个不同的页面 [重复]
【发布时间】:2013-03-02 13:59:49
【问题描述】:

我同时发送两个表单,但我的脚本中有错误。我研究了错误 - Uncaught TypeError: Property 'submit' of object # is not a function。他们告诉我添加一个提交按钮,但这里已经有一个提交按钮是我的表单

<form name="form1" id="form1" method="post" action="page1.php">
    <input type="hidden" name="pt" id="pt" value="<?=$b64string?>" style="width:800px; padding: 10px;">
    <input type="submit" value="submit" name="submit"/>
</form>
<form name="form2" id="form2" method="post" action="page2.php">
    <input type="hidden" name="signature" value="<?=$_sign?>"/>

    <input type="submit" value="submit" name="submit">
</form>
<span onclick="submitBoth();" style="cursor:pointer;"><img src="image1.png"></span>

这是我的 jquery:

jQuery.post("form2.php", jQuery("#form2").serialize(), function(data){
    if(data.trim()=='valid'){
        document.form1.submit();
    }
});

【问题讨论】:

  • 表单元素需要在表单内部才能被视为表单的一部分。我从未尝试过,但尝试在每个表单中放置一个隐藏的提交。
  • 使用$("#form1").trigger('submit'); 而不是document.form1.submit();
  • 嗨@RickCalder 它不起作用我尝试添加&lt;input type="submit" value="submit" name="submit"/&gt; form2 提交但表单没有我之后没有收到任何错误。
  • 嗨@NB尝试使用您的 form1 未提交。我也没有收到任何错误。
  • 我编辑了我的标题,它应该发送到两页。

标签: php javascript jquery


【解决方案1】:

您使用“form2”作为提交 URL,对吗?

jQuery.post("form2", jQuery("#form2").serialize() ... etc

jqery.post 的第一个参数需要是一个 url。

【讨论】:

  • 对不起,这是一个错字,我在把它放在这里之前编辑了它。应该是form2.php
  • 那么为什么不像这样使用 smth 呢? jQuery.post("form2.php", jQuery("#form2").serialize(), function(data){ // you sure, that response will be a string, right? if(data.trim()=='valid'){ jQuery.post("form1.php", jQuery("#form1").serialize(), function(data){ // todo: postprocess form1.php responce }); } });
  • 我已经尝试过一段时间,但其他服务器有限制,我认为他们不接受来自其他站点的 ajax 帖子。我将表单传递给不同的页面和不同的主机。
  • 但也感谢您的帮助!
  • 在跨域请求的情况下,下面的帖子可能很有用:您应该遵循不同的模式。您的本地 JS 将对本地 URL 进行 ajax 发布,该 URL 将接受带有您的 json 数据的 POST 方法。此时,您的服务器代码将使用适当的数据向远程服务器执行 HTTP POST,获取响应,并将其发送回调用 js。见stackoverflow.com/questions/6761982/…
【解决方案2】:

那为什么不就这样使用 smth 呢?

jQuery.post("form2.php", jQuery("#form2").serialize(), function(data){
   // you sure, that response will be a string, right?
    if(data.trim()=='valid'){
        jQuery.post("form1.php", jQuery("#form1").serialize(), function(data){
     // todo: postprocess form1.php responce
    });
    }
});

抱歉之前的评论标记

【讨论】:

    【解决方案3】:

    我使用 N.B 的想法完成了这项工作,但我通过触发提交按钮 jQuery('#submit1').trigger('click'); 进行了更改,谢谢!

    【讨论】:

      【解决方案4】:

      你可以用这个

      function submitBoth(){
          $.post('form1.php',$('form[name=form1]').serializeArray(),function(){
                $('form[name=form2]').submit();
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-21
        • 2016-03-26
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2016-12-01
        • 1970-01-01
        相关资源
        最近更新 更多