donglan6777 2019-02-26 23:21
浏览 43
已采纳

PHP变量没有执行

So, I am preparing my school project, I am struggling to find out, What's the error in this code ? When I remove the $sql = "UPDATE wp_assign_cards_numbers SET numbers "; code is executing perfectly, when I use the above line. The Code is not executing, the blank is displayed,Even i am not aware where to check error ?? So I am not able to trace the error also.

function mySubmission(){

    alert("Submission..");

    var grid = document.getElementById("myTable");

        //Reference the CheckBoxes in Table.
        var checkBoxes = grid.getElementsByTagName("input");
        var card = "";
        var number ="";

        //Loop through the CheckBoxes.
        for (var i = 0; i < checkBoxes.length; i++) {
            if (checkBoxes[i].checked === false) {
                var row = checkBoxes[i].parentNode.parentNode;

                card = row.cells[0].innerHTML;
                number = row.cells[1].innerHTML;

                document.cookie = "myJavascriptVarCard = " + card; 
                document.cookie = "myJavascriptVarNumber = " + number; 

               <?php
                $link = mysqli_connect($hostname, $username, $password, $database);
                $card= $_COOKIE['myJavascriptVarCard'];
                $number = $_COOKIE['myJavascriptVarNumber'];    
                $sql = "UPDATE wp_assign_cards_numbers SET numbers ";


               ?>

            }
        }

}
  • 写回答

1条回答 默认 最新

  • doutenglou6588 2019-02-26 23:46
    关注

    The short answer here is that you cannot mix PHP and JS like this without some kind of interface bridging the two.

    Check out these basic examples of XHR requests: https://blog.garstasio.com/you-dont-need-jquery/ajax/

    But the basic example is here (replace the php chunk you have with this):

    var xhr = new XMLHttpRequest();
    
    xhr.open('POST', '/your-php-script.php');
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onload = function() {
        if (xhr.status === 200 && xhr.responseText !== newName) {
            alert('Something went wrong. ' + xhr.responseText);
        }
        else if (xhr.status !== 200) {
            alert('Request failed.  Returned status of ' + xhr.status);
        }
    };
    xhr.send(encodeURI('card=' + card + '&number=' + number));
    

    And at the root of your directory you have your-php-script.php with some basic handler:

    <?php
    if(isset($_POST['card'])){
        $card = $_POST['card'];
        $number = $_POST['number'];
    
        // do something with these vars
    }
    

    However, we're not done. For one, this xhr request is in a loop. You will want to collect those values and pass them as one request outside that loop. Your php example code is in pretty bad shape as it is. We have no idea if you're setting your mysqli connection correctly, and the query itself is definitely not going to work that way. But those are for another question. A little googling will help you with that part.

    This answer is simply to illustrate how you communicate with a php handler from javascript. You would still need to make sure those inputs are set and something is calling the mySubmission function, plus make sure the js, html, and php are organized in a sane way. Make use of console logs and php logs to help debug issues as you go along.

    Also, if this has anything to do with credit cards, don't approach it this way. Never store credit info in a cookie, local storage, or anything else that can be intercepted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊