How To ? Form Post to Multiple Locations

前端 未结 4 659
猫巷女王i
猫巷女王i 2020-12-01 15:23

I have a form which I need to POST to multiple scripts. How can I do this the simplest way?

I know it could be done with Javascript, Curl or Snoopy class, but really

4条回答
  •  悲&欢浪女
    2020-12-01 15:48

    The easiest way to do this is to use jQuery to send an $.ajax (or $.post or $.get) to each script, retrieving the result from each of them and doing what you will with the results.

    $(document).ready( function(){
        $('#mySubmitButton').click(function(){
            //Send data to the email script
            $.post( 'send-email.php', $('form').serialize(), function(data, textStatus) {
                //data is the result from the script
                alert(data);
            });
    
            //Send data to the other script
            $.post( 'my-other-script.php', $('form').serialize(), function(data, textStatus) {
                //data is the result from the script
                alert(data);
            });
        });
    });
    

    update: The serialize command is the data that is being sent. Take a look at the jQuery serialize function. It basically just takes the various inputs, selects, textareas, checkboxes, etc in your form, and puts them into a string like this:

    myNameInput=john&active=on&whateverSelected=3

    It's just a string of your form element names and their values. That is what is sent to the external script via the ajax command.

    A side note, when doing serialize, make sure that all your form elements have a name attribute, not just an id. The serialize doesn't pay any attention to their id's. Only their name.

提交回复
热议问题