我想在数据插入之前在 php 中使用 java 脚本警报。我能够将数据插入数据库,但 echo 无法完成它的工作。这可能只是一个简单的错误,但我无法发现它:(/// Java script/// send data for insertion$.get("tute7_add.php",{unit_code:unit_code,unit_name:unit_name,lecturer:lecturer,semester:semester}).done(function(data){ alert('You have added a unit successfully!'); });tute7_add.php<?php // I Can't send alert!! echo "<script type='text/javascript'> alert('Don\'t have a record'); </script>"; $code= $_GET['unit_code']; $name= $_GET['unit_name']; $lecturer= $_GET['lecturer']; $semester= $_GET['semester']; $query = "INSERT INTO units (`unit_code`,`unit_name`,`lecturer`,`semester`) VALUES ('$code','$name','$lecturer','$semester');"; $mysqli->query($query); ?>
1 回答

慕的地10843
TA贡献1785条经验 获得超8个赞
您正在使用 Ajax 请求 URL。
该data变量将包含一大块 HTML,其中包括脚本元素。
您没有使用 的值做任何事情data。你只是忽略它。
通常在处理 Ajax 时,您将以 JSON 之类的格式返回原始数据,而不是使用 HTML。然后您可以使用客户端代码处理它。
例如
header("Content-Type: application/json");
if (1) { # Use a real condition here
echo json_encode([ "error" => "Don't have a record" ]);
exit;
} else {
# do database stuff and then…
echo json_encode([ "success" => "Something something" ]);
exit;
}
随着:
$.get("tute7_add.php", {
unit_code,
unit_name,
lecturer,
semester,
}).done(function (data) {
if (done.success) {
alert("You have added a unit successfully!");
} else {
alert(done.error);
}
});
- 1 回答
- 0 关注
- 121 浏览
添加回答
举报
0/150
提交
取消