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

  php

Q(Question):

In first part
1.html..i have one combo box
and code as below


<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….


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..


if(isset($_REQUEST['num']))
{
switch($_REQUEST['num'])
{
case 1:
echo "ONE";
break;
case 2:
echo "TWO";
break;
}
}

A(Answer):

where is the part where you define the XMLHttpRequest object?

A(Answer):

i include this as separate file named ajaxex.js in the function request()..


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

A(Answer):

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?

A(Answer):

by using the statement below i include the file in these file

<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..

A(Answer):

do you have a live page?

A(Answer):

My coding is work.. with slight modification in php part and in <option> tag.


<?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..

LEAVE A COMMENT