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>
Related
I want to fetch the corresponding value of my dropdown the image below shows the form and the database. the table was inside the javascript and Iam having a dificult time in figuring out this problem. please help me how to solve this problem... Thank you very much in advance...
Database
forms entry
My Controller
function getFeetypeEndDate() {
$feetype_id = $this->input->get('feety_id[]');
$data = $this->feegroup_model->getFeetypeByEndDate($feetype_id);
echo json_encode($data);
}
My Model
public function get($id = null) {
$this->db->select()->from('feetype');
$this->db->where('is_system', 0);
if ($id != null) {
$this->db->where('id', $id);
} else {
$this->db->order_by('id');
}
$query = $this->db->get();
if ($id != null) {
return $query->row_array();
} else {
return $query->result_array();
}
}
My Javascript in the View Section
<script>
$(document).ready(function (){
$("body").on('click', '.btn-add-more', function (e) {
e.preventDefault();
var amount = $("#amount").val();
var penalty = $("#penalty").val();
var $sr = ($(".jdr1").length + 1);
var rowid = Math.random();
var $html = '<tr class="jdr1" id="' + rowid + '">' +
'<td><span class="btn btn-sm btn-default">' + $sr + '</span><input type="hidden" name="count[]" value="'+Math.floor((Math.random() * 10000) + 1)+'"></td>' +
'<td><select id="feetype_id[]" name="feetype_id[]" class="form-control" >' +
'<?php foreach ($feetypeList as $feetype) { ?>' +
' <option value="<?php echo $feetype['id'] ?>" ' +
'<?php if (set_value('feetype_id[]') == $feetype['id']) { echo "selected =selected"; } ?>><?php echo $feetype['type'] ?></option> <?php $count++; } ?> </select></td>' +
'<td><input type="text" id="startDate" name="startDate" value="<?php echo date($this->customlib->getSchoolDateFormat($feetype->start_date), $this->customlib->dateyyyymmddTodateformat($feetype->start_date)); ?>" placeholder="Start Date" class="form-control input-sm"/></td>' +
'<td><input type="text" id="endDate" name="endDate" value="<?php echo date($this->customlib->getSchoolDateFormat($feetype->end_date), $this->customlib->dateyyyymmddTodateformat($feetype->end_date)); ?>" placeholder="End Date" class="form-control input-sm"/></td>' +
'<td><input type="text" name="amount_td[]" placeholder="Amount" class="form-control input-sm" value="'+amount+'"></td>' +
'<td><input type="text" name="penalty_td" placeholder="Penalty" class="form-control input-sm" value="'+penalty+'"></td>' +
'</tr>';
$("#table-details").append($html);
});
$("body").on('click', '.btn-remove-detail-row', function (e) {
e.preventDefault();
if($("#table-details tr:last-child").attr('id') != 'row1'){
$("#table-details tr:last-child").remove();
}
});
});
These are my databases, where a division can have many districts (division_id is the foreign key in districts table).
When I submit the modal (using Ajax with laravel) the division name comes as undefined.
However, after I refresh the browser, everything seems to be working okay. Why is this happening and how do I fix it?
This is the code I am using to show the data.
{{ csrf_field() }}
<?php $no=1; ?>
#foreach ($district as $district)
<tr class="post{{$district->id}}">
<td>{{ $no++ }}</td>
<td>{{ $district->division->name}}</td>
<td>{{ $district->code}}</td>
<td>{{ $district->name}}</td>
<td>{{ $district->created_at}}</td>
<td>
<a href="#" class="show-modal btn btn-info btn-sm" data-id="{{$district->id}}" data-division_id="{{$district->division->name}}" data-code="{{$district->code}}" data-name="{{$district->name}}" >
<i class="fa fa-eye"></i>
</a>
<a href="#" class="edit-modal btn btn-warning btn-sm" data-id="{{$district->id}}" data-division_id="{{$district->division->name}}" data-code="{{$district->code}}" data-name="{{$district->name}}" >
<i class="glyphicon glyphicon-pencil"></i>
</a>
<a href="#" class="delete-modal btn btn-danger btn-sm" data-id="{{$district->id}}" data-division_id="{{$district->division->name}}" data-code="{{$district->code}}" data-name="{{$district->name}}" >
<i class="glyphicon glyphicon-trash"></i>
</a>
</td>
</tr>
#endforeach
This is my controller.
use Illuminate\Http\Request;
use App\Division;
use App\District;
use Validator;
use Response;
use Illuminate\Support\Facades\Input;
use App\http\Requests;
class DistrictController extends Controller
{
public function index()
{ $district = District::all();
$divisionDistricts = Division::pluck('name','id');
return view('masterForms.district',compact('district','divisionDistricts'));
}
public function store(Request $request)
{
if($request->ajax())
{
$district = District::create($request->all());
$district->save();
return response($district);
}
}
This is my District Model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Division;
class District extends Model
{
protected $fillable = ['code','name','division_id'];
public function division()
{
return $this->belongsTo(Division::class);
}
}
?>
And this is the javaquery I am using to add my data to the database.
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="token"]').attr('content')
}
});
$(document).on('click','.create-modal', function() {
$('#create').modal('show');
$('.form-horizontal').show();
$('.modal-title').text('Add District');
});
$('#ddistrict').on('submit',function(e){
e.preventDefault();
var data = $(this).serialize();
var url = $(this).attr('action');
var post = $(this).attr('method');
$.ajax({
type: post,
url: url,
data: data,
dataTy: 'json',
success:function(data)
{
$('.error').remove();
$('#table').append("<tr class='post" + data.id + "'>"+
"<td>" + data.id + "</td>"+
"<td>" + data.division_id.name + "</td>"+
"<td>" + data.code + "</td>"+
"<td>" + data.name + "</td>"+
"<td>" + data.created_at + "</td>"+
"<td><button class='show-modal btn btn-info btn-sm' data-id='" + data.id + "' data-division_id.name='" +
data.division_id.name + "' data-code='" +
data.code + "' data-name='" +
data.name + "'><span class='fa fa-eye'></span></button> <button class='edit-modal btn btn-warning btn-sm' data-id='" + data.id +"' data-division_id.name='" +
data.division_id.name + "' data-code='" +
data.code + "' data-name='" +
data.name + "'><span class='glyphicon glyphicon-pencil'></span></button> <button class='delete-modal btn btn-danger btn-sm' data-id='" + data.id + "' data-division_id.name='" +
data.division_id.name + "' data-code='" +
data.code + "' data-name='" +
data.name + "' ><span class='glyphicon glyphicon-trash'></span></button></td>"+
"</tr>");
}
});
})
</script>
Just returning $disrtict will just give you division_id not division name. And you can't use data.division_id.name in your javascript.
To return division you must append division name with your other attributes of districts. Or you might use Api Resources to provide your custom json response.
To append division name attribute in you district you should use $appends variable.
class District extends Model
{
protected $appends = ['division_name'];
protected $fillable = ['code','name','division_id'];
public function division()
{
return $this->belongsTo(Division::class);
}
public function getDivisionNameAttribute(){
return $this->division->name;
}
}
Now in your ajax resopnse use
success:function(data)
{
$('.error').remove();
$('#table').append("<tr class='post" + data.id + "'>"+
"<td>" + data.id + "</td>"+
"<td>" + data.division_name + "</td>"+
"<td>" + data.code + "</td>"+
"<td>" + data.name + "</td>"+
"<td>" + data.created_at + "</td>"+
"<td><button class='show-modal btn btn-info btn-sm' data-id='"+ data.id + "' data-division_id='" +
data.division_name + "' data-code='" +
data.code + "' data-name='" +
data.name + "'><span class='fa fa-eye'></span></button> <button class='edit-modal btn btn-warning btn-sm' data-id='" + data.id +"' data-division_id.name='" +
data.division_name + "' data-code='" +
data.code + "' data-name='" +
data.name + "'><span class='glyphicon glyphicon-pencil'></span></button> <button class='delete-modal btn btn-danger btn-sm' data-id='" + data.id + "' data-division_name='" +
data.division_name + "' data-code='" +
data.code + "' data-name='" +
data.name + "' ><span class='glyphicon glyphicon-trash'></span></button></td>"+
"</tr>");
}
I have created a custom post "news" in Wordpress and I want to search posts and paginate them without page reload.
I.m using $wpdb class and it is now working fine but when I try to do an ajax call I don't get any results.
Here is the error I am getting:
<b>Notice</b>: Undefined variable: parent_slug in
<b>C:\xampp\htdocs\website\wp-content\themes\website\includes\news\output_more-news.php</b> on line <b>14</b>
<b>Fatal error</b>:
Call to undefined function get_terms() in <b>C:\xampp\htdocs\website\wp-content\themes\website\includes\news\output_more-news.php</b> on line <b>15</b>
here's my ajax code:
jQuery( document ).ready(function() {
jQuery('#news_form').on('submit', function(e) {
var
that = jQuery(this),
url = "http://localhost/website/wp-content/themes/website/includes/news/output_more-news.php",
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index,value){
var
that = jQuery(this),
name = that.attr('name'),
value = that.val();
data[name]= value;
console.log(data);
});
jQuery.ajax({
url: url,
type: type,
data: data,
beforeSend : function (){
//do something like loading gif
},
success: function(data){
alert("form submited");
console.log(data);
},
error:function(){
alert("error");
}
});//ajax call
e.preventDefault()
});
here's output_more-news.php
$search_term = "%" . strtolower($_POST['name_search'])."%";
if(!empty($_POST['archive-dropdown'])){
$archive_selected = $_POST['archive-dropdown'];
}else{
$archive_selected = "";
}
$taxonomy = ucfirst($parent_slug);
foreach(get_terms('news_categories') as $term){
if($term->slug == $parent_slug){
$term_ID = $term->term_id;
}
}
$home_url = get_home_url();
$site_directory_url = get_template_directory_uri();
$archive_selected = $_POST['archive-dropdown'];
$archive_selected = str_replace("$home_url","",$archive_selected );
$archive_selected = str_replace("?post_type=news","",$archive_selected );
$archive_date = preg_split("/[\/]+/",$archive_selected );
$archive_month = $archive_date[2];
$archive_year = $archive_date[1];
global $wpdb;
$more_news_query =
"
SELECT $wpdb->posts.ID
FROM $wpdb->posts
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
AND $wpdb->term_taxonomy.taxonomy = 'news_categories'
AND $wpdb->term_taxonomy.term_id IN ($term_ID)
WHERE $wpdb->posts.post_title LIKE '$search_term'
";
if(!empty($archive_selected)){
$more_news_query .=
"
AND month($wpdb->posts.post_date) = '$archive_month'
AND year($wpdb->posts.post_date) = '$archive_year'
";
}
$more_news_query .=
"
AND $wpdb->posts.post_type = 'news'
AND $wpdb->posts.post_status = 'publish'
ORDER BY $wpdb->posts.post_date
";
$count_results = "SELECT COUNT(1) FROM (${more_news_query}) AS combined_table";
$total = $wpdb->get_var($count_results);
$items_per_page = get_option( 'posts_per_page' );
$paged = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $paged * $items_per_page ) - $items_per_page;
$max_num_pages = ceil($total / $items_per_page);
$more_news = $wpdb->get_results( $more_news_query . " LIMIT ${offset}, ${items_per_page}" );
$prev_link = $paged - 1;
if($prev_link <= 0){
$prev_link = 1;
}
$nextlink = $paged + 1;
if($nextlink >= $max_num_pages){
$nextlink = $max_num_pages;
}
echo"<div class='panel-group' id='accordion'>
<div class='panel panel-default'>
<div class='panel-heading'>
<h4 class='panel-title''>
<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion' href='#collapseMore'>
<span>More News</span>
</a>
<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion' href='#collapseMore'>
<i class='pull-right fa fa-plus-circle' aria-hidden='true'></i>
</a>
</h4>
</div>";
echo "<div id='collapseMore' class='panel-collapse collapse '>";
echo "<div class='panel-body'>";
echo "<div class='search_bar clearfix'>";
echo "<div class='col-lg-10 col-md-9 col-sm-8 col-xs-7'>
<form id='news_form' method='post' action='' class='clearfix'>
<div class='form_fields_container col-md-7'>
<span class='btns cut_corner_btns news_search_label'>News</span><input placeholder='Search' type='text' name='name_search'>
<i class='fa fa-search' aria-hidden='true'></i>";
?>
</div>
<div class='form_fields_container select_container col-md-5'>
<span class="btns cut_corner_btns news_search_label"><?php echo strtoupper($parent_slug);?> archives</span>
<select name="archive-dropdown" >
<option value=""></option>
<?php wp_get_archives( array( 'news_categories' => $taxonomy, 'type' => 'monthly', 'format' => 'option', 'post_type'=>'news', 'show_post_count' => false ) ); ?>
</select>
</div>
</form>
</div>
<?php
echo "<div class='col-lg-2 col-md-3 col-sm-4 col-xs-5 news-pagination-container'>";
echo"<div class='news-pagination pull-right'>
<span>Page</span>
<a class='page-numbers current' href='$home_url/$parent_slug/news/?cpage=$paged'>$paged</a>
<span> of</span>
<a class='page-numbers' href='$home_url/$parent_slug/news/?cpage=$max_num_pages'>$max_num_pages </a>";
echo "<a class='prev page-numbers' href='$home_url/$parent_slug/news/?cpage=$prev_link'><i class='fa fa-angle-left' aria-hidden='true'></i></a>";
echo" <a class='next page-numbers' href='$home_url/$parent_slug/news/?cpage=$nextlink'><i class='fa fa-angle-right' aria-hidden='true'></i></a>";
echo "</div>";
echo "</div>";
echo "</div>";
if(!empty($total)) :
foreach($more_news as $news) {
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($news->ID, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
$date = get_the_date("Y-m-d ",$news->ID);
$title = get_the_title($news->ID);
$content = get_the_content($news->ID);
$content = substr($content,0,30);
$permalink = get_the_permalink();
echo"<div class='col-md-6 col-sm-12 col-xs-12 more_post_container'>";
echo"<div class='more_post_thumb col-xs-4'><img class='img-responsive' src='$thumb_url'/></div>";
echo"<div class='more_news_content col-xs-8'>
<p>$date</p>
<p class='color-$parent_slug '>$title</p>
<p class=''>$content...</p>
<a class='more_post_permalink' href='$permalink'>more</a>
</div>";
echo "</div>";
}
echo"</div>";
echo"</div>";
else:
echo "no results found!";
endif;
echo"<div id='response'></div>";
echo"</div>";
echo"</div>";
?>
Any help will be much appriciated.
Thanks in advance
You have 2 ways to resolve the error
Adding require '../../../wp-load.php'in output_more-news.php, which will manually load in WordPress corefiles, but its a bad way.
The correct way is documented here
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.
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;