简体   繁体   English

php if语句不适用于jquery变量

[英]php if statement doesn't work with jquery variable

The jQuery variable I send to my PHP doesn't work (Or atleast, it doesn't seem to work ). 我发送到PHP的jQuery变量不起作用(或者,至少它似乎不起作用)。 I've sent it to my php with ajax. 我已经用ajax将其发送到我的php中。

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. 基本上,我想做的就是在单击TD时在PHP的if语句中使用“ gebridauth”。 If the TD is the same as the person that's logged in, show the dialog. 如果TD与登录的人相同,则显示对话框。

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. 您需要在$.post调用上进行回调,现在您只是发送POST,而无需关注服务器发送回的内容,因此不会出现对话框。 I think you want something more like this (with real code where the big comment is): 我认为您想要更多类似的东西(带有真正注释的代码):

$.post("agenda.php", {gebridauth: gebridauth}, function(data, textStatus, jqXHR) {
    // If the server sent back a "show the dialog" value in data then
    // show the dialog and all the other stuff that's currently in a
    // bunch of PHP echo calls.
});

I think you're misunderstanding how AJAX works. 我认为您误会了AJAX的工作原理。 You can't mix Javascript and PHP like that, since they're running at completely different times on different systems. 您不能像这样将Javascript和PHP混合使用,因为它们在不同系统上的运行时间完全不同。 If you're POSTing to agenda.php , your PHP code needs to be in the file agenda.php . 如果您要发布到agenda.php ,则您的PHP代码必须位于文件“ agenda.php That file should not contain Javascript. 该文件不应包含的JavaScript。 You also won't be able to echo Javascript in return like that. 这样您也将无法echo Javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM