1

This may be a noobish question as I am just now learning PHP, jquery/ajax and even javascript. I am having trouble passing a variable to PHP to query the MYSQL server. I am passing the variable to the javascript function in the following HTML in newCharacter.php:

    Choose your name:<input type='text' id='name' 
            onchange="verifyName(this.value)">

I then try to send it to the PHP page via the following code.

function verifyName(name) {
    $.ajax({
        url: "Classes/pc.php",
        type: "POST",
        data: {
            'name': name
        }
    });

    <? php
    echo pc::nameTaken($_SESSION['name']).';'; ?>

}

However I get the following error in the page source:

var result = 0;
             <br />
<b>Notice</b>:  Undefined index: name in
<b>D:\Server\htdocs\dungeonexplorers\newCharacter.php</b> 
on line <b>33</b><br />    ;                

What am I doing wrong? As I understand it the POST session variable is not getting passed along properly.

Edit: Adding Pc class (relevant parts)

public static function nameTaken($name)
{
    try{
        $db = DBConnect::connect();
        $result = $db->prepare('SELECT name                
           FROM pc WHERE name=:name LIMIT 1');
        $result->bindParam(':name',$name);
        $result->setFetchMode(PDO::FETCH_ASSOC);
        $result->execute();
        $return = $result->fetchAll();

        if ($return)
        {
            return true;
        }
        else{
            return false;
        }
    }
    catch (PDOException $e)
    {
        Echo 'PDO error: ' . $e->getMessage() . '<br>';
    }
    catch (Exception $e)
    {
        Echo 'General Error retrieving username and password.<br>';
    }
}
2
  • Please show us newCharacter.php lines 28-38 Commented Sep 1, 2013 at 4:37
  • This is it. I simply removed alerts to try and debug. Commented Sep 1, 2013 at 4:55

2 Answers 2

2

Welcome to web development :)

POST Body variables are contained in the $_POST superglobal. Try switching $_SESSION to $_POST and see if that helps.

You can check out http://php.net/manual/en/reserved.variables.post.php for more info

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

4 Comments

I feel stupid for not noticing this! I tried however and still get the same error.
Can you post the code of your pc class? I'd like to see what nameTaken is doing.
edit: I would alert(name) or console.log(name) in your jquery to make sure the variable is getting passed correctly
Where would you put it exactly? In the ajax statement? Because putting it simply in the verifyName(name) function, it comes up with the correct name to check
1

Not really an answer, but I just want to point out that

    $return = $result->fetchAll();

    if ($return)
    {
        return true;
    }
    else{
        return false;
    }

Is kind of pointless, you're basically saying: return true if it's true.. return false if it's false, you might just aswell return the $return right away since it's a bool :)

But I'm afraid it has nothing to do with your question.

But, are you mixing together javascript and php? it appears so in your first code example. You should also take advantage of .success, .complete, .failure etc etc that can help you to see what happens with the response you get from the ajax call.

5 Comments

I have some PHP in the javascript to call the PHP function that will run a MYSQL query on the server and return the result. Then the rest of the javascript takes the result and applies to it the HTML on the page. I am adding the success and failure to see what happens. I'll modify to return the $return. Nice catch.
That's not what you want to do, you never want to mix php and javascript. You want to pc.php to only contain the php code and perform behavior corresponding to what the JavaScript says. Once returned from the ajax call, there should be no php code executed.
So you would have a PHP page for each query you want to do?
No, not at all, but you can execute different pieces of queries on the same page depending on what you're asking for. You can use if else statements or switch cases. You can POST variables that say which query you want to execute, you can store the posted in a variables and use them like in every other logical programming.
Since I don't have anything more appropriate I will do as you suggest.

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.