3

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();
}
6
  • Is there a specific reason why you're not using a library to make Ajax requests easier? Commented Aug 26, 2011 at 10:27
  • @Pekka ... that's how learning is done. For every other reason, you are right, of course. Commented Aug 26, 2011 at 10:28
  • i dont know about creating a library to make Ajax requests easier, im still new to ajax. for now can i ask can i do this and if so what is the correct syntax? Commented Aug 26, 2011 at 10:29
  • Just add & group="+group+"&subgroup= and boom you have multiple variables. Commented Aug 26, 2011 at 10:29
  • 1
    @Martin yeah, it's fine to do this on ground level for learning purposes, of course. But it's often that people just follow the wrong (years-old) tutorials Commented Aug 26, 2011 at 10:29

5 Answers 5

6

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) );        
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

1

Try this!

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

Comments

1

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,

Comments

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

& so on.....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.