在以下问题上,我需要您的帮助: 提交表单时,我希望在用户侧显示一条消息。 该代码运行正常,但是成功后不会向用户显示警告消息。 我想念什么吗? 我认为我的脚本位置有问题。我必须将其放置在代码中的其他位置还是出现一些错误?
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function save3() {
var pn = 4;
$("#user_id").val(prompt("Give the User Id:"))
$("#book_id").val(prompt("Give the Book Id:"))
$("#game_id").val(prompt("Give the Game Id:"))
$("#site_id").val(pn)
}
function load2() {
}
</script>
</head>
<body>
<p align="center">example</p>
<table align="center" width="730">
<tr>
<td align="center">
<div>
<table class="blueTable" style="float: left">
<thead>
<tr>
<th colspan="1"><u>Menu</u></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="button" value="New" id="new" onclick="new1()" class="button12" /></td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<form name="SaveGame" id="SaveGame" method="post" action="http://127.0.0.1/PHP/mine2.php" enctype="multipart/form-data">
<input type="submit" value="Save" id="save" onclick="save3()" class="button12" />
<input type="hidden" name="user_id" id="user_id">
<input type="hidden" name="book_id" id="book_id">
<input type="hidden" name="game_id" id="game_id">
<input type="hidden" name="site_id" id="site_id">
</form>
<script>
$("#SaveGame").submit(function(e) {
var form = $(this);
var url = form.attr('action');
$.ajax({
type: "POST",
url: url,
data: form.serialize(), // serializes the form's elements.
success: function(data) {
alert("The game has been saved!"); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
</script>
</td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</table>
</body>
</html>
和我的php代码:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];
// Attempt insert query execution
if ($result = $link->query("INSERT INTO `components` (`user_id`, `book_id`, `game_id`, `site_id`) VALUES ('$user_id','$book_id','$game_id', '$site_id')") == true)
echo json_encode($result);
else {
echo "Error";
}
//$qry ="INSERT INTO `components` (`user_id`, `book_id`, `game_id`, `site_id`) VALUES ('".$user_id."','".$book_id."','".$game_id."', '".$site_id."')";
// if($qry){
// mysqli_query($link,$qry);
// }
// Close connection
mysqli_close($link);
?>