Results 1 to 5 of 5

Thread: Javascript call php file.

  1. #1

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Javascript call php file.

    I hope its possible. Here's what I want to achieve. I have a register form that has fields for user info. and a button, in button onclick arguments it will call a javascript function. This function will check if the password field and confpassword field are the same if so call a php file where it has a function that check if the username inputted by the user already exist in the database if so prompt a message that username already used.

    How can I do that?

    Thanks in advance guys.

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Javascript call php file.

    You want to look at AJAX.

    Code:
    <script language="javascript" type="text/javascript">
    function createRequestObject()
    {
    	var request_o; //declare the variable to hold the object.
    	var browser = navigator.appName; //find the browser name
    	if(browser == "Microsoft Internet Explorer"){
    		/* Create the object using MSIE's method */
    		request_o = new ActiveXObject("Microsoft.XMLHTTP");
    	}else{
    		/* Create the object using other browser's method */
    		request_o = new XMLHttpRequest();
    	}
    	return request_o; //return the object
    }
    /* Function called to handle the list that was returned from the internal_request.php file.. */
    function handleThoughts(){
    	/* Make sure that the transaction has finished. The XMLHttpRequest object 
    		has a property called readyState with several states:
    		0: Uninitialized
    		1: Loading
    		2: Loaded
    		3: Interactive
    		4: Finished */
    	try
    	{
    		if(http.readyState == 4){ //Finished loading the response
    			/* We have got the response from the server-side script,
    				let's see just what it was. using the responseText property of 
    				the XMLHttpRequest object. */
    			var response = http.responseText;
    			//alert(response);
    			if(response!="ok")
    			{
    				alert("Please select another username!");
    			}
    		}
    	}
    	catch(Exception)
    	{
    	}
    
    }
    
    var http = createRequestObject();
    function checkUsername(userName)
    {
    	http.open('get', 'yourscript.php?username=' + userName);
    	http.send(null);
    }
    </script>
    
    <form>
    	<input type="text" name="uname" id="uname" />
    	<input type="button" onclick="javascript:checkUsername(document.getElementById('uname'));"/>
    </form>
    Note: your PHP script will have to return "ok" if the username is ok and anything else if it is not.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Javascript call php file.

    I'll explain that a little - Since the PHP function is on the server, you need to do a request to get PHP to run. The XMLHttpRequest object is what does that for you without having to refresh the page, it runs in the background and waits for the response. Meanwhile the response is generated by your PHP script, which is where the function runs - and then you process that response once you get it on the client side in your Javascript.

    "AJAX" is just a fancy name for a combination of several technologies - HTTP, Javascript, and XML primarily. You don't even need the XML part, it's just convenient to send the response in that format if you have a lot of variable-amount data being returned.

  4. #4

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Javascript call php file.

    Thanks john and pen for the response.

    Question: So all I need is declare a variable in php file for the user name then evaluate it?about this one? http.open('get', 'yourscript.php?username=' + userName);. Does it mean that yourscript.php file has a variable name username?
    also the function handleThoughts() I can't see where it was called?sorry for asking this question i'm very new at this thingy.
    Last edited by mar_zim; Apr 9th, 2006 at 09:15 PM.

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Javascript call php file.

    Requesting pages via Javascript, makes your server side applications kind of event driven.

    Your server side script receives the variables in exactly the same way as it would normaly. (you can also send via post):
    Code:
    xmlHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHTTP.send('username=blah&password=blah');
    The variables will be visible in PHP via the $_POST and $_GET.

    Unless your call to the server is part of the page load. You should make the request ansynchronously. This will ensure that should it time out the user is not sat their waiting for a hanging script.
    Code:
    // use a callback function to get the response
    xmlHTTP.onreadystatechange = function()
    {
        if (xmlHTTP.readyState != 4) return; // response is not reay until it is 4
    
        if (xmlHTTP.status == '200') {
            alert(xmlHTTP.responseText); // the body of the response is now avaialable
        }
    }
    
    xmlHTTP.send(null);
    Check out the Ajax link in my sig, I've posted a sample app which sends form variables via Javascript and validates an email address.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width