How to to quickly load session data in ajax or JQuery? - php

Please I have products(Cart) in session but loads very slow on server anytime I reload a table to display items added to the cart. How do I quickly display session data using ajax or JQuery? Kindly assist.After adding Item to cart. $("#tabler").load(" #tabler") Loads slowly. Please is there any better way to do this.
$("#newinvoice").on('submit', function(event) {
event.preventDefault();
$.ajax({
type: "post",
url: "{{route('InvoiceDump')}}",
dataType: "json",
data: $('#newinvoice').serialize(),
success: function(data){
document.getElementById('price').value = "";
document.getElementById('total').value = "";
document.getElementById('quantity').value = "1";
document.getElementById('ddlViewBy').value = "";
$("#tabler").load(" #tabler");
},
error: function(data){
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Something went wrong!',
footer: 'Why do I have this issue?'
})
}
});
});
My Table
<table class="table table-bordered table-striped m-0">
<thead>
<tr>
<th colspan="7" class="pt-3 pb-3">Invoice Details</th>
</tr>
<tr>
<th width="50%">Product</th>
<th width="50%">Category</th>
<th>Quantity</th>
<th width="20%">Price</th>
<th width="30%">Amount (Net)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach($cartItems as $key => $cart)
<tr>
<td><select class="form-control"><option>{!!#strtoupper($cart['invoice_product_name'])!!}</option></select></td>
<td><input type="text" value="{{#strtoupper($cart['item_category'])}}" class="form-control" readonly></td>
<td>
<!-- Field wrapper start -->
<div class="field-wrapper m-0">
<input type="text" readonly class="form-control" value="{{empty($app->currency) ? "GHS" : $app->currency}}#convert($cart['invoice_quantity'])" placeholder="Qty">
</div>
<!-- Field wrapper end -->
</td>
<td>
<!-- Field wrapper start -->
<div class="field-wrapper m-0">
<input type="text" readonly class="form-control" value="{{empty($app->currency) ? "GHS" : $app->currency}}#convert($cart['invoice_unit_price'])">
</div>
<!-- Field wrapper end -->
</td>
<td>
<!-- Field wrapper start -->
<div class="field-wrapper m-0">
<input type="text" readonly class="form-control" value="{{empty($app->currency) ? "GHS" : $app->currency}}#convert($cart['invoice_total_price'])">
</div>
<!-- Field wrapper end -->
</td>
<td>
<div class="table-actions">
<button class="btn btn-light" id="{{$cart['id']}}" onClick="DeleteItem(this.id)">
<i class="icon-trash-2"></i>
</button>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>

Related

Foreach loop on array of array indexes php

On the first page there is a form with a jquery script to add days.
If I just use one day I cant get all the info I need.
But when I add multiple days I just get the last entry.
I am trying to see every day of the event I book in the table on the next page book_gear.php.
But so far I am only see one day of the event .
I think it is because my foreach loop is going thought the array indexs rather than each event in the array.
book_avail.php
<?php require_once('../Include/initialize.php'); ?>
<!doctype html>
<html>
<!--Sets page title in browser-->
<?php $page_title = 'Schedule'; ?>
<!-- Imports header file with necessary scripts -->
<?php include ('../Include/header.php'); ?>
<!-- Imports Top navigation bar -->
<?php include ('../Include/navibar.php'); ?>
<!-- Imports javascript file to allow arrange by column -->
<?php include_once ('../Include/sort_by.php'); ?>
<?php include_once ('../Include/mysql_queries.php');
//loads all classes
include_once ('../Include/class-autoload.inc.php'); ?>
<script>
$(document).ready(function() {
var i = 1;
$("#add_row").click(function() {
$('tr').find('input').prop('disabled',true)
$('#addr' + i).html("<td>" + (i + 1) +
"</td><td><input type='text' value='Day" + (i + 1) +"' name='event[" + i + "][eventNameInitialQuery]' class='form-control input-md'/></td>\n\
<td><input type='date' name='event[" + i + "][dateFromIntialQuery]' placeholder='date' class='form-control' /></td>\n\
<td><input type='time' name='event[" + i + "][timeFromIntialQuery]' placeholder='time' class='form-control' /></td>\n\
<td><input type='date' name='event[" + i + "][dateToIntialQuery]' placeholder='date' class='form-control' />\n\</td>\n\
<td><input type='time' name='event[" + i + "][timeToIntialQuery]' placeholder='time' class='form-control' />\n\</td>");
$('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
i++;
});
});
</script>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<?php
//sets user location or builing
if (isset($_SESSION['location'])) {
$userLocation = $_SESSION['location']?? '';}
$build_set = build_dropdown($userLocation);
?>
<!--Main Id is the bulk of the page-->
<div id="main">
<!--Sets side navigation bar-->
<div class="sidenav">
<div>
<!--Includes side navigation script -->
<?php include_once ('../Include/side_navi_login.php'); ?>
</div>
</div>
<br><br>
<body>
<!--Sets Title "Schedule Event"-->
<h1 class="book_event_title">Schedule Event</h1>
<!--This table is the form that runs along the top of the page-->
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form action="book_gear.php?" method="post">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr>
<th class="text-center">
Day
</th>
<th class="text-center">
Event Name
</th>
<th class="text-center">
Date From
</th>
<th class="text-center">
Time From
</th>
<th class="text-center">
Date To
</th>
<th class="text-center">
Time To
</th>
<th class="text-center">
Building
</th>
<th class="text-center">
VP Present
</th>
<th class="text-center">
Ticket
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td>
<input type="text" name='event[0][eventNameInitialQuery]' placeholder='Event Name' class="form-control" />
</td>
<td>
<input type="date" name='event[0][dateFromIntialQuery]' placeholder='date' class="form-control" />
</td>
<td>
<input type="time" name='event[0][timeFromIntialQuery]' placeholder='time' class="form-control" />
</td>
<td>
<input type="date" name='event[0][dateToIntialQuery]' placeholder='date' class="form-control" />
</td>
<td>
<input type="time" name='event[0][timeToIntialQuery]' placeholder='time' class="form-control" />
</td>
<!-- Gets the dropdown menu from a method in the site view class -->
<td>
<select name="buildingCodeIntialQuery" id="site_value" required>
<option value="" disabled selected>-Building-</option>
<?php
$buildList = new SiteView;
$buildList->showMyBuildings($userLocation) ;
?>
</select>
</td>
<td>
<input type="hidden" name="vpInitialQuery" value="No" class="form-control"/>
<input type="checkbox" name="vpInitialQuery" value="Yes" class="form-control"/>
</td>
<td>
<input type="text" name='ticketInitialQuery' placeholder='Link to Event Ticket' class="form-control" />
</td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
<div align="right" id="schedule_button">
<input type="submit" value="Schedule Event"/>
</div>
</form>
</div>
</div>
<button id="add_row" class="btn btn-primary btn-lg pull-left">Add Day</button>
</div>
</div>
</body>
<!--Includes bottom footer-->
<?php include('../Include/footer.php'); ?>
</html>
book_gear.php
<?php require_once('../Include/initialize.php'); ?>
<!doctype html>
<html>
<!--Sets page title in browser-->
<?php $page_title = 'Schedule'; ?>
<!-- Imports header file with necessary scripts -->
<?php include ('../Include/header.php'); ?>
<!-- Imports Top navigation bar -->
<!-- Imports javascript file to allow arrange by column -->
<?php include_once ('../Include/sort_by.php');
include_once ('../Include/mysql_queries.php'); ?>
<?php
//once the person is logged in their site is stored in this variable
if(isset($_SESSION['logins'])) {
$users_site = $_SESSION['location'];
}
if(isset($_POST['event'])) {
$events = $_POST['event'];
}
foreach($events as $unique =>$event){
?>
<table>
<tr>
<th>event name</th>
<th>datefrom</th>
<th>timefrom</th>
<th>dateto</th>
<th>timeto</th>
</tr>
<td><?php echo $event['eventNameInitialQuery'];?></td>
<td><?php echo $event['dateFromIntialQuery'];?></td>
<td><?php echo $event['timeFromIntialQuery'];?></td>
<td><?php echo $event['dateToIntialQuery'];?></td>
<td><?php echo $event['timeToIntialQuery'];?></td>
</table><?php ;}?>
EDIT
After suggested in the comments ran
echo '<pre>'; var_dump($events);echo '</pre>';die(); before the foreach loop
Result
array(1) {
[1]=>
array(5) {
["eventNameInitialQuery"]=>
string(4) "Day2"
["dateFromIntialQuery"]=>
string(10) "2020-07-23"
["timeFromIntialQuery"]=>
string(5) "18:57"
["dateToIntialQuery"]=>
string(10) "2020-07-11"
["timeToIntialQuery"]=>
string(5) "19:54"
}
}
I've done a quick test and the problem is with disabling the earlier inputs when adding a new event row. What you could do is copy the values of your inputs to hidden inputs before you disable them.

Ajax, unable to append data to specific "data" tag

I have this for generating php buttons, and I want to append a specific data right after the button in the table that's right below.
This is my html
<tr>
<td colspan="2">
<!-- -->
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="ver"></label>
<div class="col-md-4">
<button data-dnipass="<?= $dni?>" class="ver" name="ver" class="btn btn-primary">Ver líneas</button>
</div>
</div>
<table class="table userInfo" data-formpost="<?= $dni?>"></table> <!-- This is were i want to append -->
</td>
</tr>
Note that its located inside a for loop
for($i = 0 ; $i < count($usuarios); $i++){ }
And this is my Ajax/jQuery call
<script type="text/javascript">
$(document).ready(function(){
$(".ver").click(function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'adminVerLineas.php',
data: {
dni: $(".ver").data("dnipass")
},
success: function(data) {
alert(data);
$(".userInfo").data("formpost").append(data);
}
});
});
});
</script>
The alert works properly, but the append isn't targeting the specific table below each button
EDIT: after testing the first answer still not works, this is how I see my html on inspection.
After adding to my Ajax:
alert(data);
$(".userInfo").data("formpost", data);
--
<td colspan="2">
<!-- -->
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="ver"></label>
<div class="col-md-4">
<button data-dnipass="12345678B" class="ver" name="ver">Ver líneas</button>
</div>
</div>
<table class="table userInfo" data-formpost="12345678B"></table>
</td>
This is adminVerLineas.php
<?php
echo "<tr>
<td style='color : white'>Número</td>
<td style='color : white'> 1</td>
</tr>
<tr>
<td style='color : white' >Minutos Gastados</td>
<td style='color : white' >1 / 1</td>
</tr>
<tr>
<td style='color : white' >Megas Gastados</td>
<td style='color : white' >1 / 1</td>
</tr>
<tr>
<td style='color : white' >Tarifa</td>
<td style='color : white' >1</td>
</tr>";
?>
If you want to insert data into the data-formpost attribute you have to use $(".userInfo").data("formpost", data)
$(".userInfo").data("formpost") get the data from data-formpost
$(".userInfo").data("formpost", data) sets the data in data-formpost
if your data contains table html, then you have to use $(this).closest("tr").find(".userInfo").append(data)

Codeigniter Submit Ajax Result Via Button to Datatable Not Working

I'm currently using Codeigniter and using Ajax to post my search result through the server and result the result and display on my responsive datatable. However the result was not added into my datatable and i even try to add it with .append but it just added below my table and the search function on datatable is not searching it and it still show "No data available in table".
Here are my View :
<div id="reporting" class="container">
<h3 class="section-title">Export Time Location Panel</h3>
<form id = "search">
<div class="form-group">
<div class="form-group">
<h5>Date/ Time</h5>
<input type="datetime-local" name="datetime" id = "datetime">
<?php echo form_error('datetime'); ?>
</div>
</div>
<button type="submit" class="btn btn-primary">Confirm</button>
</form><br/>
<table class="table table-bordered table-light" >
<thead>
<tr>
<td class="id">ID</td>
<td class="na">NA Name</td>
<td class="centre">Centre Name</td>
<td class="login">Date & Time</td>
<td class="lat">Latitude</td>
<td class="long">Longtitude</td>
<td class="accuracy">Accuracy</td>
<td class="place">Location Name</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<?php include('footer.php'); ?>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
var tbl = $('.table').DataTable({
responsive: true,
"order": [[0, "desc"]]
})
$( "#search" ).submit(function(event) {
event.preventDefault();
var value = $("#datetime").val();
$.ajax({
type:'POST',
url:'<?php echo base_url(); ?>location/search_record',
data:{'datetime':value},
success:function(data){
$('.table').append(data);
}
});
});
} );
</script>
Here are my Model :
public function search_record(){ //Display Record at View Page
$this->load->model('location_model');
$this->load->library('form_validation');
echo '
<tr>
<td class="id">ID</td>
<td class="na">NA Name</td>
<td class="centre">Centre Name</td>
<td class="login">Date & Time</td>
<td class="lat">Latitude</td>
<td class="long">Longtitude</td>
<td class="accuracy">Accuracy</td>
<td class="place">Location Name</td>
</tr>
';
}
You have to add data to table body by
success:function(data){ tbl.rows.add($(data)).draw(); }
Please check this link
https://jsfiddle.net/kuLcqwav/3/
https://datatables.net/examples/api/add_row.html

How to calculate sum of all rows with jQuery? [duplicate]

This question already has answers here:
jQuery sum rows adding all cells
(3 answers)
Closed 5 years ago.
Below is my table code with Add New button. Onclick Add New
button new row will create. In picture you can see this. User will
input debit and credit values at the end all debit values should be
calculate and sum should be display and same for the credit value. I
am using jQuery to create new row:
Jquery code:
<script type="text/javascript">
$("#add, .check ").click(function() {
$('#datatable-buttons tbody>tr:last')
.clone(true)
.insertAfter('#datatable-buttons tbody>tr:last').find('input').each(function(){
$(this).val('');
});
});
</script>
Html table code:
<table id="datatable-buttons" class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th>
<input type="checkbox" id="check-all" class="flat">
</th>
<th class="column-title">Account</th>
<th class="column-title">Debits</th>
<th class="column-title">Credits</th>
<th class="column-title">Description</th>
<th class="column-title">Name</th>
<th class="column-title no-link last"><span class="nobr">Action</span>
</th>
<th class="bulk-actions" colspan="7">
<a class="antoo" style="color:#fff; font-weight:500;">Bulk Actions ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>
</th>
</tr>
</thead>
<tbody>
<tr class="even pointer">
<td class="a-center ">
<input type="checkbox" class="flat" name="table_records">
</td>
<td class=" "><select class="form-control" name="journalname">
<option>Choose option</option>
<?php
$sql = mysqli_query($conn,'SELECT * FROM `chartofaccounts`');
while ($row1 = mysqli_fetch_array($sql))
{?>
<option value="<?php echo $row1["name"];?>"><?php echo $row1["name"];?></option>
<?php }?>
</select></td>
<td class=""><input class="form-control" type="text" name="debit" ></td>
<td class=" "><input class="form-control" type="text" name="credit"></td>
<td class=" "><input class="form-control" type="text" name="descc"></td>
<td class=" "><input class="form-control" type="text" name="name"></td>
<td class=" last">View
</td>
</tr>
</tbody>
</table>
<button type="button" id="add">Add New</button>
Below image will give you clear understanding:
made you a simple example of how to calculate the sum from the input fields.
check snippet below or jsFiddle
add numbers to the 3 inputs, and then click on View , it will get the sum of the 3 input values.
var input = $("input"),
button = $("td.last a")
$(button).click(function () {
var sum = 0;
$(input).each(function() {
sum += Number($(this).val());
});
$(this).html(sum)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
<input>
</td>
<td>
<input>
</td>
<td>
<input>
</td>
<td class=" last">View
</tr>
</table>

Displaying value in textbox for to edit using ajax and php

editModal.php
<div id="edit-modal" class="modal fade" tabindex="-1" role="dialog" aria- labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">
<i class="glyphicon glyphicon-edit"></i> Edit Profile
</h4>
</div>
<div class="modal-body">
<div id="modal-loader" style="display: none; text-align: center;">
</div>
<div id="dynamic-content">
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>First Name</th>
<td id="txt_first">
<input id="result_table" type="text" class="form-control" name="Firstname" ng-model="name">
</td>
</tr>
<tr>
<th>Middle Name</th>
<td id="txt_middle" > </td>
</tr>
<tr>
<th>Last Name</th>
<td id="txt_last"></td>
</tr>
<tr>
<th>Email Add</th>
<td id="txt_emailadd"></td>
</tr>
<tr>
<th>Contact Number</th>
<td id="txt_cnumber"></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" value="send" name="submit" class="btn btn-primary">Save Changes</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div><!-- /.modal -->
</div>
$(document).on('click', '#getUser', function(e){
e.preventDefault();
var uid = $(this).data('id'); // get id of clicked row
$('#dynamic-content').hide(); // hide dive for loader
$('#modal-loader').show(); // load ajax loader
$.ajax({
url: '../model/editUser.php',
type: 'POST',
data: 'id='+uid,
dataType: 'json'
})
.done(function(data){
console.log(data);
$('#dynamic-content').hide(); // hide dynamic div
$('#dynamic-content').show(); // show dynamic div
$('#txt_first').html(data.First_Name);
$('#txt_middle').html(data.Middle_Name);
$('#txt_last').html(data.Last_Name);
$('#txt_emailadd').html(data.Email_Add);
$('#txt_cnumber').html(data.Contact_Number);
$('#modal-loader').hide(); // hide ajax loader
})
.fail(function(){
$('.modal-body').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...');
});
});
});
</script>
I wanted to display the value of the $('#txt_first').html(data.First_Name); on the input type="text" value = "php echo $firstName " But it seems that I'm doing it wrong. Is it possible for me to enclosed the table and the script in a ; segment?
It is input field so you need to do it with 'value' not 'html'. try this:
$('#txt_first').val(data.First_Name);
Created demo to display data.First_Name in input type ="text"
$('#result_table').val("test");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered">
<tr>
<th>First Name</th>
<td id="txt_first">
<input id="result_table" type="text" class="form-control" name="Firstname" ng-model="name">
</td>
</tr>
<tr>
<th>Middle Name</th>
<td id="txt_middle" > </td>
</tr>
<tr>
<th>Last Name</th>
<td id="txt_last"></td>
</tr>
<tr>
<th>Email Add</th>
<td id="txt_emailadd"></td>
</tr>
<tr>
<th>Contact Number</th>
<td id="txt_cnumber"></td>
</tr>
</table>

Categories