Hi,

I don't know much about javascripts. I am preparing a module in PHP in which I have to validate data entered by a user through javascripts. Now there is a field in which I have ensure that the value entered by the user already resides in the database. How could I achieve this?

First you have to make a script in PHP to query the database with the value passed from the script below and then give an output in the script by using either print or echo. In the page that you want to pass the value back to the script you need to add the following....

<script language="JavaScript1.2" type="text/javascript">
function makeRequest(url) {
	var httpRequest;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    	httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
        	httpRequest.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) { // IE
    	try {
        	httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
        	try {
            	httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
	}

    if (!httpRequest) {
    	alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
	
    httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
    httpRequest.open('GET', url, true);
    httpRequest.send('');

}

// Function to deal with returned request
function alertContents(httpRequest) {
	if (httpRequest.readyState == 4) {
    	if (httpRequest.status == 200) {
			changeText(httpRequest.responseText);
			//alert(httpRequest.responseText);
        } else {
            alert('There was a problem with the request. '+httpRequest.status);
        }        
	}
}

Then with is in the page you only have to make your own function in JS to deal with the reply back from your script on the webserver. 

function changeText(){
    // This is were you put your script to handel if it is the same or not. 
}
</script>
This article has been dead for over six months. Start a new discussion instead.