i want to display alert on the basis of responce text in AJAX, meaning to say i am getting responce text in id "responce " through php file by AJAX method, when responce is incorrect it shows alert before form submission the code is as follow,
<?php session_start(); ?>
<script type="text/javascript">
var xmlHttp
function checkCAP(str) {
if (str=="") {
alert("plase enter the code");
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request")
return
}
var url="test.php"
url=url+"?id="+str
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("responce").innerHTML=xmlHttp.responseText
}
}
function GetXmlHttpObject() {
var xmlHttp=null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
<?php
if (!empty($_GET['id'])) {
include("securimage.php");
$img = new Securimage();
$valid = $img->check($_GET['id']);
if($valid == true) {
$responce = "correct";
} else {
$responce = "incorrect";
}
echo $responce;
} else { //form is posted
?>
<html>
<head>
<title>Securimage Test Form</title>
</head>
<body>
<form method="POST">
<!-- pass a session id to the query string of the script to prevent ie caching -->
<img src="securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>"><br />
<input type="text" name="code" onblur= "checkCAP(this.value);"/><br />
<input type="submit" value="Submit Form" />
</form>
<div id ="responce">Responce will be dispaly here</div>
<?php
}
?>
</body>
</html>
however the script is running very much fine.
i have used may methods to but in vain..
please help somebody how to do it, or any alternative way...
thanks in advance...