How to not search the blank in query - php

When I go to this page It's already search the blank string in the variable before pick the value in option. I just want to search the value when the button is click not automatically search when the user go to this page. And Also there's a problem on my "asc" in query.
If I go to this page. It's already search and show up the blank values
Already Search the Blank
And I insert this code $asc but as you can see in the picture it shows up an error but If I pick in the select option the error will vanish and show the value.
this Ascending to Descending
Please Enter Details Below
<div class="nice" style=" width:20%; margin-left:10%; z-index:100px;">
<div class="control-group">
<label class="control-label" for="inputPassword">Search Catalog:</label>
<div class="controls">
<select name="catalogs">
<option></option>
<?php $result = mysqli_query($dbcon,"select * from book_catalog")or die(mysqli_error());
while ($row=mysqli_fetch_array($result)){ ?>
<option value="<?php echo $row['catalog']; ?>"><?php echo $row['catalog']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Author:</label>
<div class="controls">
<select name="status">
<option></option>
<option>Local</option>
<option>Foreign</option>
</select>
</div>
</div>
<div class="control-group" style="margin-left:210%; margin-top:-150px;">
<label class="control-label" for="inputPassword">Ascending-Descending</label>
<div class="controls">
<select name="asc" >
<option></option>
<option value="acc_id">Accession Number</option>
<option value="author">Author</option>
<option value="book_title">Book Title</option>
<option value="publisher_name">Publisher Name</option>
<option value="copyright_year">Copyright Year</option>
<option value="date_added">Date Added</option>
</select>
</div>
</div>
</div>
<input type="submit" name="submit" value="Search" style="margin-left:43%; margin-top:10%;" /> <br>
</form>
</div>
<?php
$selected_stat = "";
$selected_val = "";
$asc =""; // Storing Selected Value In Variable
if(isset($_POST['submit'])){
$selected_val = $_POST['status'];
$selected_stat = $_POST['catalogs'];
$asc = $_POST['asc']; // Storing Selected Value In Variable
}
?>
<br>
<thead>
<tr>
<th>Acc No.</th>
<th>Call Number</th>
<th>Author</th>
<th>Author Category</th>
<th>Book Title</th>
<th>Category</th>
<th>Book Copies</th>
<th>Publisher Name</th>
<th>Copyright Year</th>
<th>Date Added</th>
<th class="action">Action</th>
</tr>
</thead>
<tbody>
<?php $user_query=mysqli_query($dbcon,"select * from book where catalog = '$selected_stat' OR status = '$selected_val' ORDER BY book.$asc ASC")or die(mysqli_error($dbcon));
while($row=mysqli_fetch_array($user_query)){
$id=$row['book_id'];
?>
<tr class="del<?php echo $id ?>">
<td><?php echo $row['acc_id']; ?></td>
<td><?php echo $row['callnum']; ?></td>
<td><?php echo $row['author']; ?> </td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['book_title']; ?> </div></td>
<td><?php echo $row ['catalog']; ?> </td>
<td class="action"><?php echo $row['book_copies']; ?> </td>
<td><?php echo $row['publisher_name']; ?></td>
<td><?php echo $row['copyright_year']; ?></td>
<td><?php echo $row['date_added']; ?></td>
<?php include('toolttip_edit_delete.php'); ?>
<td class="action">
<a rel="tooltip" title="Delete" id="<?php echo $id; ?>" href="#delete_book<?php echo $id; ?>" data-toggle="modal" class="btn btn-danger"><i class="icon-trash icon-large"></i></a>
<?php include('delete_book_modal.php'); ?>
<a rel="tooltip" title="Edit" id="e<?php echo $id; ?>" href="edit_book.php<?php echo '?id='.$id; ?>" class="btn btn-success"><i class="icon-pencil icon-large"></i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Sorry for my bad english

When the SQL statement throws an error the best thing to do is log it or dump it to the screen so you can see it as actual SQL. Given the form of the statement I would think that $asc isn't what you think it is, it could possibly even be blank giving:
ORDER BY book. ASC
The book. being something the database can't deal with.
I think the issue with the page running a query automatically on load is because you are calling the query directly in your select element
<select name="catalogs">
<option></option>
<?php $result = mysqli_query($dbcon,"select * from book_catalog")or die(mysqli_error());
while ($row=mysqli_fetch_array($result)){ ?>
<option value="<?php echo $row['catalog']; ?>"><?php echo $row['catalog']; ?></option>
<?php } ?>
</select>
That PHP code isn't waiting for user input, it's just running immediately.

Make your query like this
mysqli_query($dbcon,"select * from book where catalog = '" . $selected_stat ."' OR status = '" . $selected_val ."' ORDER BY book." . $asc . " ASC")or die(mysqli_error($dbcon));

Related

Checkbox in PHP foreach loop - all checkboxes activate the 1st

I have a foreach loop providing data to a html table. I have added a form around the table and on each row am adding an input/checkbox with the value set as the row_id from the db call.
When clicking on the first checkbox it selects, when clicking on other rows the 1st checkbox in the table selects not the row you intended.
I was under the impression that setting the name="whatever[]" like so adds the checkbox to an array? So in the form processor I can get it via $_POST['checkbox'] array. Which is working on the form processor part.
I am using bootstrap to style everything so this might be an issue?
I can't for the life of me seem to be able to fix the click one checkbox get only the first checked.
This is my table code:
<form action="<?php echo URLROOT.'/admin/voyage/sendemail'; ?>" method="POST">
<table id="volunteers" class="table table-bordered text-secondary">
<thead class="bg-msp-lightgrey">
<tr>
<th width="35px">
</th>
<th>
Volunteer
</th>
<th>
Position
</th>
<th>
DBS Status
</th>
<th>
Last Training
</th>
<th>
Last Refit
</th>
<th>
Contact
</th>
<th>
Form
</th>
</tr>
</thead>
<tbody>
<?php foreach ($data['volunteerData'] as $volunteer) : ?>
<?php
$result = 'DBS Form to be sent';
$order = 0;
if ($volunteer->volunteer_dbsSent) {
$result = 'DBS Form sent to volunteer - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsSent));
$order = 1;
}
if ($volunteer->volunteer_dbsReceived) {
$result = 'DBS Form returned to us - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsReceived));
$order = 2;
}
if ($volunteer->volunteer_dbsASTO) {
$result = 'DBS Form sent to ASTO - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsASTO));
$order = 3;
}
if ($volunteer->volunteer_dbsReturned) {
$result = 'DBS returned - '.date('d/m/Y', strtotime($volunteer_dbsReturned));
$order = 4;
}
if ($volunteer->volunteer_dbsClear) {
$result = 'DBS clear, cert number: - '.$volunteer->volunteer_dbsCertNum;
$order = 5;
}
?>
<tr>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="check" name="check[]" value="<?php echo $volunteer->volunteer_id; ?>" />
<label class="custom-control-label" for="check"></label>
</div>
</td>
<td>
<a class="text-msp-lightblue" href="<?php echo URLROOT.'/admin/volunteer/manage/'.$volunteer->volunteer_id; ?>"><?php echo $volunteer->volunteer_firstName.' '.$volunteer->volunteer_lastName; ?></a>
</td>
<td>
<?php echo $volunteer->volunteer_rank; ?>
</td>
<td data-sort="<?php echo $order; ?>">
<?php echo $result; ?>
</td>
<td>
</td>
<td>
</td>
<td>
<a class="text-msp-lightblue" href="mailto:<?php echo $volunteer->volunteer_email; ?>"><?php echo $volunteer->volunteer_email; ?></a><br />
<a class="text-msp-lightblue" href="tel:<?php echo $volunteer->volunteer_mobile; ?>"><?php echo $volunteer->volunteer_mobile; ?></a>
</td>
<td>
<a target="_blank" class="text-msp-lightblue" href="<?php echo URLROOT.'/admin/volunteer/volunteerForm/'.$volunteer->volunteer_id; ?>">View</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="row mt-4">
<div class="col-5">
<div class="input-group">
<select class="custom-select" id="email" name="email">
<option selected>Update DBS Status...</option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<div class="input-group-append">
<button class="btn btn-msp-lightblue text-white" type="submit"><i class="fas fa-paper-plane"></i> Send</button>
</div>
</div>
</div>
</div>
</form>
Try this code. Added new variable $type= ""; and passed in checkbox array.
<input type="checkbox" class="custom-control-input" id="check_<?php echo $Inc;?>" name="check[<?php echo $type;?>][]" value="<?php echo $volunteer->volunteer_id; ?>" />
Checkbox Id also made unique.
<tbody>
<?php
$Inc = 1;
foreach ($data['volunteerData'] as $volunteer) : ?>
<?php
$result = 'DBS Form to be sent';
$order = 0;
$type= "";
if ($volunteer->volunteer_dbsSent) {
$result = 'DBS Form sent to volunteer - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsSent));
$order = 1;
$type= "dbsSent";
}
if ($volunteer->volunteer_dbsReceived) {
$result = 'DBS Form returned to us - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsReceived));
$order = 2;
$type= "dbsReceived";
}
if ($volunteer->volunteer_dbsASTO) {
$result = 'DBS Form sent to ASTO - '.date('d/m/Y', strtotime($volunteer->volunteer_dbsASTO));
$order = 3;
$type= "dbsASTO";
}
if ($volunteer->volunteer_dbsReturned) {
$result = 'DBS returned - '.date('d/m/Y', strtotime($volunteer_dbsReturned));
$order = 4;
$type= "dbsReturned";
}
if ($volunteer->volunteer_dbsClear) {
$result = 'DBS clear, cert number: - '.$volunteer->volunteer_dbsCertNum;
$order = 5;
$type= "dbsClear";
}
?>
<tr>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="check_<?php echo $Inc;?>" name="check[<?php echo $type;?>][]" value="<?php echo $volunteer->volunteer_id; ?>" />
<label class="custom-control-label" for="check"></label>
</div>
</td>
<td>
<a class="text-msp-lightblue" href="<?php echo URLROOT.'/admin/volunteer/manage/'.$volunteer->volunteer_id; ?>"><?php echo $volunteer->volunteer_firstName.' '.$volunteer->volunteer_lastName; ?></a>
</td>
<td>
<?php echo $volunteer->volunteer_rank; ?>
</td>
<td data-sort="<?php echo $order; ?>">
<?php echo $result; ?>
</td>
<td>
</td>
<td>
</td>
<td>
<a class="text-msp-lightblue" href="mailto:<?php echo $volunteer->volunteer_email; ?>"><?php echo $volunteer->volunteer_email; ?></a><br />
<a class="text-msp-lightblue" href="tel:<?php echo $volunteer->volunteer_mobile; ?>"><?php echo $volunteer->volunteer_mobile; ?></a>
</td>
<td>
<a target="_blank" class="text-msp-lightblue" href="<?php echo URLROOT.'/admin/volunteer/volunteerForm/'.$volunteer->volunteer_id; ?>">View</a>
</td>
</tr>
<?php
$Inc++;
endforeach; ?>
</tbody>

Display selected row on another page, on button click

I have a table reading out a few columns of a db, with an edit button in each row. All I am trying to do is, when the edit button is clicked, that row is displayed with all of its columns in another page. Any help would be great. I know it's simple, but I can get it to work. I am able to navigate to the next page, but not sure how to carry the id of the specific row and read that out. Thanks.
index. php
<form id="mainform" action="editFunding2.php">
<div class='row'>
<div class='col-12'>
<div class='table table-responsive'>
<table class='table table-striped table-bordered datatable active' id='grantTable'>
<thead>
<tr>
<th>FP ID</th>
<th>Short Title</th>
<th>PI</th>
<th>Department</th>
<th>Funding Type</th>
<th>Change Funding Type</th>
</tr>
</thead>
<tbody>
<?php
//Foreach loop iterates through each column of $getallrows function
foreach($allRows as $rowID => $rowInfo){ ?>
<tr>
<td><?php echo $rowInfo['fpID'];?></td>
<td><?php echo $rowInfo['shortTitle'];?></td>
<td><?php echo $rowInfo['PI'];?></td>
<td><?php echo $rowInfo['Department'];?></td>
<td><?php echo $rowInfo['fundingType'];?></td>
<!--Create dynamic id -->
<?php $rdDrop = "ddgrantType_".$rowInfo['fpID'];?>
<td>
<button type="submit" class="btn btn-danger" name="action" value='Change Funding Type'>Change Funding</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</form>
My editFunding2.php page
<div class='row'>
<div class='col-12'>
<div class='table table-responsive'>
<table class='table table-striped table-bordered datatable active' id='grantTable'>
<thead>
<tr>
<th>FP ID</th>
<th>Short Title</th>
<th>PI</th>
<th>Department</th>
<th>Division</th>
<th>Sponsor Name</th>
<th>Project Start</th>
<th>Project End</th>
<th>Funding Type</th>
<th>Yes/No</th>
<th>Proper Type If No</th>
<!-- <th>Exclusions</th>
<th>FP Durtation</th> -->
<th>Comment</th>
</tr>
</thead>
<tbody>
<?php
session_start();
$editRow = $_REQUEST['fpID'];
$mssql = mysqli_query("SELECT * FROM spacing.METADATA_WEBFORM WHERE FUNDING_PROPOSAL_ID = $fpID' ") or die (mysqli_error());
while($rowInfo = mysqli_fetch_array($mssql)) {
?>
<tr>
<td><?php echo $rowInfo['fpID'];?></td>
<td><?php echo $rowInfo['shortTitle'];?></td>
<td><?php echo $rowInfo['PI'];?></td>
<td><?php echo $rowInfo['Department'];?></td>
<td><?php echo $rowInfo['Division'];?></td>
<td><?php echo $rowInfo['sponsorName'];?></td>
<td><?php echo $rowInfo['Date_Project_Start']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['Date_Project_End']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['fundingType'];?></td>
<?php } ?>
<!--Create dynamic id -->
<?php $rdDrop = "ddgrantType_".$rowInfo['fpID'];?>
<td>
<div class="form-check">
<label class="form-check-label">
<input type="radio" class='form-check-input' name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="Yes" id="rdYes" onclick="disable('<?php echo $rdDrop;?>')" checked/> Yes
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input class='form-check-input' type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="No" id="rdNo" onclick="enable('<?php echo $rdDrop;?>')"/> No
</label>
</div>
</td>
<td>
<div class="dropdown">
<select class='form-control' name="ddgrantGroup[<?php echo $rowInfo['fpID'];?>]" id="<?php echo $rdDrop;?>" disabled>
<option value=''>Select Proper Funding Type</option>
<option value="Corporate Sponsor">Corporate Sponsor</option>
<option value="Federal">Federal</option>
<option value="Foundation Selected">Foundation Selected</option>
<option value="Internally Funded">Internally Funded</option>
<option value="State/Local">State/Local</option>
</select>
</div>
</td>
<td>
<div class="comment">
<textarea class="form-control" aria-label="With textarea" name="grantComment[<?php echo $rowInfo['fpID'];?>]" id="grantComment" placeholder="Comments"></textarea>
</div>
</td>
</tr>
</tbody>
</div>
</div>
</div>
<div class='row'>
<div class='col-12 text-right'>
<button type="submit" class="btn btn-secondary formsubmitbttn" name="action" value='save'>Save</button>
<button type="submit" class="btn btn-primary formsubmitbttn" name="action" value='complete'>Complete and Save</button>
</div>
</div>
</div>
Change your button to a
My Link to xx
In editFunding2.php :
$editRow = $_GET['id'];

auto select month on the dropdown base on the present month

I want to make my dropdown list auto select base on the present month
and show the record on my datatable
<div class="container-fluid">
<h2 class="form-signin-heading">Schedule Management</h2><hr />
<button class="btn btn-info" type="button" id="btn-add"> <span class="glyphicon glyphicon-pencil"></span> Add Schedule</button>
<button class="btn btn-info" type="button" id="btn-view"> <span class="glyphicon glyphicon-eye-open"></span> View Schedule</button>
<hr />
this part is where I select the month and submit(If i pick a month on my drop down ex.Nov it will auto submit its already working) what I want is to select automatically default value base on the present month ex. next month is Dec. everytime I open my website its already selected in Dec.
(isset($_POST["months"])) ? $dropDownVal = $_POST["months"] : $dropDownVal=0;
?>
<div class="content-loader">
<form method="post" action="index.php">
Select a month <select name="months" id="months" onchange ='this.form.submit()'>
<option value="0" <?php if ($dropDownVal==0) echo 'selected'; ?>>--------------</option>
<option value="1" <?php if ($dropDownVal==1) echo 'selected'; ?>>January</option>
<option value="2" <?php if ($dropDownVal==2) echo 'selected'; ?>>February</option>
<option value="3" <?php if ($dropDownVal==3) echo 'selected'; ?>>March</option>
<option value="4" <?php if ($dropDownVal==4) echo 'selected'; ?>>April</option>
<option value="5" <?php if ($dropDownVal==5) echo 'selected'; ?>>May</option>
<option value="6" <?php if ($dropDownVal==6) echo 'selected'; ?>>June</option>
<option value="7" <?php if ($dropDownVal==7) echo 'selected'; ?>>July</option>
<option value="8" <?php if ($dropDownVal==8) echo 'selected'; ?>>August</option>
<option value="9" <?php if ($dropDownVal==9) echo 'selected'; ?>>September</option>
<option value="10"<?php if ($dropDownVal==10) echo 'selected'; ?>>October</option>
<option value="11"<?php if ($dropDownVal==11) echo 'selected';?>>November</option>
<option value="12"<?php if ($dropDownVal==12) echo 'selected';?>>December</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
<table cellspacing="0" width="100%" id="example" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>Sched #</th>
<th>First Name</th>
<th>Last Name</th>
<th>Check In Date</th>
<th>Check Out Date</th>
<th>Room Rate</th>
<th>Reservation Fee</th>
<th>Date Paid</th>
<th>Mode of Paymnet</th>
<th>Status</th>
<th>edit</th>
<th>delete</th>
</tr>
</thead>
<tbody>
<?php
require_once 'dbconfig.php';
if(isset($_POST['months'])){ $months = $_POST['months']; }else { $months='';}
$sqladd = mysql_query("SELECT sum(rrate) from tblguest WHERE status = 0 and MONTH(checkin) = '".$months."' ");
while($result = mysql_fetch_assoc($sqladd)){
$stmt = $db_con->prepare("SELECT * FROM tblguest WHERE status = 0 and MONTH(checkin) = '".$months."' ");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fname']; ?></td>
<td><?php echo $row['lname']; ?></td>
<td><?php echo $row['checkin']; ?></td>
<td><?php echo $row['checkout']; ?></td>
<td><?php echo $row['rrate']; ?></td>
<td><?php echo $row['reservefee']; ?></td>
<td><?php echo $row['datepaid']; ?></td>
<td><?php echo $row['modepayment']; ?></td>
<td><?php echo $row['stats']; ?></td>
<td align="center">
<a id="<?php echo $row['id']; ?>" class="edit-link" href="#" title="Edit">
<img src="edit.png" width="20px" />
</a></td>
<td align="center"><a id="<?php echo $row['id']; ?>" class="delete-link" href="#" title="Delete">
<img src="delete.png" width="20px" />
</a></td>
</tr>
<?php
}
echo "<b>Total Room Rental Income</b> = " , $result['sum(rrate)'];
}
?>
</tbody>
Try this:
<?php
$currentMonth = date('n');
?>
<select>
<option value="1" <?php if ($currentMonth == 1) echo 'selected'; ?>>January</option>
...
//up to december
</select>
Use this to check on date parameters of PHP.
http://php.net/manual/en/function.date.php

Jquery mouse over event

I want to show a hidden div when I mouse over a link on my table cell. The event only fires up on the first row. This is what i did:
$(document).ready(function(){
$("#show_div").mouseover(function() {
$("#hello").css('visibility', 'visible');
});
$("#hello").mouseover(function() {
$("#hello").css('visibility', 'visible');
});
$("#hello").mouseout(function() {
$("#hello").css('visibility', 'hidden');
});
});
<form action="" method="post">
<table class="table table-bordered table-hover">
<div id="bulkOptionContainer" class="col-xs-4">
<select class="form-control" name="bulk_options" id="">
<option value="">Select Option</option>
<option value="publish">Publish</option>
<option value="draft">Draft</option>
<option value="delete">Delete</option>
</select>
</div>
<div class="col-xs-4">
<input class="btn btn-success" type="submit" name="submit" value="Apply"/>
<a class="btn btn-primary" href="posts.php?source=add_post">Add New</a>
</div>
<thead>
<tr>
<th><input type="checkbox" name="checkbox[]" id="selectAllCheckBoxes"></th>
<th>Id</th>
<th>Author</th>
<th scope="col">Title</th>
<th>Category</th>
<th>Status</th>
<th>Image</th>
<th>Tags</th>
<th>Comments</th>
<th>Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php $query = "SELECT * FROM post ORDER BY post_id DESC";
$result_set = mysqli_query($link, $query);
confirm_query($result_set);
while ($row = mysqli_fetch_assoc($result_set)):?>
<tr>
<td><label><input type="checkbox" class="checkBoxes" name="checkBoxArray[]"
value="<?php echo $row['post_id']; ?>"></label></td>
<td><?php echo $row['post_id'] ?></td>
<td><?php echo $row['post_author'] ?></td>
<td width="100%">
<!-- link to over-->
<a id="show_div"
href="../post.php?p_id=<?php echo $row['post_id'] ?>"><?php echo $row['post_title'] ?></a>
<!-- div to become visible on hover-->
<div id="hello" style="visibility:hidden;">
<p>post</p>
</div>
</td>
<td><?php echo sel_cat_byId($row['post_category_id']) ?></td>
<td><?php echo $row['post_status'] ?></td>
<td><img width="100" class="img-responsive" src="../images/<?php echo $row['post_image'] ?>"></td>
<td><?php echo $row['post_tag'] ?></td>
<td><?php echo $row['post_comment_count']; ?></td>
<td><?php echo $row['post_date'] ?></td>
<td><a class="btn btn-danger" href="posts.php?delete=<?php echo $row['post_id']; ?> "
onclick="alert('Are You Sure?')">Delete</a></td>
</td>
<td><a class="btn btn-success" href="posts.php?source=edit_post&edit=<?php echo $row['post_id']; ?>">Edit</a>
</td>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</form>
****the event only fires up on the first row.
Thanks
First replace your id with class and then try this
$(".show_div").mouseover(function() {
$(this).next(".hello").css('visibility', 'visible');
});
$(".show_div").mouseout(function() {
$(this).next(".hello").css('visibility', 'hidden');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a class="show_div"
href="#">abc</a>
<!-- div to become visible on hover-->
<div class="hello" style="visibility:hidden;">
<p>post</p>
</div>

PHP: Form echo's id in source but shows first id in link for all rows

I'm having an issue with a pop up form that has a field where the user needs to enter some info regarding a row of data.
The stuff he/she enters needs to be inserted into the table which I will do a Update query... But my issue is that the form echo's the id from the database in the source code but it doesn't show when I hover over the button.
After I press the button it only executes the query for the first id. And the others show only the 2nd and so forth until they are removed one by one. I don't know what I'm doing wrong:
<?php include_once('classes/profile.class.php');?>
<?php include_once('header.php');?>
<h1>
<?php _e('Traffic Fines Adjudication'); ?>
</h1>
<br>
<div class="tabs-left">
<?php
$result = "SELECT * FROM login_fines_adjudicated WHERE active = 0 ORDER BY date_issued";
$stmt = $generic->query($result);
//this function will take the above query and create an array
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
//do nothing
//with the array created above, I can create variables (left) with the outputted array (right)
$id = $row['id'];
$date = $row['date_added_db'];
$date_issued = $row['date_issued'];
$time_arrive = $row['time_arrived'];
$time_depart = $row['time_departed'];
$ref = $row['reference_code'];
$reason = $row['violation_reason'];
$location = $row['location'];
$bay = $row['bay_number'];
$licence = $row['licence'];
$punit = $row['parking_unit'];
$value = $row['value'];
$operator = $row['operator'];
$operator_id = $row['operator_id'];
//If date field is empty display nothing! Or sentence...
if(!empty($date))
{
?>
<table class="table">
<thead>
<tr>
<th>Photo</th>
<th>Date Added</th>
<th>Date Issued</th>
<th>Time Arrived</th>
<th>Time Departed</th>
<th>Reference Code</th>
<th>Violation Category</th>
<th>Location</th>
<th>Bay Number</th>
<th>Licence Plate</th>
<th>Parking Unit</th>
<th>Amount</th>
<th>Operator Name</th>
<th>Operator ID</th>
<th>Action</th>
</tr>
</thead>
<tr>
<td><img class="photo" src="photos/no-available.jpg" /></td>
<td><?php echo $date; ?><br /></td>
<td><?php echo $date_issued; ?></td>
<td><?php echo $time_arrive; ?></td>
<td><?php echo $time_depart; ?></td>
<td><?php echo $ref; ?></td>
<td><?php echo $reason; ?></td>
<td><?php echo $location; ?></td>
<td><?php echo $bay; ?></td>
<td><?php echo $licence; ?></td>
<td><?php echo $punit; ?></td>
<td><?php echo $value; ?></td>
<td><?php echo $operator; ?></td>
<td><?php echo $operator_id; ?></td>
<td>
<form action="approve.php" method="post">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>" />
<p>Approve</button></p>
</form>
<div id="reject-form" class="modal hide fade">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3><?php _e('Reject Reason'); ?></h3>
</div>
<div class="modal-body">
<div id="message"></div>
<form action="reject.php" method="post" name="rejectform" id="rejectform" class="form-stacked rejectform normal-label">
<div class="controlgroup rejectcenter">
<div class="control">
<input id="reject" name="reject" type="text"/>
</div>
</div>
<input type="hidden" name="delete_id" value="<?php echo $id; ?>" />
</form>
</div>
<div class="modal-footer">
<?php _e('Submit'); ?></button>
<p class="pull-left"><?php _e('Please give a short reason for rejecting.'); ?></p>
</div>
</div>
</form>
<p><a data-toggle="modal" href="#reject-form" id="rejectlink" tabindex=-1><button url="#" class="btn btn-primary">Reject</button></a></p>
</td>
</tr>
</table>
<?php
}
}
?>
</div>
<?php include ('footer.php'); ?>
Any help would be appreciated!
The problem is that you have many elements with the same id attribute. Genarally, it is a really bad practice to have more than elements with the same id, not to mention it does not conform to the HTML Spec, according to which "id = [...] This name must be unique in a document.". Therefore, you are strongly advised to fix all IDs mking sure it is unique (e.g. appending the current entry's ID).
What causes the specific problem you described in your question is this:
You define for every table a pop-up dialog (div), with id reject-form.
Then, you implement the "reject"-button as a link with href="#reject-form".
So, when you click on any "reject"-button, the user is taken to the first element found in the DOM with id="reject-form" (which is always the dialog for the first entry).
In order to fix that, you can append each entry's ID in the id attributes in two locations: the pop-up dialog and the "reject"-button:
// Replace that:
<div id="reject-form" class="modal hide fade">
// with this:
<div id="reject-form-<?php echo $id; ?>" class="modal hide fade">
// Replace that:
<a data-toggle="modal" href="#reject-form" id="rejectlink" tabindex=-1>
// with this:
<a data-toggle="modal" href="#reject-form-<?php echo $id; ?>" id="rejectlink" tabindex=-1>

Categories