jackpf Posted April 18, 2009 Share Posted April 18, 2009 Hi all, Just wondering if it's possible to send multiple post requests with ajax. My current script is: function AJAX(form, integer) { var message = 'post=whatever'; var url = '/main/misc.php?status=ajax'; if(window.XMLHttpRequest) { var ajax = new XMLHttpRequest(); } else if(window.ActiveXObject) { var ajax = new ActiveXObject('Microsoft.XMLHTTP'); } if(ajax) { ajax.open('POST', url); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; Charset = UTF-8'); ajax.send(message); } } Now, I've tried stuff like var message = 'post=whatever&post2=anotherpost'; But that doesn't seem to work. Also tried var message2 = 'post2=anotherpost'; ajax.send(message, message2); Had a google as well and couldn't find anything. Thanks in advance, Jack. Quote Link to comment Share on other sites More sharing options...
solon Posted April 23, 2009 Share Posted April 23, 2009 Hi jackpf , i would suggest that you use a library. it makes it really easy for you on that problem. I personally use jquery of google. here is an example on how to send to values(posts) into your server side script using ajax. //this is to load the functions of jquery <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> //here is the script you have to write <script type="text/javascript"> function act-deact() { $('#id').load('act-deact.php',{ // ('#id') is the <div> or any other place you want to load the response. // 'act-deact.php' is the file that handles the values you send. 'emp_id': $('#emp_id').val(), // emp_id is the 'id' of the <input type='' id='' /> you want to send 'do': $('#do').val() // same as above }); } </script> I hope this helps. If you need any more help just let me know Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 23, 2009 Author Share Posted April 23, 2009 Ahh, yeah, I prefer to code the stuff myself rather than use libraries tbh. But I'd be surprised if jQuery can do this tbh. Hmm...I've been trying to put the message into an array, and then send each message, but I can't get it to work...I'm not sure if this is actually possible. Ahh well, thanks for your help. Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 23, 2009 Author Share Posted April 23, 2009 Wow, actually managed to do it. All it took was a simple & in the post. Weird, I could swear I tried that before... Quote Link to comment Share on other sites More sharing options...
xtopolis Posted April 24, 2009 Share Posted April 24, 2009 I'm surprised that worked, aren't you missing a content length header for the post request? Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 24, 2009 Author Share Posted April 24, 2009 I don't think you have to use a content length header. Well...I never have and my ajax works. For example, this would work (theoretically): function AJAX(form, integer) { var message = 'post=whatever'; message += '&post2=something else'; // HERE IT ALLOWS ME TO ADD ANOTHER POST var url = '/main/misc.php?status=ajax'; if(window.XMLHttpRequest) { var ajax = new XMLHttpRequest(); } else if(window.ActiveXObject) { var ajax = new ActiveXObject('Microsoft.XMLHTTP'); } if(ajax) { ajax.open('POST', url); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; Charset = UTF-8'); ajax.send(message); } } So yeah, if anyone is wondering how to do this, all you need to do is set it up like a query string. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.