David
David

Reputation: 191

PHP AJAX POST send multiple variables

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();
}

Upvotes: 3

Views: 16623

Answers (5)

Amol
Amol

Reputation: 343

data="postvarname1="+varvalue+"postvarname2"+var

& so on.....

Upvotes: 0

user2155518
user2155518

Reputation:

i have written code for this and its work well,

page-1.

<select name="qt_n1" id="qt_n1" style="width: 100px;" onchange="return q1mrks(this.value,<?php echo $gen1_marks; ?>)">
                                <option>No. of Que.</option>
                                <?php
                                    for($i=1;$i<=25;$i++){?>
                                        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                <?php } ?>
                            </select>

page-2.js

function q1mrks(country,m)
{
  // alert("hellow");
if (country.length==0)
  {
   //alert("hellow"); 
  document.getElementById("q1mrks").innerHTML="";
  return;
  }
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=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("q1mrks").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","../location/cal_marks.php?q1mrks="+country+"&marks="+m,true);

//mygetrequest.open("GET", "basicform.php?name="+namevalue+"&age="+agevalue, true)
xmlhttp.send();
}

and simply i got the values on the third page

page-3.php

<?php 
    echo $Q1mrks = $_GET['q1mrks'];
    echo $marks = $_GET['marks'];
?>
<div id="q1mrks"></div>

Thank You,

Upvotes: 1

nobody
nobody

Reputation: 10645

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) );        

Upvotes: 6

Ashley
Ashley

Reputation: 1489

Try this!

loadXMLDoc("scripts/product_transfer.php?group="+group+"&subgroup="+subgroup+"&user="+user+, function() { //Code to run when data is sent back});

Upvotes: 1

Andrej
Andrej

Reputation: 7504

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

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

Upvotes: 1

Related Questions