Javascript 将值传递给数据库

Javascript 将值传递给数据库,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我想把Ajax代码中的值传递给数据库,因为这个程序要显示用户的详细信息,但是传递值时代码中有一个错误。我现在能做什么 函数showUser(){ httpRequest=新的XMLHttpRequest(); 如果(!httpRequest){ 警报(“放弃:(无法创建XMLHTTP实例”); 返回false; } var id=document.getElementById(“id”).value; httpRequest.onreadystatechange=alertContents;

我想把Ajax代码中的值传递给数据库,因为这个程序要显示用户的详细信息,但是传递值时代码中有一个错误。我现在能做什么

函数showUser(){
httpRequest=新的XMLHttpRequest();
如果(!httpRequest){
警报(“放弃:(无法创建XMLHTTP实例”);
返回false;
}
var id=document.getElementById(“id”).value;
httpRequest.onreadystatechange=alertContents;
httpRequest.open(“GET”http://localhost/cart/guser.php?id=“+id+”&rand=“+,true);
httpRequest.send();
}
函数alertContents(){
if(httpRequest.readyState==XMLHttpRequest.DONE){
if(httpRequest.status==200){
document.getElementById(“txtHint”).innerHTML=httpRequest.responseText;
}
}
var id=document.getElementById('id')。值;
}

输入数字:


人员信息将在此处列出。。。
或者您应该从
httpRequest.open(“GET”)中删除
http://localhost/cart/guser.php?id=“+id+”&rand=“+,true);
jsut-before
true
使
rand=true

或者在
&rand=“

之后从url中删除
+
,尝试以下操作:

加载项标题:

您必须在inputbox中键入人员id,在texthint div中键入用户信息:

您将从post中获取用户在guser.php中的id,然后运行query.php并将用户信息回送到guser.php文件中

<script type="text/javascript">
    function showUser($id){

    $("#email").keyup(function(){
        $.ajax({
        type: "POST",
        url: "http://localhost/cart/guser.php",
        data:'keyword='$id,
        success: function(data){
            $("#txtHint").html(data);

         }
        });
    });
}
   </script>

函数showUser($id){
$(“#email”).keyup(函数(){
$.ajax({
类型:“POST”,
url:“http://localhost/cart/guser.php",
数据:'keyword='$id,
成功:功能(数据){
$(“#txtHint”).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>
<!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>

希望这有帮助!!…对进一步的查询进行注释

有很多问题。按钮没有值,因此
此值
将为空。您正在调用带有参数的
showUser()
,但它不带任何参数。
“&rand”之后没有任何内容"+
。在
alertContents
中,您设置了
id
变量,但从未使用过它。同时使用
表单
ajax
没有任何意义,因为表单提交时页面会刷新,而且
提交
按钮也没有任何值。@saurabh sharma您可以上传
g的代码吗user.php
文件,然后我将在答案中上载完整的更正代码。@VikasBhandari是的,当然……你可以在问题中看到这一点now@saurabhsharma在
guser.php
中,
$\u GET['q']
是什么。它不应该是
$\u GET['id']
如果在guser.php中没有使用变量,为什么要传递
rand
变量。他为什么要删除逗号?这需要将URL与
异步
标志分开。否。在这种情况下,他应该删除
+
再次检查他的URL
”http://localhost/cart/guser.php?id=“+id+”&rand=”+,true
rand=“
之后有一个
+
没有指向任何地方。我想应该是
rand=true
来生成随机数据或其他什么是的,在
+
之后缺少一些东西,但是
true
open()
的第三个参数。