how to get data from ajax responseText. Here i use three parts.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harini0590
    New Member
    • Apr 2012
    • 19

    how to get data from ajax responseText. Here i use three parts.

    In first part
    1.html..i have one combo box
    and code as below
    Code:
    <body>
    <form name="ajax">
    <select  onchange="request()" id="cmbnum">
    <option value=1>ONE</option>
    <option value=2>TWO</option>
    <option value=3>THREE</option>
    </select></form>
    this java script part.i have function as below.
    when i am using the syntax it return only empty alert....
    Code:
    function request()
    {
    	optnum=document.getElementById("cmbnum").value;
    	
    	url="ajax\response.php?num="+optnum;
    	
    
    	
    xmlhttp.onreadystatechange=function()
     {
       if(xmlhttp.readyState==4 && xmlhttp.status==200 )
    	 {
    		 
    	   ajaxres=xmlhttp.responseText;
    	document.getElementById("sample").innerHTML=ajaxres;
    	}
     }
    xmlhttp.open("GET",url,true);
     xmlhttp.send();
    }
    this php part..
    Code:
    if(isset($_REQUEST['num']))
    { 
     	
    	switch($_REQUEST['num'])
    	{
    		case 1:
    		echo "ONE";
    		break;
    		case 2:
    		echo "TWO";
    		break;
    		
    	}
    	
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    where is the part where you define the XMLHttpRequest object?

    Comment

    • harini0590
      New Member
      • Apr 2012
      • 19

      #3
      i include this as separate file named ajaxex.js in the function request()..
      Code:
      try
      { 
      	alert("hai");
      	xmlhttp=new XMLHttpRequest();
      }
      catch(e)
      {
      	try
      	{alert("hai");
      	 xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
      	catch(e)
      	{
      		try
      		{alert("hai");
      			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
      		catch(e)
      		{
      			xmlhttp=null;
      			alert("ur browser is not work");
      		}
      	}
      }

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        i include this as separate file named ajaxex.js in the function request()
        how do you do that exactly?

        did you verify that all your variables/parameters are set correctly?

        Comment

        • harini0590
          New Member
          • Apr 2012
          • 19

          #5
          by using the statement below i include the file in these file
          Code:
          <script language="javascript" type="text/javascript" src="ajaxex.js">
          </script>
          when i select anything from list box,suppose if it is one means it should display as ONE.When i simply give echo statement in php part it display back as alert message.but the code inside if statement is not work.so only i doesn't return anything to response text..
          what i need to do now..

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            do you have a live page?

            Comment

            • harini0590
              New Member
              • Apr 2012
              • 19

              #7
              My coding is work.. with slight modification in php part and in <option> tag.
              Code:
              <?php
              
              	$sam=$_REQUEST['num'];
              	
              	switch($sam)
              	{
              		case "ONE":
              		echo "ONE";
              		break;
              		case "TWO":
              		echo "TWO";
              		break;
              		case "THREE":
              		echo "THREE";
              		break;
              		
              	}
              	
              ?>
              
              <form name="ajax">
              <select  onchange="javascript:request()" id="cmbnum">
              <option value="ONE">ONE</option>
              <option value="TWO">TWO</option>
              <option value="THREE">THREE</option>
              </select>
              here i get get alert when i select item in list box..
              thanks for ur help..

              Comment

              Working...