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:
这可能是一个没有问题的问题,因为我正在学习PHP、jquery/ajax甚至是javascript。我在将变量传递给PHP以查询MYSQL服务器时遇到了麻烦。我将变量传递给以下HTML中的javascript函数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.
然后我尝试通过以下代码将它发送到PHP页面。
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.
我做错了什么?据我所知,POST会话变量没有被正确地传递。
Edit: Adding Pc class (relevant parts)
编辑:添加Pc类(相关部分)
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 个解决方案
#1
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 :)
有点无意义,你基本上是在说:如果是真的就返回真…如果返回false,您也可以立即返回$return,因为它是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.
但是,您是否将javascript和php混合在一起?它在您的第一个代码示例中是这样显示的。您还应该利用.success、.complete、.failure等功能来了解ajax调用的响应会发生什么。
#2
2
Welcome to web development :)
欢迎来到web开发:)
POST Body variables are contained in the $_POST superglobal. Try switching $_SESSION to $_POST and see if that helps.
POST主体变量包含在$_POST超全局变量中。尝试将$_SESSION切换到$_POST,看看是否有用。
You can check out http://php.net/manual/en/reserved.variables.post.php for more info
您可以查看http://php.net/manual/en/reserved.variables.php以获得更多信息。
#1
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 :)
有点无意义,你基本上是在说:如果是真的就返回真…如果返回false,您也可以立即返回$return,因为它是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.
但是,您是否将javascript和php混合在一起?它在您的第一个代码示例中是这样显示的。您还应该利用.success、.complete、.failure等功能来了解ajax调用的响应会发生什么。
#2
2
Welcome to web development :)
欢迎来到web开发:)
POST Body variables are contained in the $_POST superglobal. Try switching $_SESSION to $_POST and see if that helps.
POST主体变量包含在$_POST超全局变量中。尝试将$_SESSION切换到$_POST,看看是否有用。
You can check out http://php.net/manual/en/reserved.variables.post.php for more info
您可以查看http://php.net/manual/en/reserved.variables.php以获得更多信息。