我有一些代码,需要在其中更新调用另一个PHP文件的表(MySQL)的列,而不必离开某些表可能允许内联编辑的页面。

我在页面的php回显中有一点,可以单击一个图标来保存输入。此时的代码是:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<?php
$sql = "SELECT * FROM table WHERE a_column='certain_value'";
if (mysqli_query($conn, $sql)) {
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            $note = $row["note"];
            $code = $row["code"];
        }
    }
}
// some tabled elements not relevant for the issue
echo "<input type='text' id='note_1' name='note_1' value=$note readonly>";
echo "<input type='text' id='new_note' name='new_note'>";
echo "<img src='icon_to_click.png' id='icon_to_click' name='icon_to_click' >";
?>

<script type="text/javascript">

$(document).ready(function() {
    $('#icon_to_click').click(function() {
        var note_orig = document.getElementById('note_1').value;
        var code_val = '<?php echo "$code" ?>';
        var note_new = document.getElementById('new_note').value;
        if (note_new != note_orig) {
            $.ajax({
                type: 'POST',
                url: 'update_notes.php',
                data: {'code': code_val, 'note': note_new},
                success: function(response){
                    document.getElementById('note_1').value = note_new;
                }
            });
        }
    });
});


update_notes.php的相关代码是:

<?php

// connection

$unsafe_note = $_POST["note"];
$code = $_POST["code"];
require "safetize.php"; // the user input is made safe
$note = $safetized_note; // get the output of safetize.php

$sqlupdate = "UPDATE table SET note='$note' WHERE code='$code'";
if (mysqli_query($conn, $sqlupdate)) {
    echo "Note updated";
} else {
    echo "Problem in updating";
}

// close connection

?>


现在,当我运行代码并查看工具时,它给了我错误:Uncaught ReferenceError:$未定义,将错误链接到先前js代码的这一行:

$(document).ready(function() {


那么,我该如何解决?

最佳答案

这意味着您尝试在Javascript代码中使用Jquery而不调用Jquery Library或在未完全加载库的情况下调用代码。

我注意到 :


您尚未关闭脚本标签
您使用Jquery,因此可以使用$('#id_name')通过ID而不是document.getElementById('note_1')选择元素
通过使用Element.val()而不是Element.value获取元素值


尝试像这样编辑您的代码

<?php
    $sql = "SELECT * FROM table WHERE a_column='certain_value'";
    if (mysqli_query($conn, $sql)) {
        $result = mysqli_query($conn, $sql);
        if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
                $note = $row["note"];
                $code = $row["code"];
            }
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Some title</title>
    </head>
    <body>
        <form method="post" accept-charset="UTF-8">
            <input type='text' id='note_1' name='note_1' value=<?= $code ?> readonly>";
            <input type='text' id='new_note' name='new_note'>";
            <img src='icon_to_click.png' id='icon_to_click' name='icon_to_click' >";
        </form>
        <script>
            $(document).ready(function() {
                $('#icon_to_click').click(function() {
                    var note_orig = $('#note_1').val();
                    var code_val = '<?= $code ?>';
                    var note_new = $('#new_note').val();
                    if (note_new != note_orig) {
                        $.ajax({
                            type: 'POST',
                            url: 'update_notes.php',
                            data: {'code': code_val, 'note': note_new},
                            success: function(response){
                                $('#note_1').val() = note_new;
                            }
                        });
                    }
                });
            });
        </script>
    </body>
</html>

关于javascript - 如何解决此“未捕获的ReferenceError:$未定义”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56099116/

10-12 13:32