i want to get the alert on responce text, but getting nothing, my 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")
	{
	var response = xmlHttp.responseText;
      if( response == "incorrect"){
	  	alert(Please enter the right words); 
		}
 	} 
}

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= "return checkCAP(this.value);"/><br />
	<input type="submit" value="Submit Form" />
	</form>
  <div id ="responce">Responce will be dispaly here</div>
<?php
}
?>
</body>
</html>

When i change the code as follow, i get all the source script in alert box...

function stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
  	alert(xmlHttp.responceText); 
		}
 	} 
}

but when i change the code as follow, it runs very much fine, it out puts PHP script result in the tag which reside in html part. but my application requirement is to get PHP out put in alert box.

function stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
               document.getElementById("response").innerHTML = xmlHttp.responceText		
 	} 
}

is there any solution????

responSe, not responCe

sorry, its just spellling mistake, but the result is at its place..

You can skip some of the lines in your php code, by simply creating a new .txt file and then save it as incorrect.txt in the same directory along with your test.php file and include the single word you need -- which is the incorrect word...

Here's the code for the main page. Try to read further instructions inside the codeLines'...

Run the whole document in your PHP server:

<?php /*><!--?*/ 
session_start();
 if ( !empty( $_GET['id'] )) { 
   include("securimage.php");
   $img = new Securimage();
   $valid = $img->check( $_GET['id'] );
} else {
/*--><?*/ ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html4" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Live Help!</title>

<script type="text/javascript">
<!-- 
var xmlHttp;
var method;
var stateChange;
var url;
var response;
var GetXmlHttpObject;

stateChange = function() {
   if (( xmlHttp.readyState === 4 ) || ( xmlHttp.readyState === "complete" )) {
   response = xmlHttp.responseText;
      if ( response === "incorrect" ) {
      alert("Please enter the right word"); 
      return false; } alert("Thank you and this (" + response +  ") serves as a valid data.");
      document.getElementById("response").innerHTML = response;
   }
};

GetXmlHttpObject = function( str ) {
    url = (( str === "" || str.length === 0 ) ? "incorrect.txt" : ( "test.php?id=" + str )); 

/* Create a seperated txt file and save it as ( incorrect.txt ), that has the "incorrect" word on it. */


/* If thus any condition fails to achieve in the ( str ) expression, then it will automatically load the incorrect.txt where you get the exact word (incorrect) you need, to process your alert( "somthing fails!" ) failure. */
 
xmlHttp = null;
method = "GET";
   try {
      if ( window.XMLHttpRequest ) {
      xmlHttp = new XMLHttpRequest();
   } else if ( window.ActiveXObject ) {
         try {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
         } catch( e1 ) {
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
         }
      }
   } catch( e2 ) {
   (( window.createRequest ) ? xmlHttp = window.createRequest() : xmlHttp = null );
   }
   (( xmlHttp.overrideMimeType ) ? xmlHttp.overrideMimeType("text/xml") : xmlHttp ); 
   if ( xmlHttp !== null ) { 
   xmlHttp.onreadystatechange = stateChange;
      
   xmlHttp.open( method, url, true );
   (( xmlHttp.setRequestHeader && method === "POST" ) ?  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") : xmlHttp ); 
   xmlHttp.send( null );
   } else {
      alert("\nYour browser does not support AJAX Request!"); 
   }
};  

// -->
</script>
</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="GetXmlHttpObject( this.value );"/><br />
   <input type="submit" value="Submit Form" />
   </form>
  <div id ="response">Response will be display here!</div>
<?php } ?>
</body>
</html>

thank you for detailed reply.
the script is running fine, when i enter no code in input box, it alerts "Please enter the right word" when enter right or wrong code it alerts "Thank you and this (</body> </html>)serves as a valid data.

You'll have to change the response being transfered to your <div id="response"></div> and incorporate it along with your PHP code to display your desired content . I've just provided that example, just to give idea on how you will be able to handle those alerts.

Or you could just re-apply this over at the stateChange() function:

stateChange = function() {
   if (( xmlHttp.readyState === 4 ) || ( xmlHttp.readyState === "complete" )) {
   response = xmlHttp.responseText;
      if ( response === "incorrect" ) { // Condition is false.
      alert("Please enter the right word"); 
      return false; } // Condition is true

 alert("Valid Information:\nField has been validated successfully.\nThank you...");
      document.getElementById("response").innerHTML = xmlHttp.responseXML; // If you are trying to display the actual content of the test.php file

// This part belongs to you-- on what action should be taken, and what is not to be applied.
   }
};

thank you very much essential for the solution. i was trying to get the solution for last 10days. i have a request, can you please post all the right code.

Can you give me some pointers on this current work?

Since current issue is focused on alerting the user with the response gathered from input field, after those process things will become boundless...

The real question is--what are the next things, to do after the whole alert process is done?

And also I can't run testing using PHP codes, so the results may become unstable when you play actual run inside your PHP server.

actually, it is for capatcha securimage validation. when any user enters the wrong picture words in input field or does not enter any words, the script respond with message alert box, if he enter the right words, it just allows him to submit the form.

All the files are attached with the script discussed here as a test2.php. please change the extension of elephant.txt to elephan.ttf.

Thanks in advance....

Since i can't open any of those files. Then I'll just simulate everything using simple words for the user that they need to verify first, before they can submit the form.

Assuming that we have the following words inside the test.php that will be sent back to the user via ajax request.

AmBxUX
LetTer
cWoRDs
EXampLeS

if any of these words matched the user entered value, then they continue on submitting their form entries. Else will return invalid.

<?php /*><!--?*/ 
session_start();
 if ( !empty( $_GET['id'] )) { 
   include("securimage.php");
   $img = new Securimage();
   $valid = $img->check( $_GET['id'] );
} else {
/*--><?*/ ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html4" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Live Help!</title>

<script type="text/javascript">
<!-- 
var xmlHttp;
var method;
var scrambler;
var captcha;
var stateChange;
var url;
var response;
var GetXmlHttpObject;
   
captcha = function() {
   var cap = [ "AmBxUX", "LetTer", "cWoRDs", "EXampLeS" ];
   return cap[ Math.floor( Math.random() * Number( cap.length ))];
};
   
stateChange = function( str ) {
   if (( xmlHttp.readyState === 4 ) || ( xmlHttp.readyState === "complete" )) {
   scrambler = [ ]; 
   response = xmlHttp.responseText;
   for ( var x = 0; x < ( Number( response.split(/[\n\s]+/).length )); x++ ) {
      scrambler[ response.split(/[\n\s]+/)[ x ] ] = response.split(/[\n\s]+/)[ x ];
   }
      if ( response === "incorrect" ) {
       alert( "Please enter the code" );
      return false; }

      if ( scrambler[ str ] ) { 
document.getElementById("response").innerHTML = "<p>This serves <b>" + String( scrambler[ str ] ).fontcolor("green") + "</b> as a valid code, you may now proceed to the next step.</p>";
document.getElementById("sbm").disabled = false;
      return true; }
   document.getElementById("response").innerHTML = "<p>Verification failed: <b><del>" + str.fontcolor("red") +"</del></b> is not a valid code.</p>"; 
   }
};

GetXmlHttpObject = function( str ) {
    url = (( str === "" || str.length === 0 ) ? "incorrect.txt" : ( "test.php?id=" + str )); 

/* Create a seperated txt file and save it as ( incorrect.txt ), that has the "incorrect" word on it. */

/* If thus any condition fails to achieve in the ( str ) expression, then it will automatically load the incorrect.txt where get the exact word (incorrect) you need, to process your alert( "somthing fails!"). */
 
xmlHttp = null;
method = "GET";
   try {
      if ( window.XMLHttpRequest ) {
      xmlHttp = new XMLHttpRequest();
   } else if ( window.ActiveXObject ) {
         try {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
         } catch( e1 ) {
         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
         }
      }
   } catch( e2 ) {
   (( window.createRequest ) ? xmlHttp = window.createRequest() : xmlHttp = null );
   }
   (( xmlHttp.overrideMimeType ) ? xmlHttp.overrideMimeType("text/xml") : xmlHttp ); 
   if ( xmlHttp !== null ) { 
   xmlHttp.onreadystatechange = function() { stateChange( str ) };
      
   xmlHttp.open( method, url, true );
   (( xmlHttp.setRequestHeader && method === "POST" ) ?  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") : xmlHttp ); 
   xmlHttp.send( null );
   } else {
      alert("\nYour browser does not support AJAX Request!"); 
   }
};  
window.onload = function() {
   document.getElementById("capTest").innerHTML = "Type in <b>" + captcha().fontcolor("red") + "</b>, letters are case sensitive.";
}; 
// -->
</script>
</head>
<body>
<form id="testform" name="testform" action="yourProcess.php" method="post">
<div>
<div id="capTest"></div><br><br>
<img src="securimage_show.php?sid=<?php /*><!--?*/ echo md5( uniqid( time() )); /*--><?*/ ?>">
<br><br>
<input type="text" id="code" name="code" value="" onblur="GetXmlHttpObject( this.value );" size="30">
<br><br>
<input id="sbm" name="sbm" type="submit" value="Submit Form" disabled>
</div>
</form>
<div id="response">Responce will be displayed here.</div>
<?php } ?>
</body>
</html>

thank you essential, for giving so much ideas & solutions.

You are always welcome...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.