简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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