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>';
}
}