Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an html form with 2 fields: name and email. I need to submit the form values to 2 different urls from the same submit button. I have tried using jquery, serialize and post but I am not clear how to accomplish this. I cannot use PHP in this instance.

<!doctype>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

</head>
<body>

<form action="" method="post" name="myform" id="myform">

<input type="text" value="name" />
<input type="email" value="email" />
<input type="button" id="submit" value="submit" />
</form>

<script type="text/javascript">
$(document).ready(function(){
    var url1 = "some-url.com";
    var url2 = "some-other-url";
   $("#submit").click(function() {
        $.post("url1", $("form#myform").serialize());
    $.post("url2", $('formmy#myform').serialize());
    })
})
</script>
</body>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
241 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you're using jquery:

$( "form" ).submit(function( event ) {
  submitToURL1();
  submitToURL2();
  //something else
  alert( "Handler for .submit() called." );
  event.preventDefault(); //so that it doesn't get suimitted again
});

submitToURL1 could be something like:

$.post("url1", data : $("form").serialize());

.serialize() is a jquery function that takes the elements in a form and arranges them nicely so you can send them as params to a post method, for instance. https://api.jquery.com/serialize/

More information here: https://api.jquery.com/submit/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...