gpt4 book ai didi

javascript - 将值传递给数据库

转载 作者:行者123 更新时间:2023-11-28 01:09:29 25 4
gpt4 key购买 nike

我想将值从 Ajax 代码传递到数据库,因为这个程序是为了显示用户的详细信息,但是代码在传递值时出错。我现在可以做什么?

function showUser() {
httpRequest = new XMLHttpRequest();

if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
var id = document.getElementById("id").value;
httpRequest.onreadystatechange = alertContents;

httpRequest.open("GET", "http://localhost/cart/guser.php?id=" + id + "&rand=" + , true);
httpRequest.send();
}

function alertContents() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
document.getElementById("txtHint").innerHTML = httpRequest.responseText;
}
}
var id = document.getElementById('id').value;

}
<form>
enter digit :
<input type="text" id="id" name="id" />
<br />
<input type='button' onclick='showUser(this.value)' value='select' />

</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b>
</div>

以下代码用于 guser.php

<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}

table, td, th {
border: 1px solid black;
padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = intval($_GET['q']);

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cart";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


mysqli_select_db('cart',$con);

$sql="SELECT * FROM user_details WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

最佳答案

试试这个,因为你的代码有很多问题新html文件的代码

<!DOCTYPE html>
<html>
<body>

<form>
enter digit :
<input type="text" id="id" name="id" onkeyup='showUser(this.value)'/>
<br />


</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b>
</div>

<script>
function showUser(id) {
httpRequest = new XMLHttpRequest();

if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
else
{

httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
document.getElementById("txtHint").innerHTML = httpRequest.responseText;
}
};
httpRequest.open("GET", "localhost/cart/guser.php?id=" + id, true);
httpRequest.send();
}

}
</script>

</body>
</html>

和新guser.php文件的代码

<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}

table, td, th {
border: 1px solid black;
padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$id = intval($_GET['id']);

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cart";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


mysqli_select_db('cart',$con);

$sql="SELECT * FROM user_details WHERE id = '".$id."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

希望这有帮助!!..评论进一步查询

关于javascript - 将值传递给数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38320555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com