0

I think I'm doing this wrong for what I want returned with PHP. What I'm trying to do is check if the username is paid or not via ajax.

The PHP, which works by itself if I do this:

$username = $_POST['username']; 

     function checkPlayer($player) {
        $mcURL = 'http://www.minecraft.net/haspaid.jsp?user=';
        $auth = file_get_contents($mcURL . $player);

        if (trim($auth) == "true") {
          echo $player. ' is ';
        } else {
          echo $player. ' is ';
        }

        return $auth;
      }

      echo checkPlayer($username);

Does what I want if I change $username value to something static, like $username = "Notch";. But not if I use $_POST['username'], and use the following JS:

$(document).on('keyup', 'input', function(){

        var inputVal = $('input').val();
        $.post('auth.php', inputVal, function(data){
          console.log(input.Val + ' is ' data);
        });
      });

Which should print out in console if I typed in 'Notch', true, if something else like 'fslfjslkfjls' should be false. HTML:

<form>
    <input type="text" name="username" value="" class="authcheck">
  </form>

What do I have wrong?

UPDATE: After galchen's answer, it (kinda) works now (doesn't give an error)

var inputVal = $('input').val();
        $.post('auth.php', { 'username' : inputVal }, function(data){
            console.log(inputVal + ' is ' + data);
        });

But now everything that inputs returns true, even if it's not true. How can I fix this?

0

3 Answers 3

3

try:

$.post('auth.php', { 'username' : inputVal }, function(data){
    console.log(input.Val + ' is ' data);
});

you need to send the object in the ajax - it's a dictionary of the request variables

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

1 Comment

Well, it works! but now everything I type in is always returning true.
0

you can also use: .serialize()

$.post('auth.php', { $("form").serialize() }, function(data){
console.log(input.Val + ' is ' data);

});

Encode a set of form elements as a string for submission

Comments

0

You can use this code that works fine with the ajax call as u expected.

auth.php:

 $username = $_POST['username']; 
  function checkPlayer($player) {
    $mcURL = 'http://www.minecraft.net/haspaid.jsp?user=';
    $auth = file_get_contents($mcURL . $player);

    return $auth;
  }

  echo checkPlayer($username);

Take this as your html for the ajax response:

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    </head>
    <body>
    <form>
    <input type="text" name="username" value="" class="authcheck">
  </form>
<script type="text/javascript">
    $(document).on('keyup', 'input', function(){
        var inputVal = $('input').val();
        $.post('auth.php', { 'username' : inputVal }, function(data){
            console.log(inputVal +' is '+ data);
        });
      });
    </script>
    </body>
</html>

Just modified the php part to exclude unnecessary codes and you can check your console now with the above html file or you could use it in your code.

Hope you can check it with the above code.

Comments

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.