ajax change function execute listByBatch() and show the table with data by batch which is post by ajax. And then I trying to insert table rows by insertAttendance, code below:
Jquery post:
$("#batch-list").change(function(){
/*dropdown post */
$.ajax({
url:"<?php echo base_url(); ?>index.php/attendance/list_ByBatch",
data: {batchid: $(this).val()},
type: "POST",
dataType:'json',
success: function(data){
$("#traineeList").html(data);
$("#subTotal").html("Total: " + $('#traineeList tr').length.toString());
document.getElementById("classHour").defaultValue = "4";
}
});
});
Controller:
public function list_ByBatch() {
$batch = $this->input->post('batchid', TRUE);
$data['trainee']= $this->AttendanceModel->get_traineeList($batch);
if (!empty($data['trainee'])) {
echo form_open('attendance/insertAttendance');
echo"<table class='table table-hover type-list2'id='traineeList'>
<tr class='success'>
<th>Trainee ID</th>
<th>Trainee Name</th>
<th>Present</th>
</tr>";
foreach ($data['trainee'] as $row) {
echo "
<tr><td>" . str_pad($row->TraineeID, 7, "0", STR_PAD_LEFT) . "</td>
<td>" . $row->Name . "</td>
<td><input type='checkbox' name='.$row->TraineeID[]' value='" . $row->TraineeID . "' checked></td>
</tr>";
}
echo"</table>";
echo"
<div class='row'>
<div class='form-group col-sm-2'>
<h5 id='subTotal'></h5>
</div>
<div class='form-group col-sm-6'>
<input type='date' class='form-control' name='attnDate' id='attnDate' placeholder='Attendance Date' required>
</div>
<div class='form-group col-sm-2'>
<input type='number ' class='form-control' name='classHour' id='classHour' placeholder='Class Hour' required>
</div>
<div class='form-group col-sm-2'>
<input type='submit' class='btn btn-default btn-success' name='record' value='Record'>
</div>
</div>";
echo "</form>";
}
}
public function insertAttendance() {
$TID ['TID']= $this->input->post('trainee');
$attnDate = $this->input->post('attnDate');
$classHour = $this->input->post('classHour');
echo '<pre>';
print_r($TID);
if(is_array($TID)){
foreach ($TID as $TID=>$key) {
$query = "INSERT INTO `tbl_attn_temp` (TraineeID, Date, classHour) VALUES ('" . $key . "','" . $attnDate . "','" . $classHour . "')";
$this->db->query($query);
}
}else{
echo "Trainee ID is not array";
}
}
I just trying to insert all rows (TraineeID) shows in table with "attnDate" and 'classHour" by clicking submit button.
Related
I am using ajax to get some students data from database. And I have separate markup inside ajax for that data to display in the table. Now what I wanna do is to get the last inserted record or the latest record on the top but I have no I idea how to do that. I use sortByDesc() function but that does not work in this case. Below is my code. Help :)
Ajax Call
var classID = $(this).val();
if (classID) {
$.ajax({
url: '/attendance/ajax/' + classID,
type: "GET",
dataType: "json",
success: function (data) {
var table = $('table[id="studentsData"]');
table.DataTable().destroy();
var markup = '';
markup = '<thead><tr><th style="width: 2%" class="align-middle text-center"><input type="checkbox" id="options"></th><th style="width: 15%" class="text-center">Student ID</th> <th style="width: 15%" class="text-center">Student Name</th> <th style="width: 15%" class="text-center">Attendance</th> <th style="width: 15%" class="text-center">Date</th> <th style="width: 15%;" class="align-middle text-center">Actions</th> </tr></thead><tbody>';
$.each(data, function (key, value) {
markup += '<tr> <td><input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="' + value.id + '"></td> <td class="text-center align-middle"><input type="hidden" value="' + value.student_id + '" name="student_id[]">' + value.student_id + '</td> <td class="text-center align-middle"><input type="hidden" value="' + value.first_name + '" name="first_name[]"><input type="hidden" value="' + value.last_name + '" name="last_name[]">' + value.first_name + ' ' + value.last_name + '<td class="text-center align-middle"><input type="hidden" value="' + value.attendance + '" name="attendance[]">' + value.attendance + '</td>' + '<td class="text-center align-middle"><input type="hidden" value="' + value.date + '" name="date[]">' + value.date + '</td>' + '<td style=" width=12%" class="text-center"> <a data-toggle="modal" data-target="#editAttendanceModal' + value.id + '"><button title="Edit" class="btn btn-primary"><span class="fas fa-pencil-alt"></span></button></a> <a data-toggle="modal" data-target="#deleteAttendanceModal' + value.id + '"><button title="Delete" class="btn btn-danger"><span class="fas fa-trash-alt"></span></button></a> </td>' + '</td> </tr>';
});
markup += '</tbody>';
var table = $('table[id="studentsData"]');
table.html(markup);
table.DataTable();
}
});
}
});
**Controller**
public function myAttendanceAjax($id) {
$students_register = StudentsAttendance::where('class_id', $id)->get();
return json_encode($students_register);
}
You can use orderBy('id', 'desc')->get(); for geting latest record.
There is a method latest() defined in Illuminate\Database\Query\Builder Class.
public function latest($column = 'created_at')
{
return $this->orderBy($column, 'desc');
}
So, It will just orderBy with the column you provide in descending order with the default column will be created_at.
For more information about sorting have a look at https://laravel.com/docs/5.8/queries#ordering-grouping-limit-and-offset
I have a problem in getting the actual values from ids. I can't understand the issue because while storing these values I am getting them using Ajax in a dropdown where it stores its ids from the dropdown. But when I am trying to get these values back, then it shows me those ids and not the actual values. I can't understand how I can get the actual values from these ids. I also plucked them in my index method in the controller, but that's not working because it's Ajax. Below is my code, please let me know if you want to know anything more about this.
Controller actions for creating the form.
public function create()
{
$classes = StudentsClass::pluck('class_name', 'id')->all();
$rep_cat = ReportCtegories::pluck('name', 'id')->all();
return view('admin.reports.create', compact('classes', 'rep_cat'));
}
public function getStudentId($id)
{
$students = DB::table("students")->where("students_class_id", $id)->pluck("student_id", "id");
return json_encode($students);
}
public function getStudentName($id)
{
$students = DB::table("students")->select("id", DB::raw("CONCAT(first_name, ' ', last_name) as name"))
->where("students_class_id", $id)->pluck("name", "id");
return json_encode($students);
}
Ajax for getting values.
<script>
$(document).ready(function () {
//FOR LOADING STUDENTS
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('select[name="class_id"]').on('change', function () {
var classID = $(this).val();
if (classID) {
$.ajax({
url: '/reports/ajax/' + classID,
type: "GET",
dataType: "json",
success: function (data) {
var markup = '';
markup = '<thead><tr class="filters"><th style="width: 2%" class="align-middle text-center"><input type="checkbox" id="options"></th><th style="width: 15%" class="text-center">Student ID<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Student Name<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Report Category<input type="text" class="form-control" disabled></th> <th style="width: 15%;" class="align-middle text-center">Actions</th> <tr></thead><tbody>';
$.each(data, function (key, value) {
markup += '<tr> <td><input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="' + value.id + '"></td> <td><input type="hidden" value="' + value.student_id + '" name="student_id[]">' + value.student_id + '</td> <td><input type="hidden" value="' + value.student_name + '" name="student_name[]">' + value.student_name + '<td><input type="hidden" value="' + value.report_categories_id + '" name="report_categories_id[]">' + value.report_categories_id + '</td>' + '<td style=" width=12%" class="text-center"> <a data-toggle="modal" data-target="#editAttendanceModal' + value.id + '"""><button title="Edit" class="btn btn-outline-primary"><span class="fas fa-pencil-alt"></span></button></a> </td>' + '</td> <tr>';
});
markup += '</tbody>';
$('table[id="studentsData"]').html(markup);
}
});
}
});
});
</script>
First of all i recommend you use,
return response()->json($students)
Second pluck only return an array of ids(in your case)
I think you can try with this.
public function getStudentId($id)
{
$students = DB::table("students")->where("students_class_id", $id)->get();
return response()->json($students);
}
I'm using Ajax for CRUD. How can I use an associative array for inserting data with Ajax, so I don't have to reload the page and to append the inserted data below the existing data?
Here's The JS
function addTextBox(section, id_group) {
row_section = "#row_" + section;
wrapper = $(row_section);
// $(wrapper).append('<div class="movement_group"><input type="text" class="title_group" name="title_detail[' + section + ']" placeholder="Input" required="" style="width: 300px" /><button type="button" style="margin-left:10px;" class="btn btn-sm sending">Ok</button><button type="button" onClick="remove_field(\'' + section + '\')" style="margin-left:10px;" class="btn btn-sm remove_content">X</button></div>'); //add input box
// $(wrapper).append('<ul id="' + section + '" class="input_fields" style="list-style-type:none"><li><div class="movement_group"><input type="text" class="title_group" name="title_detail[\'' + section + '\']" placeholder="Input" required="" style="width: 300px" /><button type="button" style="margin-left:10px;" class="btn btn-sm sending">Ok</button><button type="button" onClick="remove_field(\'' + section + '\')" style="margin-left:10px;" class="btn btn-sm remove_content">X</button></div></li></ul>'); //add input box
$(wrapper).append('<ul id="fail_' + section + '" class="input_fields" style="list-style-type:none"><li><div class="movement_group"><input type="text" id="txt_' + section + '" id_group = "' + id_group + '"class="title_group" name="title_detail[\'' + section + '\']" placeholder="Input" required="" style="width: 300px" /><button type="button" style="margin-left:10px;" class="btn btn-sm sending" onClick="saveTitleGroup(\'' + section + '\', \'' + id_group + '\')">Ok</button><button type="button" onClick="remove_field(\'' + section + '\')" style="margin-left:10px;" class="btn btn-sm remove_content">X</button></div></li></ul>'); //add input box }
function remove_field(section) {
section_id = "#fail_" + section;
$(section_id).remove(); }
function saveTitleGroup(section, id_group) {
title_group = "#txt_" + section;
title_group = $(title_group).val();
td_section = "#td_" + section;
row_section = "#row_section" + section;
$.ajax({
type: "POST",
url: adminUrl+"/interest/save_detail",
data: {title: title_group, interest_group_id: id_group},
dataType: "text",
cache:false,
success:
function(data, textStatus, jqXHR){
console.log('test');
console.log(row_section);
/*$('.movement').fadeOut(800, function(){
$('.movement').fadeIn().delay(500);
// $('.movement').reload('http://127.0.0.1/camtravel-web/administrator/interest');
// return data;
window.location.reload();
});*/
// console.log(row_section);
// console.log(title_group);
// $('#tableinterest').html('');
$(td_section).html(data);
// $(row_section).html(''');
}
}); }
View
<table id="tableinterest" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
$n=0;
foreach ($interest_groups as $interest_group) {
$n++;
echo "<tr>";
echo "<td>";
// echo $interest_group['id'] . ". "
echo $n . ". ";
?>
</td>
<?php
echo "<td id='td_section_$interest_group[id]' group-id='$interest_group[id]'>";
// echo "<div id='title_group_'></div>";
echo "<b style='font-size:11pt !important;'>" . $interest_group['title'] . "</b>" . "<span style='margin-left:850px; font-size:15pt;' class='glyphicon glyphicon-plus interest_add' data-toggle='tooltip' data-placement='top' title='Add Detail' onclick='addTextBox(\"section_$interest_group[id]\", \"$interest_group[id]\")'></span><button type='button' style='float:right;' class='btn btn-xs text-red btn-delete-group' data-id='$interest_group[id]' data-category='$interest_group[title]'><span class='glyphicon glyphicon-minus'></span></button> ";
echo "<br>";
echo "</br>";
foreach ($interest_details as $interest_detail) {
echo "<ul style='list-style-type:none'>";
if ($interest_detail['interest_group_id'] == $interest_group['id']) {
echo "<li id='" . $interest_detail['id'] . "'>" . "<label style='font-size:10pt !important;' data-title=\"$interest_detail[title]\" id=\"$interest_detail[id]\" detail_id=\"$interest_group[id]\" class='editme1' id-title='\"$interest_group[id]\"'>" . $interest_detail['title'] . "</label>" .
"<button type='button' style='float:right;' class='btn btn-xs btn-delete-interest text-red' data-id='$interest_detail[id]' data-category='$interest_detail[title]'><span class='glyphicon glyphicon-minus'></span></button>" . "</li>";
}
echo "</ul>";
}
/*echo "<ul style='list-style-type:none'>";
echo "<li id='row_section_$interest_group[id]' class='input_fields'>" . "</li>";
echo "</ul>";*/
echo "<div id='row_section_$interest_group[id]'></div>";
echo "</td>";
// echo "<tr class='input_content'>";
// echo "</tr>";
echo "</tr>";
}
?>
</tbody>
</table>
Controller
function save_detail(){
// $id = $this->input->post('interest_group_id');
$title = $this->input->post('title');
$save = $this->save = $this->interest_group->save();
if ($save) {
echo $title;
// redirect('administrator/interest','refresh');
$this->layout = false;
} else {
echo "save failed";
// $this->layout = false;
}
// $title = $this->input->post('id-title');
// echo $title;
}
Model
function save(){
$id = $this->input->post('id');
// date_default_timezone_set('Asia/Jakarta');
$data = array(
"interest_group_id" => $this->input->post('interest_group_id'),
"title" => $this->input->post('title'),
"created_at" => date("Y-m-d H:i:s"),
"updated_at" => date("Y-m-d H:i:s"),
);
if ($id == null) {
$save = $this->db->insert('interest_detail',$data);
if ($save) {
return true;
$data = array();
$data['interest_group_id'] = 'interest_group_id';
$data['title'] = 'title';
// $data['title'] =
$data = array("data" => $data);
$data = json_encode($data);
$interest_group = $this->get_interest_group_by_id($this->input->post('interest_group_id'));
$interest_details = $this->get_interest_detail_by_id($this->input->post('interest_group_id'));
echo "<b>" . $interest_group['title'] . "</b>" . "<span style='margin-left:875px;' class='glyphicon glyphicon-plus interest_add' data-toggle='tooltip' data-placement='top' title='Add Detail' onclick='addTextBox(\"section_$interest_group[id]\", \"$interest_group[id]\")'></span><button type='button' style='float:right;' class='btn btn-xs text-red btn-delete-group' data-id='$interest_group[id]' data-category='$interest_group[title]'><span class='glyphicon glyphicon-minus'></span></button> ";
echo "<br>";
echo "<br>";
foreach ($interest_details as $interest_detail) {
echo "<ul style='list-style-type:none'>";
if ($interest_detail['interest_group_id'] == $interest_group['id']) {
echo "<li>" . "<label class='editme1'>" . $interest_detail['title'] . "</label>" .
"<button type='button' style='float:right;' class='btn btn-xs btn-delete-interest text-red' data-id='$interest_detail[id]' data-category='$interest_detail[title]'><span class='glyphicon glyphicon-minus'></span></button>" . "</li>";
}
echo "</ul>";
}
echo "<div id='row_section_$interest_group[id]'></div>";
print_r($interest_group);
print_r($interest_details);
} else {
return false;
}
} else {
$this->db->where('id', $id);
$update = $this->db->update('interest_detail', $data);
if ($update) {
return true;
} else {
return false;
}
}
}
Use single quotes when defining a string $string = '<div class"dre"></div>' and associative arrays need quotes names $array['name'] not $array[name] and last properly concatenate the strings and variables.
Try this:
<table id="tableinterest" class="table table-bordered table-striped">
<thead>
<tr>
<th>No</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
$n=0;
foreach ($interest_groups as $interest_group) {
$n++;
echo "<tr>";
echo "<td>";
// echo $interest_group['id'] . ". "
echo $n . ". ";
?>
</td>
<?php
echo "<td id='td_section_".$interest_group['id']."' group-id='".$interest_group['id']."'>";
echo "<b style='font-size:11pt !important;'>" . $interest_group['title'] . "</b>" . '<span style="margin-left:850px; font-size:15pt;" class="glyphicon glyphicon-plus interest_add" data-toggle="tooltip" data-placement="top" title="Add Detail" onclick="addTextBox(section_'.$interest_group['id'].','.$interest_group['id'].')"></span><button type="button" style="float:right;" class="btn btn-xs text-red btn-delete-group" data-id='.$interest_group['id'].' data-category="'.$interest_group['title'].'"><span class="glyphicon glyphicon-minus"></span></button> ';
echo "<br>";
echo "</br>";
foreach ($interest_details as $interest_detail) {
echo "<ul style='list-style-type:none'>";
if ($interest_detail['interest_group_id'] == $interest_group['id']) {
echo "<li id='" . $interest_detail['id'] . "'>" . "<label style='font-size:10pt !important;' data-title=\"".$interest_detail['title']."\" id=\"".$interest_detail['id']."\" detail_id=\"".$interest_group['id']."\" class='editme1' id-title='\"".$interest_group['id']."\"'>" . $interest_detail['title'] . "</label>" .
"<button type='button' style='float:right;' class='btn btn-xs btn-delete-interest text-red' data-id='$interest_detail[id]' data-category='$interest_detail[title]'><span class='glyphicon glyphicon-minus'></span></button>" . "</li>";
}
echo "</ul>";
}
echo "<div id='row_section_".$interest_group['id']."'></div>";
echo "</td>"
echo "</tr>";
}
?>
</tbody>
</table>
I have a problem with my website. I made the lines where my problem lays thick(bottom). Here it is:
" **result = '<p style="color:white;"><?php echo** JText::_('VG_SK_CONTACT_SUCCESS'); ?></p>'; } else {
**result = '<p style="color:white;"><?php echo**
"
If I write between >< "Thank you for..." then it says in the automatic answer "Thank you for..." and there is another sentence which is maybe called by a function. I don't know very well programming, so can someone tell me maybe where I could find this data file, where I can change the automatic answer.
//CODE
// no direct access defined('_JEXEC') or die;
//library jimport('joomla.application.module.helper');
//vars //$class_sfx = htmlspecialchars($params->get('moduleclass_sfx')); $c_emailto = explode( '#', $params->get('emailto') ); $c_justdata = $params->get('justdata'); $c_justsocial = $params->get('justsocial');
echo '<div class="contactform"> <span class="error"></span> <span class="error"></span> <span class="error"></span> <span class="error"></span>
<form id="contactForm" method="post" action="">
<input type="hidden" name="emailto1" value="' . $c_emailto[0] . '" />
<input type="hidden" name="emailto2" value="' . $c_emailto[1] . '" />
<input type="text" name="contactName" id="contactName" class="requiredField" value="" placeholder="' . JText::_('VG_SK_CONTACT_NAME') . '" />
<input type="text" name="email" id="email" value="" class="requiredField email" placeholder="' . JText::_('VG_SK_CONTACT_EMAIL') . '" />
<textarea class="requiredField" name="comments" id="comments" placeholder="' . JText::_('VG_SK_CONTACT_MESSAGE') . '"></textarea>
<input type="hidden" name="submitted" id="submitted" value="true" />
<div class="clearfix"></div>
<button type="submit" name="submit" id="submitMsg" class="large_btn contact-btn">' . JText::_('VG_SK_CONTACT_SUBMIT') . '</button>
</form> <div id="note"></div>
</div>
<div class="contactinfo">
' . $c_justdata . ' ' . $c_justsocial . '
</div>'; ?>
<script> // mail-form jQuery(document).ready(function($){ $("#contactForm").submit(function(){ var str = $(this).serialize();
$.ajax({ type: "POST", url: "<?php echo JURI::base(); ?>modules/mod_circle_contact/ajax/send.php", data: str, success: function(msg){
$("#note").ajaxComplete(function(event, request, settings){ if(msg == 'OK') {
**result = '<p style="color:white;"><?php echo** JText::_('VG_SK_CONTACT_SUCCESS'); ?></p>'; } else {
**result = '<p style="color:white;"><?php echo** JText::_('VG_SK_CONTACT_FORGOT'); ?></p>'; }
$(this).html(result).fadeIn("slow"); $(this).html(result);
}); //alert(msg);
}
}); return false; }); }); </script>
//CODE
Thank you, greetings
You should change the strings inside the JText::_('VG_SK_CONTACT_FORGOT'); that is the VG_SK_CONTACT_FORGOT inside en-GB.tpl_vg_grettla.ini see this relevant answer: Customize Joomla Text
I have modified magento extension for tier pricing and added three text fields in app->design->adminhtml->default->default->catalog->product->edit->price->tier.phtml file.
Data from three new field successfully inserted into table catalog_product_entity_tier_price.
Data is also visible on frontend. But problem is that in tier box price inserted data and field mismatched
<table>
<thead>
<tr class="headings">
<th>Website</th>
<th>Customer Group</th>
<th>Qty</th>
<th>Custom_field_1</th>
<th>Custom_field_2</th>
<th>Price</th>
<th>Custom_field_3</th>
<th>Action</th>
</thead>
</tr>
<tr>
<td>Showing Correct</td>
<td>Showing Correct</td>
<td>Showing Correct</td>
<td>Showing Custom_field_2</td>
<td>Showing Custom_field_3</td>
<td>Showing Correct</td>
<td>undefined</td>
<td> </td>
</tr>
</table>
<p> </p>
<p> </p>
<b>And javascript code is</b>
<script type="text/javascript">
var tierPriceRowTemplate = '<tr>'
+ ' <small class="nobr"><?php echo Mage::helper("catalog")->__("and above")?></small></td>'
+ '<td><input class="<?php echo $_htmlClass ?> Custom_field_1" type="text" name="<?php echo $_htmlName ?>[{{index}}][Custom_field_1]" value="{{Custom_field_1}}"
id="tier_price_row_{{index}}_Custom_field_1" /></td>'
+ '<td><input class="<?php echo $_htmlClass ?> Custom_field_2" type="text" name="<?php echo $_htmlName ?>[{{index}}][Custom_field_2]" value="{{Custom_field_2}}" id="tier_price_row_{{index}}_Custom_field_2" /></td>'
+ '<td><input class="<?php echo $_htmlClass ?> required-entry <?php echo $_priceValueValidation ?>" type="text" name="<?php echo $_htmlName ?>[{{index}}][price]" value="{{price}}" id="tier_price_row_{{index}}_price" /></td>'
+ '<td><input class="<?php echo $_htmlClass ?> Custom_field_3" type="text" name="<?php echo $_htmlName ?>[{{index}}][Custom_field_3]" value="{{Custom_field_3}}" id="tier_price_row_{{index}}_Custom_field_3"/></td>';
var data = {
website_id: '<?php echo $this->getDefaultWebsite() ?>',
group: '<?php echo $this->getDefaultCustomerGroup() ?>',
qty: '',
Custom_field_1:'',
price: '',
Custom_field_2:'',
Custom_field_3:'',
readOnly: false,
index: this.itemsCount++
};
//alert(Custom_field_1);
if(arguments.length >= 4) {
data.Custom_field_1 = arguments[4]
data.readOnly = arguments[5];
data.Custom_field_2 = arguments[7];
data.Custom_field_3 = arguments[8];
}
if (arguments.length == 5) {
data.readOnly = arguments[4];
}
$('tier_price_row_' + data.index + '_Custom_field_1').value = data.Custom_field_1;
$('tier_price_row_' + data.index + '_Custom_field_2').value = data.Custom_field_2;
$('tier_price_row_' + data.index + '_Custom_field_3').value = data.Custom_field_3;
</script>
Problem Solved: Problem was in jquery code.
Please check my edited code
if(arguments.length >=8) {
data.website_id = arguments[0];
data.group = arguments[1];
data.qty = arguments[2];
data.product_type = arguments[3]
data.ourprice = arguments[4];
data.price = arguments[5];
data.links = arguments[6];
data.readOnly = arguments[7];
}
if (arguments.length == 9) {
data.readOnly = arguments[8];
}
Element.insert($('<?php echo $_htmlId ?>_container'), {
bottom : this.template.evaluate(data)
});
$('tier_price_row_' + data.index + '_cust_group').value = data.group;
$('tier_price_row_' + data.index + '_website').value = data.website_id;