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
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):
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!!
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?
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.
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?
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. Ohh and I'm using this as the Ajax scripts: http://www.prototypejs.org/
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):
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):
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?