我想在數據插入之前在 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 關注
- 120 瀏覽
添加回答
舉報
0/150
提交
取消