i have a basic search form # service.php
Currently the search results display # search.php
i want the results display # service.php inside modal "bootstrap" instate of moving to search.php
Thank you
service.php
<form class="input-group" method="post">
<input type="text" class="form-control" name="keyword" placeholder="search..." />
<span class="input-group-btn">
<button class="btn btn-primary" data-toggle="modal" data-target=".bs example-modal-lg" href="index.php"><i class="fa fa-search"></i></button>
</span>
</form>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- modal body -->
<div class="modal-body">
Search Results
</div>
<!-- /modal body -->
</div>
</div>
</div>
search.php
$page = 1;
if(isset($_GET['page'])){
$page = $_GET['page'];
}
$searchQuery = '';
if(isset($_GET['keyword'])){
$keyword = $_GET['keyword'];
$searchQuery = '(serviceTitle LIKE "%'.$keyword.'%" or serviceDescription LIKE "%'.$keyword.'%")';
}
// get total news rows
$countRset = mysql_query('SELECT COUNT(*) as Total FROM service where '.$searchQuery);
$countRows = mysql_fetch_assoc($countRset);
$countRows = $countRows['Total'];
// get news by page
$limitPerPage = 20;
$startPage = 0;
if($page>1){
$startPage = ($page-1)*$limitPerPage;
}
$newsRset = mysql_query('SELECT * FROM service where '.$searchQuery.' order by serviceID desc LIMIT '.$startPage.', '.$limitPerPage);
$news = array();
while($row = mysql_fetch_array($newsRset)){
array_push($news, $row);
}
?>
<?php foreach($news as $key=>$item){?>
<?php echo $item['serviceTitle']; ?> ......
<?php }?>
So display it inside a modal you should definitely need an ajax request.
So first is:
make your search.php returns a json encoded result
Based on your code above you can return the json encoded result like this:
return json_encode($news);
then grab the result using an ajax request
then display it by target the modal body element
You could choose an vanilla JS of Ajax or Jquery Ajax
Related
I need to be able to update my sql table from open to close task.
I already wrote code for this but it is not working properly. On the modal it shows 2 'continue' buttons and right now the task table contains two tasks. Can somebody help me?
This is what the modal looks like:
and this is my code:
<?php
if (isset($_POST["closetask"])) {
$close_task_id = mysqli_real_escape_string($con, $_POST["close_task_id"]);
$sql = "UPDATE task SET task_status='closed' WHERE id_task='$close_task_id'";
$result = mysqli_query($con, $sql);
if ($result) {
$_SESSION['success'] = "Task closed";
$_SESSION['text'] = "Task has been closed successfully";
$_SESSION['icon'] = "success";
} else {
$_SESSION['success'] = "Error";
$_SESSION['text'] = "Unkown error, please try again";
$_SESSION['icon'] = "error";
}
}
?>
<?php
$query = "SELECT * FROM task ORDER BY id_task DESC";
$result = mysqli_query($con, $query);
?>
<!-- reject button -->
<form action="task-view.php" method="post" enctype="multipart/form-data">
<div id="rejectModal" class="modal fade" role="dialog" >
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Close this task?</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="d-flex align-items-center justify-content-center form-group" >
<p class="align-items-center text-center" for="company">This button means closing the task. Are you sure you want to continue?</p>
</div>
</div>
<div class="modal-footer">
<?php
while ($row = mysqli_fetch_array($result)) {
if ($row['task_status'] == 'open'){
$check_task = '
<input type="text" name="close_task_id" class="form-control" value="' . $row["id_task"] . '" hidden>
<button class="btn btn-success" type="submit" name="closetask" class="btn btn-success">Continue</button>
</button>
';
}
echo '
<br>
' . $check_task . '
';
}
?>
</div>
</div>
</div>
</div>
</div>
</form>
<!-- end of reject button-->
<?php
if (isset($_SESSION['success']) && $_SESSION['success'] != '') {
?>
<script>
swal({
title: "<?php echo $_SESSION['success']; ?>",
text: "<?php echo $_SESSION['text']; ?>",
icon: "<?php echo $_SESSION['icon']; ?>",
button: "OK",
});
</script>
<?php
unset($_SESSION['success']);
}
?>
Well, after going through your code snippet,
I could see this query
$query = "SELECT * FROM task ORDER BY id_task DESC";
$result = mysqli_query($con, $query);
meaning this fetches all records in the task table, the two continue buttons you see are coming from the while loop
while ($row = mysqli_fetch_array($result)) {
....
}
it logically implies you have two records or rows in your database task table and all the records where retrieved from the table.
I am developing a page in php that has multiple dynamic modals for displaying pdf files in the page itself with respective title and author names. All the data comes from a database designed in MySQL. The trigger buttons are also meant to be dynamic. I have tried the following code. The problem is I am getting the modals blank. I am able to print the data in fetch-reader.php page itself. I think I am doing something wrong in AJAX or modal part. Please guide me through it.
<?php $sqlGal = "select * from articles where year = '2019'";
$queryGal = $con->query($sqlGal);
if ($queryGal->num_rows > 0) {
while ($row = $queryGal->fetch_array()) { ?>
<div class="col-md-3 gallery-grid">
<h3 style="height: 200px; vertical-align: bottom; margin: 0; display: table-cell;"><?php echo($row['title']) ?></h3>
by<h4><?php echo($row['name']) ?></h4>
<i class="fa fas fa-book-open"></i> Read
</div>
<?php }
}
?><div class="clearfix"></div>
<div class="modal fade" id="myReader" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 id="articleName"></h3>
<h4 id="name"></h4>
<h6 id="position"></h6>
<h6 id="address"></h6>
</div>
<div class="modal-body">
<object data="" id="reader" width="100%" height="450px"></object></div>
</div>
</div>
</div>
</div>
in <script> part
$(document).ready(function() {
$('#myReader').on('show.bs.modal', function(e) {
var rowid = $(e.relatedTarget).data('id');
$.ajax({
type: 'get',
url: 'include/fetch_reader.php',
data: 'rowid='+rowid,
dataType: 'json',
success : function(response) {
$('#articleName').text(response.title);
$('#name').text(response.name);
$('#position').text(response.position);
$('#address').text(response.address);
var add = "uploads/articles/" + response.yearSl + ".pdf";
$('#reader').prop('data', add);
}
});
});
});
in fetch-reader.php
<?php
require_once('connect.php');
if ($_GET['rowid']) {
$id = $_GET['rowid'];
$sql = "SELECT `title`, `name`, `position`, `address`, `yearSl` FROM articles WHERE `id` = '".$id."'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_array();
}
$connect->close();
header('Content-Type: application/json');
echo json_encode($row);
}
Please guide me any other way which can serve my purpose, but I don't want to use any third-party tools for the purpose.
I think you are sending the wrong data in ajax. Try this into your "$ .ajax" call:
data: { 'rowid': rowid }
And try checking into browse console if the request shows any error. Into your ajax call you check only for success return, not for fail.
Trying to open a modal which should grab information from my database and present it onto the modal, after clicking on a specific image. The modal pops up, but it shows on the html inputs and none of the php inputs. How can i fix this? rental_id is the primary in the database table.
<?php
require_once '../core/init.php';
$id = $_POST['rental_id'];
$id = (int)$id;
$sql = "SELECT * FROM rental WHERE rental_id = '$id'";
$result = $db->query($sql);
$rental = mysqli_fetch_assoc($result);
?>
<!-- the div that represents the modal for the form -->
<?php ob_start();?>
<div class="modal fade" id="quote" tabindex="-1" role="dialog" aria-labelledby="quote" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" data-dismiss='modal' aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title text-center">Quote</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<div class="center-block">
<img src="<?= $rental['img']; ?>" alt="<?= $rental['make']; ?>" class="w3-image img-responsive"/>
</div>...
and...
<script>
function detailsmodal(rental_id){
var data = {"rental_id" : rental_id};
jQuery.ajax({
url : <?php echo BASEURL;?> + 'includes/detailsmodal.php',
method : "post",
data : data,
success: function(data){
jQuery('body').append(data);
jQuery('#quote').modal('show');
},
error: function(){
alert("Something went wrong!");
}
});
}
</script>
Have you checked $id = (int)$id; ? is it set, ok with a value ?
Any return error message ? (SO question )
<?php
print_r($rental); /* seems ok when tested with ie: $id=3 */
$sql = " SELECT img, make FROM rental WHERE rental_id=? ";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $id);
$results = $stmt->execute();
$stmt->bind_result($img, $make);
$stmt->store_result();
while($stmt->fetch()){
echo"[ $img / $make ]";
}
?>
does it make a difference ?
I need to pass a variable through the link when calling a pop up window. When adding a variable like I normally would the names don't match up with the pop up window.
This is what I have:
<a data-toggle="modal" href="#allMessages?id=<?php echo $job['job_id']; ?>" class="btn btn-warning btn-sm btn-icon">
<i class="fa fa-unlock-alt"></i>View All Messages
</a>
<div class="modal fade" id="allMessages" tabindex="-1" role="dialog" aria-labelledby="allMessages" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header success">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
<h4 class="modal-title">Reply</h4>
</div>
<?php
$getMessages = mysqli_query($mysqli,
"SELECT *
FROM messages
WHERE messages.job_id = $GET['id']
"
);
$messages = array();
while($message = mysqli_fetch_assoc($getMessages)) {
$messages[] = $message;
}
The problem is when I add the variable at the end of the button link #allMessages?id= it no longer calls the pop-up window.
Javascript:
$("[data-toggle='popover']").popover();
});
function load_page() {
var selected_page = document.getElementById("selected_page").value;
if (selected_page != "") {
window.location.href = selected_page
//Please note that the value recived,
//in this case selected_page,
//should be a valid url!
//Therefore the value of the
//<option> tag should be itself
//a url !
//ex.: <option value="page.php"> is valid
//<option value="page_1"> is not valid
}
I can't see this working.
The modal is already on the page, just invisible so any PHP queries would be done when the page is rendered, not when the modal is activated.
Personally I'd send the ID over in a data attribute, do the processing in an AJAX request and then push the results into the modal box.
Hello guys I'm trying to delete a record/row from my table using a modal delete confirmation. This is what I've tried so far:
<script type="text/javascript">
$(function(){
$(".btn-show-modal").click(function(e){
e.preventDefault();
$("#dialog-example").modal('show');
});
$("#btn-delete").click(function(e) {
$("#dialog-example").modal('hide');
});
});
</script>
<table class="table table-bordered">
<?php
$stmt2 = $conn->prepare( "SELECT project_code, description
FROM tblprojects" );
$stmt2->execute();
for($i=0; $row2 = $stmt2->fetch(); $i++){
$project = $row2['project_code'];
$desc = $row2['description'];
?>
<tr>
<td><a href="project-detail.php?code=<?php echo $project; ?>">
<?php echo $project; ?></a></td>
<td><?php echo $desc; ?></td>
<td><a href="update-project.php?code=<?php echo $project; ?>" title="Update record">
<i class="icon-edit icon-white">
</i>
</a></td>
<td><a href="#<?php echo $project; ?>"
id="<?php echo $project; ?>"
data-id="<?php echo $project; ?>"
class="btn-show-modal" data-toggle="modal" title="Delete record">
<i class="icon-trash icon-white"></i></a></td>
<div class="modal hide fade" id="dialog-example">
<div class="modal-header">
<h5>Confirm Delete</h5>
</div>
<div class="modal-body">
<p class="modaltext">Are you sure you want to delete this record?</p>
</div>
<div class="modal-footer">
<a href="#" data-dismiss="modal" class="btn btn-info">No<a>
<a href="delete-project.php?code=<?php echo $project; ?>"
class="btn btn-danger" id="btn-delete">Yes<a>
</div>
</div>
</tr>
<?php
}
?>
</table>
But the problem is, when I am about to delete the last row the one that gets deleted is the first row. Why is it like that? Any ideas? Help is much appreciated. Thanks.
The problem lies in the modal generation and passing the $project value.
You are using a loop as
for($i=0; $row2 = $stmt2->fetch(); $i++){
$project = $row2['project_code'];
$desc = $row2['description'];
?>
And inside the above loop you are generating the modals so basically you will have many modals which are equal to the num of rows in the query.
Now all of them are having the same "id" i.e. "dialog-example" and once you click on the delete it pop ups the first modal from the DOM and is deleting wrong data.
Solution
For each modal you give the id as
<div class="modal hide fade" id="dialog-example_<?php echo $project; ?>">
Then in the blow code
$(".btn-show-modal").click(function(e){
e.preventDefault();
$("#dialog-example").modal('show');
});
Get the id of the element using attr("id") and append this at the end of
"dialog-example_"+{id you received}
The same thing you need to do for the hide modal as well.
UPDATE ON HOW TO DO IT
Give the modal div id as
<div class="modal hide fade" id="dialog-example_<?php echo $project; ?>">
Then in the click function to as
$(".btn-show-modal").click(function(e){
e.preventDefault();
var id = $(this).attr('id');
var modal_id = "dialog-example_"+id;
$("#"+modal_id).modal('show');
});
Change
<a href="delete-project.php?code=<?php echo $project; ?>"
class="btn btn-danger" id="btn-delete">Yes<a>
to
<a href="delete-project.php?code=<?php echo $project; ?>"
class="btn btn-danger" id="<?php echo $project ;?>">Yes<a>
AND finally
$("#btn-delete").click(function(e) {
$("#dialog-example").modal('hide');
});
to
$(".btn btn-danger").click(function(e) {
var id = $(this).attr('id');
var modal_id = "dialog-example_"+id;
$("#"+modal_id).modal('hide');
});
because you generate modal for every row this is wrong !
and show modal(first modal show) this is wrong!(this delete first row)
create one modal and set parameter with jquery e.g
$(".btn-show-modal").click(function(e){
e.preventDefault();
$("#dialog-example").modal('show');
$("#dialog-example #btn-delete").attr('href','delete-project.php?code='+$(this).attr('data-id'));
});
good luck
On topic
Problem is you have multiple modals. But select it on 1 id. So jQuery will select the first value.
A solution would be to put the delete url in an hidden input field. Then when the user clicks the open delete modal you select the url and put it in the a tag.
Example time
JavaScript Part
$(function(){
$(".btn-show-modal").click(function(e){
// put the right url in the delete
$("#dialog-example .delete-url").attr('href', $(this).attr('data-delete-url');
$("#dialog-example").modal('show');
return e.preventDefault();
});
$("#btn-delete").click(function(e) {
$("#dialog-example").modal('hide');
});
});
PHP part
I assume $stmt is prepared etc
<ul>
<? foreach($stmt->fetchAll() as $record) : ?>
<li>
delete
</li>
<? endforeach; ?>
</ul>
<div class="modal hide fade" id="dialog-example">
<div class="modal-header">
<h5>Confirm Delete</h5>
</div>
<div class="modal-body">
<p class="modaltext">Are you sure you want to delete this record?</p>
</div>
<div class="modal-footer">
<a href="#" data-dismiss="modal" class="btn btn-info">No<a>
<a class="delete-url btn btn-danger">Yes<a>
</div>
</div>
Code explaind
It is not handy to put an bootstrap modal in an foreach/for/while unless you change the id. But then again lot of duplicate code.
What the code does it changes the delete-url on the fly depending on which a the user clicked
Off topic
I highly recommend using the foreach for your iteration of the data records instead of a for loop.
$stmt2 = $conn->prepare( "SELECT project_code, description
FROM tblprojects" );
$stmt2->execute();
for($i=0; $row2 = $stmt2->fetch(); $i++){ /** code **/}
would be
$stmt2 = $conn->prepare( "SELECT project_code, description
FROM tblprojects" );
$stmt2->execute();
foreach($stmt2->fetchAll() as $record){}
Because when you open the modal, it open the first #dialog-example div.
<script type="text/javascript">
$(function(){
$(".btn-show-modal").click(function(e){
e.preventDefault();
$(this).parent().find("#dialog-example").modal('show');
});