#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Passing Data To External PHP File From JavaScript File


    Hi Everyone

    I have the following js function which is located in its own file named checkUsername.js :

    Code:
    function checkUsername(){
    	var status = document.getElementById("usernamestatus");
    	var u = document.getElementById("uname").value;
    	if(u != ""){
    		status.innerHTML = 'checking...';
    		var hr = new XMLHttpRequest();
    		hr.open("POST", "passUsername.php", true);
    		hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    		hr.onreadystatechange = function() {
    			if(hr.readyState == 4 && hr.status == 200) {
    				status.innerHTML = hr.responseText;
    			}
    		}
        var v = "name2check="+u;
        hr.send(v);
    	}
    }
    I have the following PHP file named passUsername.php which is located in the same directory as checkUsername.js


    Code:
    <?php
    
     include_once('../classes/validation.class.php');
    
    echo 'Hello'; // Testing purposes
    
     
    ?>

    It seems the data is not being sent to the PHP file from the javascript file. On the HTML page the output "Checking..." appears but then nothing else. I have tried this with all code in the same file and it works, but I really do not want to jam everything into one file.

    Clearly I am calling the PHP file incorrectly in the js function, would anyone be able to tell me how I should call it?

    Thanks for your time!
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    there are no obvious errors in the code itself, so you'll have to debug the problem on your own server. Open the developer tools of your browser (F12 in Chrome) and check the JavaScript console for errors. In the "Network" tab, you can also track the POST request and see the exact status.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    there are no obvious errors in the code itself, so you'll have to debug the problem on your own server. Open the developer tools of your browser (F12 in Chrome) and check the JavaScript console for errors. In the "Network" tab, you can also track the POST request and see the exact status.

    Thank you so much, using the method you suggested I was able to find the problem
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    And what was it?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo