I am trying to get details of products to be displayed on my webpage in a specific format by submitting an id number through a form. But no data is being displayed on submitting the query. I want the latest retrieved data to be appended below the already existing data on the same page from where the form was submitted.
This is my 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(); ?>
This is my 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;
This is 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());
}
?>
FYI - I am using a php script that maintains a user login session that connects to a database and keeps a user logged in until he signs out. Is there by any an interference between the two scripts: one that maintains user sessions and another that accesses database for getting product details. Thanks in advance.
You are not sending any data via ajax;
I'm supposing that you'have included jquery correctly;
Then try this
$(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);
}
});
});
});
Good luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.