modals showing blank after ajax call - php

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.

Related

display data dynamically in bootstrap modal in laravel

I was watching tutorial on how to display MySQL data in dynamic window then I realized its not for laravel i hope there is a why to use it on laravel.
jQuery / AJAX :
<script>
$(document).ready(function(){
$('.view_data').click(function(){
var student_id = $($this).attr("id");
$.ajax({
url:select.php,
method:"post",
data:{student_id:student_id},
success:function(data){
$('student_detail').html(data);
$('#dataModal').modal("show");
}
});
});
});
</script>
the window shows up when the script has only this line: $('#dataModal').modal("show"); so script works when its like this but does not show any data.
<script>
$(document).ready(function(){
$('.view_data').click(function(){
var student_id = $($this).attr("id");
$('#dataModal').modal("show");
});
});
</script>
url:select.php
<?php
if(isset($_POST["student_id"]))
{
$output = '';
$connect = mysqli_connect("localhost", "root", "", "sms");
$query = "SELECT * FROM students WHERE id = '".$_POST["student_id"]."'";
$result = mysqli_query($connect, $query);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td width="30%"><label>first name</label></td>
<td width="70%">'.$row["firstName"].'</td>
</tr>
';
}
$output .= "</table></div>";
echo $output;
}
?>
I have more data columns but im using firstName for now
and this is the window model that shows up :-
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Employee Details</h4>
</div>
<div class="modal-body" id="student_detail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Try this
<script>
$(document).ready(function(){
$('.view_data').click(function(){
var student_id = $(this).attr("id");// also here $this will be not defined
$.ajax({
url:select.php,
method:"post",
data:{student_id:student_id},
success:function(data){
$('#student_detail').html(data);//selector are not valid you are missing to add # at the beginning
$('#dataModal').modal("show");
}
});
});
});
</script>
You should do a test first, check (I don't know if you did this already or not) if the PHP script actually returns some data. You can do an ajax call and display the response in the console.

keeping what i searched in my page

i'm working on a a website and i've made a php file that will show my search results, (i'm working on request appointment) so when i click that it works but the only problem is my webpage refresh and it shows a blank page until i click on home button to redirect
// my research loop...
if(isset($_GET['submit-search'])) {
$search = mysqli_real_escape_string($conn, $_GET['search']);
$sql = "SELECT * FROM doctor WHERE DoctorFullName LIKE '%$search%' OR DoctorLocation LIKE '%$search%' OR DoctorSpeciality LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if ($queryResult > 0) {
?>
<div class="container overflow">
<div class="container overflow">
<span class="mentor">
<h2 class="display Text-mentor">
<span class="mentor-admin">If the result shown bellow not seems like what you need then ;</span>
<span class="mentor-admin-quote">select the speciality of the doctor you need and your insurance, and <span class="turquoise">search</span> again</span>
</h2>
</span>
</div>
<div class="container overflow">
<p class="p-about-results"><?php echo "There is ".$queryResult." result matching your search"?></p>
<p class="p-about-results">we hope that these results are the ones you are looking for :</p>
</div>
<?php
while ($row = mysqli_fetch_assoc($result)) :
?>
// my calendar for so i can submit appointment
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<form>
<input type="text" name="ap-time" id="t1" class="modal-input-text" placeholder="Choose a date">
<div id="sub" style="top: 40px;left: 5px;z-index:1;text-align: center;"></div>
<input type=submit name="ap_validation" value="request appointment" class="input-requestdate">
</form>
</div>
</div>
//Ajax code that i've been working on
<script type="text/javascript">
$(document).ready(function(){
//alert("jquery is working");
$(".input-requestdate").click(function(){
var name = $(".modal-input-text").val();
$.ajax({
url: "Appointment.php",
type: "POST",
async: false,
data: {
"done": 1,
"message": name,
},
success: function(data){
alert("requested");
window.location = window.location.href
}
})
});
});
</script>`
thanks for helping :)

fetch dashboard count from database using php jquery ajax

I'm trying fetch data count from database to HTML div using PHP, jQuery and AJAX, but code below is not working.
I just started learning ajax please help. Thank you!
index.php
<div class='col-lg-3 col-md-6'>
<div class='panel panel-primary'>
<div class='panel-heading'>
<div class='row'>
<div class='col-xs-3'>
<i class='fa fa-microphone fa-5x'></i>
</div>
<div class='col-xs-9 text-right'>
<!-- here i want to fetch result -->
<div id="leadmonth"></div>
<div>Total Leads</div>
</div>
</div>
</div>
</div>
</div>
Jquery
$(document).ready(function() {
$.ajax({
type:'POST',
url:'php_action/resourceqareportdashboardcount.php',
dataType : 'json',
success:function(html){
$('#leadmonth').html(html);
}
});
PHP
<?php
// This Month Count
require_once 'db_config.php';
$output= '';
$sql = "SELECT * FROM leads WHERE YEAR(leadcreationdate) = YEAR(NOW()) AND MONTH(leadcreationdate)=MONTH(NOW())";
$query = $connect->query($sql);
$totalleads = mysqli_num_rows($query);
if ($monthtotalleadscount > "0") {
$output = "<div class='huge'>$totalleads</div>";
} else {
$output = "<div class='huge'>0</div>";
}
// database connection close
$connect->close();
echo json_encode($output);
$(document).ready(function() {
$.ajax({
type:'POST',
url:'php_action/resourceqareportdashboardcount.php',
// dataType : 'json',
success:function(html){
$('#totalleadsmonth').html(html);
}
});
});

How do I get my modal information to show, while trying to make it dynamic?

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 ?

How can I return multiple data using ajax and json

I'm trying to return a multiple data from database, then I'll pass it to jquery using ajax & json but the problem is its not working. Can anyone help me to fix it? Here is my code.
Jquery
$('.highlight').click(function() {
var y = $(this).find("td").eq(0).find('input[type="hidden"]').val();
var x = $(this).find("td").eq(1).text();
var z = $(this).find("td").eq(2).text();
//$('#ClassModal').modal('show');
$("#spanclass").text(x);
$("#spanclass2").text(z);
$('#ClassModal').modal('show');
$.ajax({
type:"GET",
url: 'db_loadStud.php',
data: 'year='+x+'&subject='+z,
dataType: 'json',
success: function(data){
for(var x=0;x<data.length; x++){
$(".studname").find("span").text(data[0]+", "+data[1]);
}
}
});
return false;
});
PHP
<?php
session_start();
include("db_conf.php");
$fid = $_SESSION['fid'];
$year = $_GET['year'];
$sub = $_GET['subject'];
$sql = "SELECT si_lname, si_fname, si_mname,si_studentno FROM `student_info` a";
$sql .= " INNER JOIN (SELECT * FROM student_enrolled WHERE se_active='True') b ON a.si_studentno=b.se_id";
$sql .= " INNER JOIN tblcourses c ON b.se_level=c.c_level WHERE c_level='$year' AND c_subject='$sub' AND c_teacher = '$fid'";
$result = mysql_query($sql);
$arr;
while($array = mysql_fetch_row($result)){
echo json_encode($array);
}
?>
HTML
<div class="modal fade" id="ClassModal" style="margin-left:-400px;margin-right:auto;" tabindex="-1" role="dialog" aria-labelledby="ClassModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" style="width:1000px;">
<div class="modal-header" style="background-color:#0a4595;color:#fff">
<h4 class="modal-title" id="myModalLabel"><center>Landstand of Knowledge Learning School, Inc. [ <span id="spanclass"></span> - <span id="spanclass2"></span> ]</center></h4>
</div>
<div class="modal-body" style="height:470px;">
<div style="float:left;border:1px solid #0a4595;width:200px;height:435px;">
<div style="width:100%;height:30px;border-bottom:1px solid #0a4595;background-color:#0a4595;padding:5px;font-size:12;color:#fff">
<center>STUDENTS</center>
</div>
<div class="studname" style="width:100%;height:30px;border-bottom:1px solid #0a4595;padding:5px;font-size:12;">
<span id="studname"></span>
</div>
</div>
</div>
<div class="modal-footer" >
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
thank you in advance
In the for loop, you are always overriding the contents of the span instead of adding contents to the existing one so only the last item in the array will be visible
$(".studname").empty();
for (var x = 0; x < data.length; x++) {
$(".studname").find("span").append(data[x][0] + ", " + data[x][1]+'<br />');
}

Categories