我试图通过表单提交身份证号码,以特定格式获取要在我的网页上显示的产品的详细信息。但是在提交查询时没有显示任何数据。我希望将最新检索到的数据附加在提交表单的同一页面上现有数据下面。
这是我的home.php:
<?php
ob_start();
session_start();
require_once 'dbconnect.php';
// if session is not set this will redirect to login page
if( !isset($_SESSION['user']) )
{
header("Location: index.php");
exit;
}
// select loggedin users detail
$res=mysqli_query($conn, "SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysqli_fetch_array($res);
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title> ShopNGo </title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).ready(function()
{
$("#scan").on('click',function()
{
var id =$("#id").val();
$.ajax(
{
type: "POST",
url: "getdata.php",
data: {id: id},
success: function(data)
{
$('.results').append(data);
}
});
});
});
</script>
</head>
<body>
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-user"></span> Hi' <?php echo $userRow['userEmail']; ?> <span class="caret"></span></a>
<a href="logout.php?logout"><span class="glyphicon glyphicon-log-out"></span> Sign Out</a>
<header id="header">
<div class="container">
<form name="products" method="POST" action="">
<br><br>
<input type="submit" name="scan" id="scan" value="SCAN">
<br><br><br>
<input type="text" name="id" id="id">
</form>
</div>
</header>
<div class="main">
<table border="0">
<div class="results" id="results">
</div>
</table>
</div>
</body>
</html>
<?php ob_end_flush(); ?>
这是我的getdata.php:
$query = "SELECT name, price, img FROM product WHERE id = $_POST[id]";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$element = "<tr> <table border='0'> <tr>";
$element .= "<img src='$row[img]'>";
$element .= "<br>";
$element .= $row["name"];
$element .= "<br>";
$element .= $row["price"];
$element .= "</tr> </table> </tr>";
}
}
echo $element;
这是dbconnect.php
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE );
// but I strongly suggest you to use PDO or MySQLi.
$servername = "localhost";
$username = "#usernasme";
$password = "#password";
$dbname = "#dbname";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if ( !$conn )
{
die("Connection failed : " . mysqli_error());
}
?>
仅供参考 - 我正在使用一个php脚本来维护一个连接到数据库的用户登录会话,并让用户登录直到他退出。两个脚本之间是否存在任何干扰:一个维护用户会话,另一个访问数据库以获取产品详细信息。提前谢谢。
答案 0 :(得分:0)
您没有通过ajax发送任何数据;我假设你已经正确地包含了jquery;
然后试试这个
$(document).ready(function(){
$("#scan").on('click',function(){
var id =$("#id").val();
$.ajax({
type: "POST",
url: "getdata.php",
data: {id: id},
success: function(data){
$('.results').append(data);
}
});
});
});
祝你好运