By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,828 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,828 IT Pros & Developers. It's quick & easy.

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

P: 19
In first part
1.html..i have one combo box
and code as below
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <form name="ajax">
  3. <select  onchange="request()" id="cmbnum">
  4. <option value=1>ONE</option>
  5. <option value=2>TWO</option>
  6. <option value=3>THREE</option>
  7. </select></form>
  8.  
this java script part.i have function as below.
when i am using the syntax it return only empty alert....
Expand|Select|Wrap|Line Numbers
  1. function request()
  2. {
  3.     optnum=document.getElementById("cmbnum").value;
  4.  
  5.     url="ajax\response.php?num="+optnum;
  6.  
  7.  
  8.  
  9. xmlhttp.onreadystatechange=function()
  10.  {
  11.    if(xmlhttp.readyState==4 && xmlhttp.status==200 )
  12.      {
  13.  
  14.        ajaxres=xmlhttp.responseText;
  15.     document.getElementById("sample").innerHTML=ajaxres;
  16.     }
  17.  }
  18. xmlhttp.open("GET",url,true);
  19.  xmlhttp.send();
  20. }
  21.  
this php part..
Expand|Select|Wrap|Line Numbers
  1. if(isset($_REQUEST['num']))
  2.  
  3.     switch($_REQUEST['num'])
  4.     {
  5.         case 1:
  6.         echo "ONE";
  7.         break;
  8.         case 2:
  9.         echo "TWO";
  10.         break;
  11.  
  12.     }
  13.  
  14. }
  15.  
Apr 21 '12 #1
Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
where is the part where you define the XMLHttpRequest object?
Apr 22 '12 #2

P: 19
i include this as separate file named ajaxex.js in the function request()..
Expand|Select|Wrap|Line Numbers
  1. try
  2.     alert("hai");
  3.     xmlhttp=new XMLHttpRequest();
  4. }
  5. catch(e)
  6. {
  7.     try
  8.     {alert("hai");
  9.      xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
  10.     }
  11.     catch(e)
  12.     {
  13.         try
  14.         {alert("hai");
  15.             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  16.         }
  17.         catch(e)
  18.         {
  19.             xmlhttp=null;
  20.             alert("ur browser is not work");
  21.         }
  22.     }
  23. }
  24.  
  25.  
Apr 22 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
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?
Apr 22 '12 #4

P: 19
by using the statement below i include the file in these file
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript" src="ajaxex.js">
  2. </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..
Apr 30 '12 #5

Dormilich
Expert Mod 5K+
P: 8,639
do you have a live page?
Apr 30 '12 #6

P: 19
My coding is work.. with slight modification in php part and in <option> tag.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $sam=$_REQUEST['num'];
  4.  
  5.     switch($sam)
  6.     {
  7.         case "ONE":
  8.         echo "ONE";
  9.         break;
  10.         case "TWO":
  11.         echo "TWO";
  12.         break;
  13.         case "THREE":
  14.         echo "THREE";
  15.         break;
  16.  
  17.     }
  18.  
  19. ?>
  20.  
  21. <form name="ajax">
  22. <select  onchange="javascript:request()" id="cmbnum">
  23. <option value="ONE">ONE</option>
  24. <option value="TWO">TWO</option>
  25. <option value="THREE">THREE</option>
  26. </select>
  27.  
here i get get alert when i select item in list box..
thanks for ur help..
Apr 30 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.