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.
Related
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.
I have this php page. I'm struggling to find a solution to this one as whenever I call this page inside a modal, it does not show.
I tried several changes to this one, like changing the quotations of ever single elements inside the code, changing the echoing structure, and modifying to all just for me to be able to echo it, but no luck in all of them.
This serves as my earnings_amendment_table.php
<?php include 'backend/session.php'; ?>
<?php
if(isset($_POST['referenceno']))
$id=$_POST['referenceno'];
$sql = "select credit, debit, referenceid, datedue,
referenceno, earningsamendment.employeeidno, earningsamendment.accounttitle, earningsamendment.postedby, approvedby,
notedby,employeemasterfile.lastname,employeemasterfile.firstname,employeemasterfile.middlename,
earningsamendment.particulars,earningsamendment.accounttype
from earningsamendment
left join employeemasterfile on earningsamendment.employeeidno= employeemasterfile.employeeidno
where referenceno='$id' order by datedue desc";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
$out = "
<div class='box-body'>
<table id='earnings_amendment_view' class='table table-bordered' style='table-layout: fixed; display: table'>
<thead>
<th></th>
<th>Reference ID.</th>
<th>Reference No.</th>
<th>Employee Name</th>
<th>Account Title</th>
<th>Amount</th>
<th>Activity</th>
<th>Posted By</th>
<th>Validated By</th>
<th>Noted By</th>
<th>Tools</th>
</thead>
<tbody>
";
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
echo "
<tr data-key-1=".$row['particulars']." data-key-2=".$row['accounttype']." data-key-3=".$row['datedue'].">
<td class='details-control'></td>
<td>".$row['referenceid']."</td>
<td>".$row['referenceno']."</td>
<td>".$row['lastname']." ".$row['middlename']." ".$row['firstname']."</td>
<td>".$row['accounttitle']."</td>
<td>".$row['credit']."</td>
<td>".(($row['debit']==$row['credit']) ? 'PAID' : 'FOR TAKE UP' )."</td>
<td>".$row['postedby']."</td>
<td>".$row['approvedby']."</td>
<td>".$row['notedby']."</td>
</tr>
";
}
$out .= '
</tbody>
</table>
</div>
';
echo $out;
?>
This script is being called by this modal caller from earnings_amendment.php
$(function(){
$("body").on('click', '.edit', function (e){
e.preventDefault();
var id = $(this).data('id');
$.ajax({
type: 'post',
url: 'earnings_amendment_table.php',
data: {referenceno:id}
}).done(function(d){
//console.log('d: '+d);
$('#earnings_amendment_table_').html(d);
$('#earnings_amendment_table_').show();
});
});
});
Along with the clicker, still within the same page (earnings_amendment.php)
$("body").on('click', '.select', function (e){
e.preventDefault();
$('#select').modal('show');
var id = $(this).data('id');
getRow(id);
$(".modal:visible").modal('toggle');
});
This is the table
function format ( dataSource ) {
var html = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;" class="table table-bordered">';
for (var key in dataSource){
html += '<tr>'+
'<td>' + key +'</td>'+
'<td>' + dataSource[key] +'</td>'+
'</tr>';
} return html += '</table>'; }
var earnings_amendment_employee_view_table = $('#earnings_amendment_view').DataTable({});
$('#earnings_amendment_view').on('click', 'td.details-control', function () { var tr = $(this).closest('tr');
var row = earnings_amendment_employee_view_table.row(tr);
if (row.child.isShown()) {
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(format({
'Particulars : ' : tr.data('key-1'),
'Account Type : ' : tr.data('key-2'),
'Date Due : ' : tr.data('key-3')
})).show();
tr.addClass('shown');
} });
And, this is the modal where it is being called.
This is earnings_amendment_modal.php
<!-- Edit -->
<div class="modal fade" id="edit">
<div class="modal-dialog" style="width:95%">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"><b>Update Founder Data</b></h4>
</div>
<div class="modal-body">
<form autocomplete='off' class="form-horizontal" method="POST" action="earnings_amendment_edit.php">
<input type="hidden" class="decid" name="id">
<input type="hidden" name="year" value="<?php echo $year; ?>">
<input type="hidden" name="accounttitle" value="<?php echo $accounttitle; ?>">
<input type="hidden" class="decid" name="id">
<!-- Table Loader -->
<div class="form-group" id="earnings_amendment_table_"><div class="col-sm-9" id="earnings_amendment_table_" style="width:100%">
</div></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-flat pull-left" data-dismiss="modal"><i class="fa fa-close"></i> Close</button>
</form>
</div>
</div>
</div>
</div>
What seems to be the problem here? As my modal caller is properly set up.
<!-- Table Loader -->
<div class="form-group" id="earnings_amendment_table_"><div class="col-sm-9" id="earnings_amendment_table_" style="width:100%">
</div></div>
It's only displaying this.
I tried to check it via purposely doing errors in the table and the error properly displays.
I'm assuming that the main cause of this concern is this line.
echo "
<tr data-key-1=".$row['particulars']." data-key-2=".$row['accounttype']." data-key-3=".$row['datedue'].">
<td class='details-control'></td>
<td>".$row['referenceid']."</td>
<td>".$row['referenceno']."</td>
<td>".$row['lastname']." ".$row['middlename']." ".$row['firstname']."</td>
<td>".$row['accounttitle']."</td>
<td>".$row['credit']."</td>
<td>".(($row['debit']==$row['credit']) ? 'PAID' : 'FOR TAKE UP' )."</td>
<td>".$row['postedby']."</td>
<td>".$row['approvedby']."</td>
<td>".$row['notedby']."</td>
</tr>
";
As I already have a variable holder $out that should hold everything together.
Is there a turnaround for this? Thanks in advance.
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 :)
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);
}
});
});
I have two tables in mysql DB, 'cars' and 'address' which they have relation with primary key and 'on delete restrict' in DB so I can't delete some address which have car and that's good. Addresss in DB have columns 'street', 'number' and 'city'. I want to delete every location which has no cars but I don't know where I'm wrong in code, it doesn't work.I checked all paths and all is ok. Any suggestion appreciate.
This is my modal delete button:
<a href="#" data-toggle="modal" data-target="#delete_loc<?php echo $value['id']?>" role="button" class="btn btn-danger btn-sm">
<i class="fa fa-trash-o"></i> Delete
</a>
<div class="modal fade" id="delete_loc<?php echo $value['id']?>" tabindex="-1" role="dialog" aria-labelledby="mydelModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" style="text-align:center;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h2 class="modal-title" id="mydelModalLabel" style="color:#a80b27;text-transform:uppercase;font-size:1.6rem;">Warning !</h2>
</div>
<div class="modal-body">
<h5>Are you shure you want delete address ID <b><?php echo $value['id']?></b>?</h5>
</div>
<div class="modal-footer">
<input type="hidden" name="loc_id" id="loc_id" value="<?php echo $value['id']?>">
<a href="" role="button" class="btn btn-danger" id="del_loc">
Yes, I'm shure!
</a>
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
This is jquery and ajax:
$(document).ready(function() {
$('#del_loc').bind('click',function(e){
e.preventDefault();
var id = $('#loc_id').val();
var data = 'loc_id='+id;
$.ajax({
type: "POST",
url: "include/locations/delete.php",
data: data,
success: function(result){
if(result == 1){
window.location.replace("address.php?id=");
}
else{
err_msg = JSON.parse(result);
if(err_msg.nAddress){
$('.panel-footer').append('<span class="help-block" style="float:right;"><strong>'+err_msg.nAddress+'</strong></span>');
}
}
}
});
});
});
This is delete.php:
<?php
require '../../config/init.php';
require '../functions/locations.php';
require '../services/xss.php';
$loc_id = _e($_POST['loc_id']);
$data = deleteLoc($loc_id);
if(is_array($data)){
print_r(1);
}
else{
echo $data;
}
?>
And this is function named locations.php:
function deleteLoc($loc_id){
global $conn;
try{
$stmt = $conn->prepare('DELETE FROM address WHERE id=:id');
$stmt->bindParam(':id',$loc_id);
$stmt->execute();
$row[] = 'deleted';
return $row;
}
catch(PDOException $e){
if(DEBUG === true){
header('Location:../../error/db_error.php?err_msg='.$e->getMessage());
}
else{
header('Location:../../error/db_error.php?err_msg');
}
}
}
If you are getting the value loc_id value properly.I think following code will help you a lot. Let's try..
JavaScript
<script type="text/javascript">
$(document).ready(function() {
$('#del_loc').bind('click',function(e){
e.preventDefault();
var id = $('#loc_id').val();
var data = 'loc_id='+id;
//try here alert(id); For checking whether you are getting value or not
$.ajax({
type: "POST",
url: "include/locations/delete.php",
dataType:'JSON',
data: data,
success: function(result){
var res=eval(result);
if(res.success){
window.location.replace("address.php?id=");
}
else
{
if(res.nAddress){
$('.panel-footer').append('<span class="help-block" style="float:right;"><strong>'+err_msg.nAddress+'</strong></span>');
}
}
}
});
});
});
</script>
PHP delete.php
<?php
require '../../config/init.php';
require '../functions/locations.php';
require '../services/xss.php';
$loc_id = _e($_POST['loc_id']);
$data = deleteLoc($loc_id);
if(is_array($data)){
echo json_encode(array('success'=>true));
}
else{
echo json_encode($data);
}
?>