在这件重要的事情上我需要帮助。 我想通过ajax或javascript将window.prompt中的值传递给服务器(我不知道这2个中的哪个可以很容易..) 我现在正在阅读,为了将数据传递到服务器,我必须使用某种形式的隐藏输入(如type = hidden的文本)。
任何人都可以帮助我,因为我不是一个经验丰富的程序员,我自己不能做到?我尝试了一下,但没有结果。.
selectfrm.html:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="select.js"></script>
</head>
<body>
<button id="button2" onclick="myFunction()">Select by ID</button>
<div id="content"></div>
</body>
</html>
select.js
$(document).ready(function(){
$("#button2").click(function() {
function show_selected() {
var id=$("#id").val();
$.ajax({
type: "POST",
url: "select.php",
data:{action:id},
success: function (data) {
$("#content").html(data);
$("#button").hide();
}
});
}
show_selected();
});
});
function myFunction()
{
var id = window.prompt("Give the userid");
}
select.php
<?php
$link=mysqli_connect("localhost", "root", "", "mysql3");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL:" .mysqli_connect_error();
$action=$_POST["action"];
$query = "SELECT * FROM components WHERE user_id = '$action'";
$show = mysqli_query($link, $query) or die ("Error");
echo "<table border='2px'><tr><td>name_id</td><td>name</td><td>age</td></tr>";
while ($row = mysqli_fetch_array($show)) {
echo "<tr><td>" .$row['book_id'] ."</td><td>" .$row['game_id'] ."</td><td>" .$row['site_id'] ."</td></tr>";
}
echo "</table>";
?>
答案 0 :(得分:0)
我找到了解决方案。在这里:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button2").click(function() {
var ut = prompt("Some");
function show_selected() {
var id=$("#id").val();
$.ajax({
type: "POST",
url: "select.php",
data:{action:ut},
success: function (data) {
$("#content").html(data);
$("#button").hide();
}
});
}
show_selected();
});
});
testAjax(function(output){
window.alert("ut");// here you use the output
});
</script>
</head>
<body>
<br>
<input type="text" id="id" />
<button id="button2">Select by ID</button>
<div id="content"></div>
</body>
</html>