0

Good day, I'm trying to use Ajax in my web application. But I have a little problem with it. I try to control a form if some username has already been registered or not. But my JavaScript seem does not send $_POST value to PHP. And responses that user in not defined on the line where I have $_POST['user'].

Here what I have.

PHP:

<?php
$opt = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
$dsn ='mysql:dbname=someone;host=127.0.0.1;charset=utf8';
$user='someone';
$pswd='aaa';

$dbh = new PDO($dsn, $user, $pswd, $opt);
$query="SELECT 1 FROM users WHERE  username = :username";   
        $query_p=array(':username' => $_POST['user']); 

        try 
        { 
            $statment = $dbh->prepare($query); 
            $result = $statment->execute($query_p); 
        }catch(PDOException $e) 
        {
            echo "Can't run query: " . $e->getMessage();        
     } 
        $row = $statment->fetch();
        if($row){           
        return 0;
        }else {
        return 1;
        }
?>

So it opens a connection to database and runs a query

JavaScript:

function checkUser(e){
    var state1 = document.getElementById("alert_text");
    var u = document.getElementById("user").value;
    if(u != ""){

        state1.innerHTML = 'processing...';
        var request = new XMLHttpRequest();

        request.open("POST", "validation.php", true);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
    var result=request.responseText;
alert(result);
   if(result==1){
        state1.innerHTML="OK";
    }else{
        state1.innerHTML="Alredy exists!";
    }
    }
  };
        request.send(u);
        var slova = request.responseText;
        }
}
document.getElementById("user").addEventListener("blur",checkUser,false );

So technically it is ajax.

HTML:

<form id="check"  name="signUp" method="post">
 <p class="alert_text"></p>
   <label for="user">Username:</label><br />

   <input id="user" name="user"  type="text" ><br />

</form>

I don't really see what's the problem...

8
  • use "POST" in your ajax call instead of "GET". Commented Jan 26, 2016 at 12:46
  • @steady_daddy php still don't know what is user Commented Jan 26, 2016 at 12:48
  • You dont have an alert_text id in your form Commented Jan 26, 2016 at 12:48
  • 1
    why is this commented out? //$dbh = new PDO($dsn, $user, $pswd, $opt); given the other answers. Commented Jan 26, 2016 at 12:50
  • Seems like you're just passing the value but not the key=>value pair. See @jeroen's answer below. Commented Jan 26, 2016 at 12:52

2 Answers 2

4

You're not passing the username into the PHP script. Your PHP is looking for a POST variable, $_POST['user'] – in your JavaScript you make a GET request, so the PHP script has nothing to look up.

Sign up to request clarification or add additional context in comments.

3 Comments

yes, you are right. I do it with POST I just tried it with GET. But it either not working with POST
It sounds like the user variable isn't making it through to your PHP script. If you look in your browser developer tools in the Network tab, you should see a request to validation.php – what parameters are being sent? Is user there?
Form Data user:gusevvas so it sends something.
3

Based on the edited question: You are not sending any key-value pairs to the server, just a single value.

You need to change:

var u = document.getElementById("user").value;

to:

var u = 'user=' + encodeURIComponent(document.getElementById("user").value);

And then later on the pair will be sent correctly:

request.send(u);    // sends a single key-value pair

Note that I have just added the encodeURIComponent function to make sure the value gets encoded correctly.

12 Comments

and not sure if OP did uncomment out //$dbh = new PDO($dsn, $user, $pswd, $opt); - If not, it's also not connecting.
document.getElementById("alert_text") and they're using <p class="alert_text"></p>, the operative word here being class so that never happens. A lot of bugs in there ;-)
Probably, or he's gone out to get us all coffee and a danish for all our hard work. ;-)
What happens if you replace return 1 and return 0 with echo '1' and echo '0'? Been a while since I did any PHP but it might be that if your script doesn't echo anything back, the JavaScript doesn't see anything.
@MattAndrews FINALLY! Thank you! It works now! I've changed it to echo. Thank you so much!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.