【问题标题】:Post data from one form to 2 different url将数据从一个表单发布到 2 个不同的 url
【发布时间】:2014-07-21 11:27:24
【问题描述】:

我如何将帖子从一个表单发送到 2 个不同的网址

喜欢同时向process.php和confirm.php发送POST名称

$(document).ready(function(){
    $("#myform").validate({
        debug: false,
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            name: "Please let us know who you are.",
            email: "A valid email will help us get in touch with you.",
        },
        submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('process.php', $("#myform").serialize(),     function(data) {
                $('#results').html(data);
            });
        }
    });
});`    

还有我的表格

<form name="myform" id="myform" action="confirm.php" method="POST"> 

<label for="name" id="name_label">Name</label>  
<input type="text" name="name" id="name" size="30" value=""/>  
<br>
<label for="email" id="email_label">Email</label>  
<input type="text" name="email" id="email" size="30" value=""/> 
<br>
<input type="submit" name="submit" value="Submit"> 

感谢您的帮助

【问题讨论】:

  • 只有在取消表单的正常提交并通过 AJAX 发出这两个请求时才能这样做。但无论如何,在大多数情况下,当人们提出这样的问题时,他们已经走错了路 - 所以请先在此处描述您真正想要实现的目标。

标签: javascript php ajax arrays forms


【解决方案1】:

好吧,如果我理解的话,您想一次将输入名称值从表单发送到两个不同的 PHP 文件? 您可以使用 jquery 来做到这一点:

提供一个 ID 提交按钮

<input type="submit" name="submit" id="send" value="Submit"> 

$(document).ready(function(){
     //When the user click on Submit, the post starts

     $("#send").click(function(){
     //get the input field name value
     var vNAME = $("#name").val();

     //send it first to process.php
     $.ajax({
      type: "post",
      url: "process.php",
      data: {name: vNAME},
      dataType: "json",
      success: function(data){
          //if process.php receive the post data and process right, it returns
          //a message of success otherwise, return a error message.
          if(data.success=="true"){
             ... successful message! 
          }else{
             ... unsuccessful message! 
          }              
      }
     });

     //sending to confirm.php
     $.ajax({
      type: "post",
      url: "confirm.php",
      data: {name: vNAME},
      dataType: "json",
      success: function(data){
          //if process.php receive the post data and process right, it returns
          //a message of success otherwise, return a error message.
          if(data.success=="true"){
             ... successful message! 
          }else{
             ... unsuccessful message! 
          }              
      }
     });
  });
});

【讨论】:

    猜你喜欢
    • 2017-12-17
    • 2019-05-26
    • 2016-01-31
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    相关资源
    最近更新 更多