php if 语句不适用于 jquery 变量
我发送到 PHP 的 jQuery 变量不起作用(或者至少,它似乎不起作用)。我已经用ajax将它发送到我的php。
请看一下,也许您可以看到问题:
$('.do').click(function(){
var cid2 = $(this).attr('id');
var gebridauthpos = cid2.indexOf('||');
var gebridauth = cid2.substring(gebridauthpos+2);
$.post("agenda.php", {gebridauth: gebridauth});
alert(gebridauth);
<?php
if ($admin == true || isset($_POST['gebridauth']) AND $_SESSION['id'] == $_POST['gebridauth']) {
echo "$('#dialog').dialog('open');\n";
echo "var cid = $(this).attr('id');\n";
echo "var datum = cid.substr(0, 10);\n";
echo "var naampos = cid.indexOf('|');\n";
echo "var gebridpos = cid.indexOf('||');\n";
echo "var naam = cid.substring(naampos+1,gebridpos);\n";
echo "var gebrid = cid.substring(gebridpos+2);\n";
echo "$.ajax({\n";
echo "type: \"POST\",\n";
echo "url: \"agenda.php\",\n";
echo "data: naam,\n";
echo "success: function(){\n";
echo "$('#gebruikerinput').html(\"<input type='text' READONLY='' size='35' value='\" + naam +\"'>\");\n";
echo "$('#gebridinput').html(\"<input type='hidden' name='gebridtextbox' value='\" + gebrid + \"'>\");\n";
echo "$('#datuminput').html(\"<input type='text' READONLY='' size='12' name='datum' value='\" + datum + \"'>\");\n";
echo "}\n";
echo "})\n";
echo "return false;\n";
}
?>
});
基本上我想做的是,当我单击 TD 时,在 PHP 的 if 语句中使用“gebridauth”。如果 TD 与登录者相同,则显示该对话框。
The jQuery variable I send to my PHP doesn't work (Or atleast, it doesn't seem to work ). I've sent it to my php with ajax.
Please take a look at it, perhaps you can see the problem:
$('.do').click(function(){
var cid2 = $(this).attr('id');
var gebridauthpos = cid2.indexOf('||');
var gebridauth = cid2.substring(gebridauthpos+2);
$.post("agenda.php", {gebridauth: gebridauth});
alert(gebridauth);
<?php
if ($admin == true || isset($_POST['gebridauth']) AND $_SESSION['id'] == $_POST['gebridauth']) {
echo "$('#dialog').dialog('open');\n";
echo "var cid = $(this).attr('id');\n";
echo "var datum = cid.substr(0, 10);\n";
echo "var naampos = cid.indexOf('|');\n";
echo "var gebridpos = cid.indexOf('||');\n";
echo "var naam = cid.substring(naampos+1,gebridpos);\n";
echo "var gebrid = cid.substring(gebridpos+2);\n";
echo "$.ajax({\n";
echo "type: \"POST\",\n";
echo "url: \"agenda.php\",\n";
echo "data: naam,\n";
echo "success: function(){\n";
echo "$('#gebruikerinput').html(\"<input type='text' READONLY='' size='35' value='\" + naam +\"'>\");\n";
echo "$('#gebridinput').html(\"<input type='hidden' name='gebridtextbox' value='\" + gebrid + \"'>\");\n";
echo "$('#datuminput').html(\"<input type='text' READONLY='' size='12' name='datum' value='\" + datum + \"'>\");\n";
echo "}\n";
echo "})\n";
echo "return false;\n";
}
?>
});
Basically what I want to do, is using "gebridauth" in the if statement of my PHP when I click on a TD. If the TD is the same as the person that's logged in, show the dialog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要对您的
$.post
调用进行回调,现在您只是发送 POST,而不关注服务器发回的内容,因此不会出现对话框。我认为你想要更多类似这样的东西(带有大注释的真实代码):You need a callback on your
$.post
call, right now you're just sending off the POST and not paying any attention to the what the server sends back so no dialog will appear. I think you want something more like this (with real code where the big comment is):我认为您误解了 AJAX 的工作原理。你不能像这样混合 Javascript 和 PHP,因为它们在不同的系统上完全不同的时间运行。如果您要发布到
agenda.php
,您的 PHP 代码需要位于文件agenda.php
中。该文件不应包含 Javascript。您也无法像那样echo
Javascript 作为回报。I think you're misunderstanding how AJAX works. You can't mix Javascript and PHP like that, since they're running at completely different times on different systems. If you're POSTing to
agenda.php
, your PHP code needs to be in the fileagenda.php
. That file should not contain Javascript. You also won't be able toecho
Javascript in return like that.