How do I post one form two two sites??

Discussion in 'JavaScript' started by TwisterMc, Jan 30, 2007.

  1. #1
    I'm looking for a good script or tutorial that will show me how to post form data to two sites.

    I'd like Ajax/JavaScript so it's platform independent.

    If you need more details, please ask.

    Thanks
     
    TwisterMc, Jan 30, 2007 IP
  2. pbmods

    pbmods Guest

    Messages:
    18
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Have your form make two function calls:

    <form action="javascript:void('postOnce(\'http://site_a/\', this);postOnce(\'http://site_b/\', this);');">
    Code (markup):
    The postOnce function would then create an Ajax (or similar) object that would send your data to to page passed as a parameter.

    E.G.,

    function postOnce(s_thePage, e_theForm) {
    	var ajax_myRequest = new Ajax.Request(
    		s_thePage,
    		{ postBody: Form.Serialize(e_theForm) }
    	);
    }
    Code (markup):
     
    pbmods, Feb 5, 2007 IP
    TwisterMc likes this.
  3. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks :D

    I'm going to try that out tomorrow.
     
    TwisterMc, Feb 5, 2007 IP
  4. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank You! Thank You! Thank You!

    I had to fiddle with it for a while and then realized I needed some Ajax scripts also but I got it to work.

    This was a big hurdle in my way and I really appreciate the help!!
     
    TwisterMc, Feb 6, 2007 IP
  5. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    pbmods -

    The code works great for posting to two forms on a single site. Ie: thanks1.php and thanks2.php but it doesn't POST data to an external site like http://www.site.com/thanks2.php.

    Any ideas why that might be?
     
    TwisterMc, Feb 7, 2007 IP
  6. pbmods

    pbmods Guest

    Messages:
    18
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The two key options you need to set when you want to send POST data using Ajax.Request are:

    var ajax_sendForm = new Ajax.Request(
    	s_destinationURL,
    	{
    		method:		'post',
    		postBody:	Form.Serialize(e_thePfhorm)
    	}
    );
    Code (markup):
    The second parameter in Ajax.Request is a JS Object. The two key elements that you will want are method and postBody. [SIDEBAR: Note that you can send both GET and POST variables. Simply include your GET variables in the url (e.g., 'http://www.example.com/test.php?variable=value&etc')].

    Form.Serialize is implemented in prototype.js. It returns a string in 'a=b&c=d' format using all the inputs in the form element that you pass as the first parameter.
     
    pbmods, Feb 7, 2007 IP
  7. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This is what I have.
    
    <script src="prototype.js" type="text/javascript"></script>
    <script type="text/javascript">
    function postOnce(s_thePage, e_theForm) {
    	var ajax_myRequest = new Ajax.Request(
    		s_thePage,
    		{
    			method:	'post',
    			postBody:	Form.serialize(e_theForm)
    		}
    	);
    }
    </script>
    
    HTML:
    and for the form
    
    <form onSubmit="postOnce('http://www.othersite.com/test/thanks2.php', this);" action="contact_us_thanksSF.php" method="post">
    
    HTML:
    Do you see any issues?
     
    TwisterMc, Feb 8, 2007 IP
  8. pbmods

    pbmods Guest

    Messages:
    18
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Offhand, it looks fine to me. Does it work?
     
    pbmods, Feb 8, 2007 IP
  9. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The post page (http://...com/test/thanks2.php) doesn't email out anything. The only code on that page is a simple PHP mail script that is suppose to mail the contents of the form. I know it works because if I change the form action to go to the thanks2.php page it works fine. :eek:

    Ohh and I'm using this as the Ajax scripts: http://www.prototypejs.org/
     
    TwisterMc, Feb 8, 2007 IP
  10. pbmods

    pbmods Guest

    Messages:
    18
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I wonder what would happen if you called postOnce for each page, and used that as the action instead of trying to do the same thing two different ways.

    <form action="javascript:void('postOnce(\'http://site_a/\', this);postOnce(\'http://site_b/\', this);');">
    Code (markup):
     
    pbmods, Feb 9, 2007 IP
  11. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #11
    good solution "pbmods"
     
    rays, Feb 12, 2007 IP
  12. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Absolute paths do not work. Relative paths do.

    This does not work:
    <form onSubmit="javascript:void('postOnce(\'http://www.site2.com/thanks2.php\', this);postOnce(\'http://www.site1.com/contact_us_thanksSF.php\', this);');">
    
    Code (markup):
    This does not work:
    <form onSubmit="postOnce('http://www.site2.com/thanks2.php', this); postOnce('http://www.site1.com/contact_us_thanksSF.php', this);">
    
    Code (markup):
    This absolute path doesn't work, relative does even though they both point to the same thanks page:
    <form onSubmit="postOnce('http://www.site1.com/contact_us_thanksSF.php', this); postOnce('contact_us_thanksSF.php', this);">
    
    Code (markup):
     
    TwisterMc, Feb 12, 2007 IP
  13. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #13
    From what I understand in all my digging, Ajax only sends requests to internal URLs for security reasons. So I'm back to the drawing board.

    Anyone know how to use fopen to POST a form?
     
    TwisterMc, Feb 14, 2007 IP