php if 语句不适用于 jquery 变量

发布于 2024-11-16 21:38:52 字数 2025 浏览 0 评论 0原文

我发送到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-11-23 21:38:52

您需要对您的 $.post 调用进行回调,现在您只是发送 POST,而不关注服务器发回的内容,因此不会出现对话框。我认为你想要更多类似这样的东西(带有大注释的真实代码):

$.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.
});

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):

$.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.
});
羁绊已千年 2024-11-23 21:38:52

我认为您误解了 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 file agenda.php. That file should not contain Javascript. You also won't be able to echo Javascript in return like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文