将数据从一个表单发布到2个不同的URL

时间:2014-06-01 01:33:08

标签: javascript php ajax arrays forms

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

喜欢在同一时间将POST名称发送到process.php和confirm.php

$(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"> 

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

好吧,如果我明白了,你想要将表单中的输入名称值一次性发送到两个不同的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! 
          }              
      }
     });
  });
});