我正在尝试将数据发布到运行 MySQL 的服务器。我正在运行以下代码,但控制台没有显示任何错误。能否请您看一下,如果您发现问题,请告诉我?任何帮助将不胜感激,因为我找不到类似的指南。
我希望我的代码做的是:我希望用户通过 window.prompt 提供 4 个值,这些值将存储在变量中: user_id
、book_id
、game_id
、site_id
。 那么这4个值一定要存在我的数据库中。
index.html
<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() {
$("#user_id").val(prompt("Give the UserId:"))
$("#book_id").val(prompt("Give the BookId:"))
$("#game_id").val(prompt("Give the GameId:"))
$("#site_id").val(prompt("Give the SiteId:"))
}
</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><input type="button" value="Load" id="load" onclick="load2()" class="button12" /></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>
</body>
</html>
mine2.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'];
//if (mysql_query("INSERT INTO `components` (`user_id`, `book_id`, `game_id`, `site_id`) VALUES ('".$user_id."','".$book_id."','".$game_id."', '".$site_id."',)"));
mysqli_query($link ,""INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('".$user_id."','".$book_id."','".$game_id."', '".$site_id."')"") ;
// Attempt insert query execution
//$sql = "INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('6', '6', '6', '6')";
//if(mysqli_query($link, $sql)){
//} else{
//}
// Close connection
mysqli_close($link);
?>
伙计们,我也发现了这个问题:JS Prompt to PHP Variable 我应该使用那里的东西吗?
最佳答案
<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() {
$("#user_id").val(prompt("Give the UserId:"))
$("#book_id").val(prompt("Give the BookId:"))
$("#game_id").val(prompt("Give the GameId:"))
$("#site_id").val(prompt("Give the SiteId:"))
}
</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><input type="button" value="Load" id="load" onclick="load2()" class="button12" /></td>
</tr>
<tr>
<td>
<form name="SaveGame" id="SaveGame" method="post" action="http://localhost/learnlaravel/oops-example/mine-2.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) {
console.log(data);
return false;
alert("The game has been saved!"); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
</script>
</body>
</html>
<?php
if(!empty($_POST)) {
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "homestead");
// 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'];
mysqli_query($link ,"INSERT INTO components (user_id, book_id, game_id, site_id) VALUES ('".$user_id."','".$book_id."','".$game_id."', '".$site_id."')") ;
// Close connection
mysqli_close($link);
echo 'Y'; exit;
}
这对我有用,你在查询中有错误,只需删除“”。$site_id。“的最后一端。
关于javascript - 我想将数据从 window.prompt 发送到我的服务器,但可能缺少某些东西 (javascript+php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52109717/