Hi,
Before this is marked as a duplicate question, the others do not apply to my situation at all.
So i am making a simple library system using PHP and HTML. My issue at the moment is that when you search for a book, it executes the SQL query fine and it returns correct results. The issue is that to find out more information about the book, you have to click on a button which does some sql queries and what not, and then opens a modal with the information. This, also, works fine but when you do that, because it executes php code, the page refreshes and the search data is gone and you have to search again to get the same results if you need to. Sorry if i explained it badly i will be happy to elaborate if needs be.
I don't want to use AJAX, I can't use node.js so i really have no clue.
The bit that displays the book:
$library = $_COOKIE["userLibrary"];
if(isset($_GET['search'])){
$author = $_GET['author'];
$title = $_GET['booktitle'];
if($author!='' and $title == '' && mysqli_num_rows(mysqli_query($conn,"SELECT * FROM `$library` WHERE Author LIKE '%$author%' ORDER BY Author"))>0){
echo "<table align='center' class='table table-striped table-hover table-bordered'>";
$allBooks = mysqli_query($conn,"SELECT * FROM `$library` WHERE Author LIKE '%$author%' ORDER BY Author");
if(mysqli_num_rows($allBooks)>0){
echo "<td id='book'><strong>Title</td>";
echo "<td id='book'><strong>Author</td>";
echo "<td id='book'><strong>More Information</td>";
while($row = mysqli_fetch_array($allBooks)){
//list($row['Title']) = explode("::", $row['Title']);
$currentCode = $row['InternalBarcode'];
echo "<tr><td id='book'>".$row["Title"]."</td><td id='book'>".$row["Author"]."</td><td id='book' align='center'> <form method='get'><input type='hidden' name='action' value='submit' /><input type='hidden' name='more' value=".$row['InternalBarcode']." /><input type='image' src='More.png' name='more' value='".$row['InternalBarcode']."'></form></td>";
}
echo "</tr></table>";
}
}
else{
echo "<table align='center' class='table table-striped table-hover table-bordered'>";
$allBooks = mysqli_query($conn,"SELECT * FROM `$library` ORDER BY Author");
if(mysqli_num_rows($allBooks)>0){
echo "<td id='book'><strong>Title</td>";
echo "<td id='book'><strong>Author</td>";
echo "<td id='book'><strong>More Information</td>";
while($row = mysqli_fetch_array($allBooks)){
//list($row['Title']) = explode("::", $row['Title']);
$currentCode = $row['InternalBarcode'];
echo "<tr><td id='book'>".$row["Title"]."</td><td id='book'>".$row["Author"]."</td><td id='book' align='center'> <form method='get'><input type='hidden' name='action' value='submit' /><input type='hidden' name='search'><input type='hidden' name='more' value=".$row['InternalBarcode']." /><input type='image' src='More.png' name='more' value='".$row['InternalBarcode']."'></form></td>";
}
echo "</tr></table>";
}
}
}
else{
echo "<table align='center' class='table table-striped table-hover table-bordered'>";
$allBooks = mysqli_query($conn,"SELECT * FROM `$library` ORDER BY Author");
if(mysqli_num_rows($allBooks)>0){
echo "<td id='book'><strong>Title</td>";
echo "<td id='book'><strong>Author</td>";
echo "<td id='book'><strong>More Information</td>";
while($row = mysqli_fetch_array($allBooks)){
//list($row['Title']) = explode("::", $row['Title']);
$currentCode = $row['InternalBarcode'];
echo "<tr><td id='book'>".$row["Title"]."</td><td id='book'>".$row["Author"]."</td><td id='book' align='center'> <form method='get'><input type='hidden' name='action' value='submit' /><input type='hidden' name='more' value=".$row['InternalBarcode']." /><input type='image' src='More.png' name='more' value='".$row['InternalBarcode']."'></form></td>";
}
echo "</tr></table>";
}
The PHP code for the search form:
if(isset($_GET['action'])){
$barcode = $_GET['more'];
//unset($_GET['action']);
$bookTitleA = mysqli_query($conn, "SELECT Title FROM `$library` WHERE InternalBarcode = '$barcode'");
$bookTitle = mysqli_fetch_array($bookTitleA)[0];
$desc = mysqli_fetch_array(mysqli_query($conn, "SELECT Description FROM `$library` WHERE Title = '$bookTitle'"))[0];
$onLoanA = mysqli_query($conn, "SELECT OnLoan FROM `$library` WHERE InternalBarcode = '$barcode'");
$onLoanQuery = mysqli_fetch_array($onLoanA)[0];
$onLoanTo = mysqli_fetch_array(mysqli_query($conn, "SELECT username FROM users where id = '$onLoanQuery'"))[0];
$anyOthers = mysqli_query($conn, "SELECT * from `$library` WHERE Title = '$bookTitle' and OnLoan = '0'");
if($onLoanQuery == '0'){
global $onLoan;
$onLoan = 'This book is currently not on loan';
}
elseif(mysqli_num_rows($anyOthers)>0){
global $onLoan;
$onLoan = "This book is currently on loan to ".$onLoanTo."<br>There are ".mysqli_num_rows($anyOthers)." more coppies available";
}
else{
global $onLoan;
$onLoan = 'This book is currently on loan to '.$onLoanTo."<br>There are no more coppies available.";
}
echo "<script>BookModal(`$bookTitle`, `$barcode`, `$desc`, '$onLoan')</script>";
}
?>
The Search Form:
<h2 align="center">Search:</h2>
<form action="" method="get">
<div class="form-group">
<label for="author">Author:</label>
<input type="text" class="form-control" name="author" id="author" placeholder="Name of Author"/>
</div>
<div class="form-group">
<label for="booktitle">Title:</label>
<input type="text" class="form-control" name="booktitle" id="booktitle" placeholder="Title of Book"/>
</div>
<input class="form-control" type="submit" name="search" id="search" value="Search">
</form>
Heres the JS if you need it:
function ModalOpen(modalName){
$(modalName).modal('show');
}
function BookModal(title, barcode, description, available){
document.getElementById('bookTitle').innerHTML=title;
document.getElementById('barcode').innerHTML=barcode;
document.getElementById('desc').innerHTML=description;
document.getElementById('available').innerHTML=available;
$('#myModal').modal('show');
}
function ModalClose(modalName){
$(modalName).modal('hide');
}
I appreciate all your help!
Update
I have decided to use AJAX, Which works, sorta. It works fine for the first row in the table, but for the rest, the modal will still pop up but it shows the same data as the book at the top of the table. It doesn't matter which one i click first, it still only shows the data from the first row in the table.
The bit that displays the books (php):
include 'connect.php';
$library = $_COOKIE["userLibrary"];
if(isset($_GET['search'])){
$author = $_GET['author'];
$title = $_GET['booktitle'];
if($author!='' and $title == '' && mysqli_num_rows(mysqli_query($conn,"SELECT * FROM `$library` WHERE Author LIKE '%$author%' ORDER BY Author"))>0){
echo "<table align='center' class='table table-striped table-hover table-bordered'>";
$allBooks = mysqli_query($conn,"SELECT * FROM `$library` WHERE Author LIKE '%$author%' ORDER BY Author");
if(mysqli_num_rows($allBooks)>0){
echo "<td class='book'><strong>Title</td>";
echo "<td class='book'><strong>Author</td>";
echo "<td class='book'><strong>More Information</td>";
while($row = mysqli_fetch_array($allBooks)){
//list($row['Title']) = explode("::", $row['Title']);
$currentCode = $row['InternalBarcode'];
echo "<tr><td id='book'>".$row["Title"]."</td><td id='book'>".$row["Author"]."</td><td id='book' align='center'> <form method='get'><input type='button' class='more' src='More.png' value='".$row['InternalBarcode']."'></form></td>";
}
echo "</tr></table>";
}
}
else{
echo "<table align='center' class='table table-striped table-hover table-bordered'>";
$allBooks = mysqli_query($conn,"SELECT * FROM `$library` ORDER BY Author");
if(mysqli_num_rows($allBooks)>0){
echo "<td id='book'><strong>Title</td>";
echo "<td id='book'><strong>Author</td>";
echo "<td id='book'><strong>More Information</td>";
while($row = mysqli_fetch_array($allBooks)){
//list($row['Title']) = explode("::", $row['Title']);
$currentCode = $row['InternalBarcode'];
echo "<tr><td id='book'>".$row["Title"]."</td><td id='book'>".$row["Author"]."</td><td id='book' align='center'> <form method='get'><input type='button' class='more' src='More.png' value='".$row['InternalBarcode']."'></form></td>";
}
echo "</tr></table>";
}
}
}
else{
echo "<table align='center' class='table table-striped table-hover table-bordered'>";
$allBooks = mysqli_query($conn,"SELECT * FROM `$library` ORDER BY Author");
if(mysqli_num_rows($allBooks)>0){
echo "<td id='book'><strong>Title</td>";
echo "<td id='book'><strong>Author</td>";
echo "<td id='book'><strong>More Information</td>";
while($row = mysqli_fetch_array($allBooks)){
//list($row['Title']) = explode("::", $row['Title']);
$currentCode = $row['InternalBarcode'];
echo "<tr><td id='book'>".$row["Title"]."</td><td id='book'>".$row["Author"]."</td><td id='book' align='center'> <form method='get'><input type='button' class='more' src='More.png' value='".$row['InternalBarcode']."'></form></td>";
}
echo "</tr></table>";
}
}
The PHP code that the AJAX executes:
<?php
if(isset($_POST["barcode"])){
$library = $_COOKIE["userLibrary"];
include 'connect.php';
$barcode = $_POST['barcode'];
$bookTitleA = mysqli_query($conn, "SELECT Title FROM `$library` WHERE InternalBarcode = '$barcode'");
$bookTitle = mysqli_fetch_array($bookTitleA)[0];
$desc = mysqli_fetch_array(mysqli_query($conn, "SELECT Description FROM `$library` WHERE Title = '$bookTitle'"))[0];
$onLoanA = mysqli_query($conn, "SELECT OnLoan FROM `$library` WHERE InternalBarcode = '$barcode'");
$onLoanQuery = mysqli_fetch_array($onLoanA)[0];
$onLoanTo = mysqli_fetch_array(mysqli_query($conn, "SELECT username FROM users where id = '$onLoanQuery'"))[0];
$anyOthers = mysqli_query($conn, "SELECT * from `$library` WHERE Title = '$bookTitle' and OnLoan = '0'");
if($onLoanQuery == '0'){
global $onLoan;
$onLoan = 'This book is currently not on loan';
}
elseif(mysqli_num_rows($anyOthers)>0){
global $onLoan;
$onLoan = "This book is currently on loan to ".$onLoanTo."<br>There are ".mysqli_num_rows($anyOthers)." more coppies available";
}
else{
global $onLoan;
$onLoan = 'This book is currently on loan to '.$onLoanTo."<br>There are no more coppies available.";
}
$thing = "BookModal(`$bookTitle`, `$barcode`, `$desc`, '$onLoan'";
echo json_encode(['booktitle' => $bookTitle,
'barcode' => $barcode,
'desc' => $desc,
'onloan' => $onLoan], JSON_FORCE_OBJECT);
exit();
}
?>
The AJAX code:
$(function(){
$('.more').on('click', function(){
var barcodeNum = $('input[class="more"]').val();
$.ajax({
type: 'post',
url: '',
data: {barcode: barcodeNum},
success: function(data){
let allVar = JSON.parse(data);
let booktitle = allVar.booktitle;
let desc = allVar.desc;
let onloan = allVar.onloan;
let barcode = allVar.barcode;
document.getElementById('bookTitle').innerHTML=booktitle;
document.getElementById('barcode').innerHTML=barcode;
document.getElementById('desc').innerHTML=desc;
document.getElementById('available').innerHTML=onloan;
$('#myModal').modal('show');
},
error: function(data){
console.log('error');
alert('error');
}
});
});
})