Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP AJAX POST send multiple variables

Tags:

ajax

php

with an ajax post to php can i send multiple variables, and if so whats the syntax?

loadXMLDoc("scripts/product_transfer.php?group="+group+"subgroup="+subgroup+"user="+user+,function()

something like that??

here is the function code:

//---------------------------------------------------------------------------------------------------    -----------------------------------------------------------------------
//Function to handle ajax
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("POST",url,true);
xmlhttp.send();
}
like image 876
David Avatar asked Jul 15 '26 19:07

David


2 Answers

Yes you can but you have forgotten the &s between values. You can also send data with POST method as argument to send() method. Also don't forget to use encodeURIComponent() on string values:

xmlhttp.open( "POST", url, true );
xmlhttp.send( "group="+encodeURIComponent(group)+
              "&subgroup="+encodeURIComponent(subgroup)+
              "&user="+encodeURIComponent(user) );        
like image 55
nobody Avatar answered Jul 17 '26 17:07

nobody


You should add & or '&amp'; between different variables in query string like

scripts/product_transfer.php?group="+group+"&subgroup="+subgroup+"&user="+user

like image 34
Andrej Ludinovskov Avatar answered Jul 17 '26 16:07

Andrej Ludinovskov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!