[英]Pass value from window.prompt to server with ajax or javascript
我需要这一重要事项的帮助。 我想通过ajax或javascript将window.prompt中的值传递给服务器(我不知道这2个中的哪个可以很容易..)我现在正在阅读,我必须使用隐藏的输入(如type ==的文本)隐藏)以将数据传递到服务器。
谁能帮我这个忙,因为我不是一个经验丰富的程序员,我自己也做不到? 我尝试了一下,但没有结果。
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>";
?>
我找到了解决方案。 这里是:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.