is it there any way to post a form to more URLs?
I need it in javascript..
For example: I've got 2 documents: 1.php and 2.php.. I want the form to post data to the both of these files, but go to the 1.php..
Do you know any way?
问题:
回答1:
the quick and dirty way to do this is to post the form to one script which will in turn post it to a second script.
回答2:
You need a form with the action "1.php" and jQuery.
$('#form_id').submit(function(event){
event.preventDefault();
var $th = $(this);
$.ajax({
type: 'post',
url: '2.php', // note: edited by Ryan
data: $th.serialize(),
success: function(){
$th.submit();
}
});
});
回答3:
Using JavaScript, yes
You'll want to make one call using the XMLHttpRequest function available in JavaScript to make an AJAX call to the php.2 script. The second call can then be done using document.forms["FORMNAME"].submit()
.
Libraries like jQuery's Ajax functions make it easier to use the XMLHttpRequest function. See the jquery.get(), jquery.post(), and jquery.ajax() (a.k.a $.get(), $.post() and $.ajax()) function documentation
Here are a few guides to doing AJAX calls using jQuery's $.post() and $.ajax() functions.
- (http://www.sitepoint.com/ajax-jquery-3/)
- (http://www.devirtuoso.com/2009/07/beginners-guide-to-using-ajax-with-jquery/)
- (http://www.phpfreaks.com/forums/index.php?topic=256503.0)
- (http://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/)
Also here is an example framework of how this can be coded up:
<form id="exampleform" name="exampleform" action="1.php"> ... </form> <script type="text/javascript"> function submitForm() { ... // set up vars for $.post() call // Make the post call for the 2.php $.post( ... ); // Submit the form for 1.php document.forms["exampleform"].submit(); } </script>
回答4:
All above solution should work, but remember ajax is only alowed in same domain posting. This mean you are on http://example.com, you can only post to example.com/etc or sub.example.com
If you really want to cross domain posting, you should create an empty ifram as target (id="iframeid") then change the target of the form to it:
<form target="_iframeid" action=""/>
After that you can use javascript to post to one page, and then change action url and repost again