...

View Full Version : Help with multiple submit calls.



Steve Garlick
12-08-2009, 06:44 AM
Hi , first post.

I'm trying to send data back to a database that's encoded in javascript objects on a page.

I've used this approach to fill in a pseudo form and submit the results to several php files to update the database. Everything works fine, done one at a time. When I call this type of function inside another loop to feed multiple objects to the database, only the first record is inserted. I'm wondering if there are latency issues with multiple submits?

function postwith (to,p) {
var myForm = document.createElement("form");
myForm.method="post" ;
myForm.action = to ;
for (var k in p) {
var myInput = document.createElement("input") ;
myInput.setAttribute("name", k) ;
myInput.setAttribute("value", p[k]);
myForm.appendChild(myInput) ;
}
document.body.appendChild(myForm) ;
myForm.submit() ;
document.body.removeChild(myForm) ;
};

Maybe I'm barking up the wrong tree. Is there an easier way to pipe arrays of javascript objects back into multiple database tables?

Regards,

Steve Garlick

Spudhead
12-08-2009, 12:56 PM
I'd suggest two things.

First, use Firebug to view exactly what's being sent over HTTP, to where. That might give you an idea of what, if anything, is happening on the server.

Second, what you're trying to do seems to be a convoluted way of mocking up an AJAX call; you'd be better off, it looks from here, dropping everything into AJAX. Since you've asked this in the javascript frameworks forum, here's how you might do it with one common JS framework, jQuery:


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.js"></script>
<script type="text/javascript">
var data = {
animal:"dog",
mineral:"salt",
vegetable:"jordan"
};
$(document).ready(function(){

$('#myform').submit(function(){
$.get('http://www.site.com/script1.php', data, function(rv){
console.log('Script 1 response:' + rv);
});
$.get('http://www.site.com/script2.php', data, function(rv){
console.log('Script 2 response:' + rv);
});
return false;
});

});
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum