如何在循环例程中解决“未捕获的ReferenceError:$未定义”

时间:2019-05-11 15:14:11

标签: ajax post dynamic

在此处搜索所有类似的问题,但没有有用的答案。尝试使用ajax将一些数据传递到update.php文件时,出现错误“未捕获的ReferenceError:$未定义”。

我的代码(php)从SELECT(MySQL)获取数据并建立表 while($ row = mysqli_fetch_assoc($ result) ..在每个动态生成的表中,我为每个表都放置了一个编辑字段结果行,以便用户可以查看结果数据,输入一些注释并保存它们。为了简短起见,当用户单击表中的图标时,单击将调用updateNotes()函数,在该函数中ajax将用户编写的注释传递给update.php文件,切掉构建表的所有复杂的嵌套动态结构,此后我将代码的一部分(简化为无动态变量和嵌套的部分)与实际的ajax部分放在一起。

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

<?php
// get all data from a SELECT and "while($row = mysqli_fetch_assoc($result)" I set $count to a number that increases for each row of result
//then show some things
//let's say that every table has a suffix "_n" for all its elements, where "n" is the value of $count
// to focus on my issue, hereafter I don't put the dynamic generation of names, but simply will use the suffix "_n"

echo "<form method='post' id='form_edit_n' name='form_edit_n' action='update.php'>";
    // echo some data from the SELECT query
    // also echo some hidden data from the SELECT query, among them there's "type_a_data_n" hidden field (same id and name)
    echo "<img src='showEditorField.png' id='showEditorField_n' name='showEditorField_n' onclick='editor_n()'/>";
    // to have a different editing field for each recurrence found with the SELECT
echo "</form>";
?>

<script type="text/javascript">

// do some things non relevant as far as my issue is concerned, such as show and hide icons and fields
// among the rest, the function "editor_n()" shows an input field dynamically named let's say 'note_n'
// "editor_n()" also shows the icon "iconEditConfirm.png" with id and name "iconEditConfirm", that has "onclick='updateNotes()'"
// file update.php listens for $_POST["data_type_a"] and $_POST["note"]
// now the following is the function updateNotes()

function updateNotes() {
    var note_x = 'note_n';
    var type_a_data_x = 'type_a_data_n';
    $.ajax({
        type: 'POST',
        url: 'update.php',
        data: {'data_type_a': type_a_data_x, 'note': note_x},
        success:function(){
            // here I don't know how to get the return from update.php
        }
    });
}

// and this is the update.php code

<?php 

// make a connection to db, result is $conn

$note = $_POST["note"];
$data_type_a = $_POST["data_type_a"];

$sqlupdatenote = "UPDATE table SET note='$note' WHERE column='$data_type_a'";
if (mysqli_query($conn, $sqlupdatenote)) {
    echo "Updated note";
    return;
} else {
    echo "Error updating";
    return;
}

mysqli_close($conn);

?>

如何解决“ Uncaught ReferenceError:未定义$”问题? 另外,使用上述语法将数据传递给js var是否正确?

只是更新:查看Firebug中的网络分析,尽管链接是正确的,但jQuery并未加载。

1 个答案:

答案 0 :(得分:-1)

$未定义意味着您错过了对jquery库的引用。此CDN链接将为您提供帮助 “”