Pass many variables to ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Pass many variables to ajax

    I want to pass 2 variables in my php page to ajax page in onchange function. Actually this drop down box use in many pages. So I want to pass the page name to ajax page with the user selected value. How can I do that? Please help me..
    This is my code.
    Code:
    ?>
    				
    <select name="Make"  class="normalTxt" onChange='getMakeIndexPage(this.value); return false; ' id="Make" >
    <option value="Select">Select</option>
    <?php  $mysql_result1 = mysql_query("SELECT DISTINCT vehicleMake FROM  vehicles");
    						while($row = mysql_fetch_array($mysql_result1)){
    foreach( $row AS $key => $val )
    	$Make = stripslashes( $val );
    	?>
    	<option value=<?php  echo  "'$Make'" ?> ><?php echo "$Make&nbsp;&nbsp;&nbsp;&nbsp&nbsp&nbsp"?>
    				<?php 
    	}	
    				?>
    </option>
    </select>

    ajax page
    Code:
    function getMakeIndexPage(){
    	
    	var dropdownIndex = document.getElementById('Make').selectedIndex;
    	var make = document.getElementById('Make')[dropdownIndex].value;
    	var request = GetXmlHttpObject();
    	var url="commonFunction.php";
    	url=url+"?make="+make+"&S=" +1;
    	//alert (url);
    	url=url+"&sid="+Math.random();
    	request.onreadystatechange=stateChanged;
    	request.open("GET",url,true);
    	request.send(null);
    	function stateChanged()
       {
    	 if (request.readyState==4)  
    		{
    			
    			document.getElementById("getMake").innerHTML=request.responseText;
    		
    		}
     	}
    	
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    where do you get the page name from?

    additional notes:
    compare
    Code:
    <select name="Make" class="normalTxt" onChange='getMakeIndexPage(this.value); return false;' id="Make">
    to
    Code:
    function getMakeIndexPage(){
     
        var dropdownIndex = document.getElementById('Make').selectedIndex;
        var make = document.getElementById('Make')[dropdownIndex].value;
        // …
    wouldn’t it be easier to use
    Code:
    function getMakeIndexPage(make){
        // …
    instead?

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      I put the page name inside the onchange function. But It doesn't work.

      Code:
      <select name="Make"  class="normalTxt" onChange='getMakeIndexPage(this.value,index); return false; ' id="Make">
      and the ajax page
      Code:
      function getMakeIndexPage(test){
      	alert(test);
      ...
      }

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        obviously, the first parameter is the dropdown’s value and the second parameter is an (currently) undefined variable named index. additionally, your function ignores the second parameter completely.

        Comment

        • deric
          New Member
          • Dec 2007
          • 92

          #5
          you need to enclose the page's name with quotes like
          Code:
          getMakeIndexPage(this.value, 'index');
          without the quotes, javascript will translate it as a variable and will produce an error because it's undefined.

          and then add the second parameter on your function to receive the page name.

          Comment

          Working...