Jump to content

Passing data to externam PHP file from JavaScript File


Recommended Posts

Hi Everyone

 

I know this is technically a javascript question.. but I was looking for some help.

 

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

 

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

 

<?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!

Edited by iammat

There is no if statement in the code you posted. 

 

That also indicates you need to enable error reporting, set to E_ALL so you can get useful errors and notices.

Edited by Jessica
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.