将值传递给数据库

时间:2016-07-12 05:18:31

标签: javascript php html ajax

我想将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>

3 个答案:

答案 0 :(得分:1)

试试这个,因为你的代码有很多问题 新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>

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

答案 1 :(得分:0)

您应该在, httpRequest.open("GET", "http://localhost/cart/guser.php?id="+id+"&rand="+,true);之前从true jsut删除rand=true以使+

或从&rand="

之后的网址中删除$(function(){ $('#getQuote').click(function (e){ e.preventDefault(); $.ajax({ headers: { 'X-Mashape-Key': 'nrXbQkfuWEmshxvDCunSMptEn0M0p1jHWCijsnX9Ow18j8TXus', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' }, method:'POST', dataType: 'json', url: 'https://andruxnet-random-famous-quotes.p.mashape.com/', success: function(response) { var ape = response//remove the parsing var quoteText = ape.quote; var quoteAuthor = ape.author; $(".quote").html(quoteText); $(".author").html(quoteAuthor);} }); }); });

答案 2 :(得分:0)

试试这个:

添加头部:<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>

您必须在输入框中键入人员ID,并在texthint div中输入用户信息:

你将从post发送用户的guser.php中的id并运行query.and回显用户信息到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>