I have created dynamic rows with type(select),name(text),unit(select), I was writing the code for on change type's select.
But when I change the value from type field it had load the value into current row name field and load option dynamically into current unit select field,
Loading value into active rows is working fine, but loading options into select unit is not.
My code is given below
$(document).on('change', '.raw', function () {
// var r = this.val();
if ($(this).val() != 0) {
var t = $(this).parents('tr').find(".raw :selected").text();
$(this).parents('tr').find(".name").val(t);
where I need help is given below
$(this).parents('tr').find(".amount").html(data); -> here i need help
// $(".amount").html(data); ->this working fine but change all the select value
Thanks in advance.
here my full coding for my table
<table id="addingtable" class="table table-bordered">
<tr>
<th>Raw Master</th>
<th>Name</th>
<th>Display</th>
<th>Value</th>
<th>Unit</th>
<th>Delete</th>
</tr>
<tr>
<td><select name='rawmaster[]' class = 'form-control raw' required><option>Select</option><?php echo rawmaster_item($conn,$type); ?><option value="0">None</option></select></td>
<td><input type='text' name='name[]' placeholder="Name" class = 'form-control name' required /></td>
<td><select name='des[]' class = 'form-control' required><option>Select</option><option Value = 'y'>Yes</option><option Value = 'n'>No</option></select></td>
<td><input type='text' name='value[]' placeholder="Value" class = 'form-control' required /></td>
<td><select name='amount[]' class = 'form-control amount' required><option>Select</option><?php echo $output2; ?></select></td>
<td><button type='button' class='btn btn-icon btn-danger remove'><i class='feather icon-trash-2'></i></button></td>
</tr>
my jquery for adding rows
$(document).on("click", ".addrows", function (event) {
{
// var planid = $("#plan_id").val();
// alert(planid);
var html = '';
html += "<tr>";
html += "<td><select name='rawmaster[]' class = 'form-control raw'><option>Select</option><?php echo rawmaster_item($conn,$type); ?><option value = '0'>None</option></select></td>";
html += "<td><input type='text' name='name[]' placeholder='Name' class = 'form-control name' /></td>";
html += "<td><select name='des[]' class = 'form-control'><option>Select</option><option Value = 'y'>Yes</option><option Value = 'n'>No</option></select></td>";
html += "<td><input type='text' name='value[]' placeholder='Value' class = 'form-control' /></td>";
html += " <td><select name='amount[] amount' class = 'form-control amount' required><option>Select</option><?php echo $output2; ?></select></td>";
html += "<td><button type='button' class='btn btn-icon btn-danger remove'><i class='feather icon-trash-2'></i></button></td>";
html += "</tr>";
$("#addingtable").append(html);
}
});
my jquery for onchange select values
$(document).on('change', '.raw', function () {
// var r = this.val();
if ($(this).val() != 0) {
var t = $(this).parents('tr').find(".raw :selected").text();
$(this).parents('tr').find(".name").val(t);
$(this).parents('tr').find(".name").attr("readonly", "TRUE");
var r = $(this).parents('tr').find(".raw").val();
// alert(r);
$.ajax({
url:"plandetails_getunit.php",
method:"POST",
data:"id="+r,
success:function(data)
{
if(data)
{
$(this).parents('tr').find(".amount").html(data); => need help here
// $(".amount").html(data);
}
}
});
}
else{
$(this).parents('tr').find(".name").val('');
$(this).parents('tr').find(".name").removeAttr("readonly");
var r = $("#ptype").val();
$.ajax({
url:"plandetails_getunit1.php",
method:"POST",
data:"id="+r,
success:function(data)
{
if(data)
{
// $(".amount").html(data);
$(this).parents('tr').find(".amount").val(); =>here remains work gud
}
}
});
}
});
I might be wrong but I think you need to set the context on the ajax request otherwise this will reference a different context (callback), adding context:this like this:
$.ajax({
url:"plandetails_getunit.php",
method:"POST",
data:"id="+r,
context:this,
success:function(data)
{
Related
Please help,
I have the data that I loaded with ajax, but I am confused, how when I enter the item code, the data generated in json form, into the table
view
<div class="form-group col-md-4">
<label for="field-1" class="control-label">KODE RUNSHEET</label>
<input name="kd_runsheet[]" class="form-control" id="kode" type="text" placeholder="Masukan Kode Runsheet" style="" required="">
<input name="id_runsheet" id="id_runsheet" class="form-control" type="hidden" style="">
</div>
after searching I want to enter the json results in the table
<table class="table table-bordered" style="width: 100%; cellspacing: 0;">
<thead>
<tr>
<th>Kode Outbound</th>
<th>Kode Resi</th>
</tr>
</thead>
<tbody id="isinya">
</tbody>
</table>
// search by ajax
$(document).ready(function(){
$('#kode').on('input',function(){
var kode=$(this).val();
$.ajax({
type : "POST",
url : "<?php echo base_url('backend/history_status/caris')?>",
dataType : "JSON",
data : {kd_runsheet: kode},
cache:false,
success: function(data) {
jmlData = data.length;
tampung = "";
for (a = 0; a < jmlData; a++) {
tampung += "<tr>" +
"<td>" + (a + 1) + "</td>" +
"<td>" + data[a]["kd_runsheet"] + "</td>" +
"<tr/>";
}
$("tbody")[0].innerHTML += tampung;
}
});
return false;
});
});
Result Json
[
{"id_runsheet":"6","kd_runsheet":"RUNSHEET-0000-0003"},
{"id_runsheet":"6","kd_runsheet":"RUNSHEET-0000-0003"}
]
Model
public function caridb($kode){
$query = $this->db->query("SELECT runsheet.id_runsheet,kd_runsheet FROM runsheet
INNER JOIN runsheet_detail ON runsheet.id_runsheet=runsheet_detail.id_runsheet WHERE kd_runsheet ='$kode'");
return $query->result();
}
Controller
public function caris(){
$id=$this->input->post('kd_runsheet');
$data=$this->M_history_status->caridb($id);
echo json_encode($data);
}
Try something like this. Your markup should be inside a loop.
success: function(data){
var markup = "<tr>
<td><input type='text'></td>
</tr>";
$("#isinya").append(markup);
}
You are getting JSON Object's array
Seperate each JSON Object using loop and add it to table:
success: function(data){
for(i=0;i<data.length;i++){
var row="<tr><td>"+data[i].id_runsheet+"</td><td>"+data[i].kd_runsheet+"</td></tr>";
$("#isinya").append(row); //Adding rows in <tbody>
}
}
I create table using ajax. When i click on button it will read the row and create a form using ajax.
Everything is fine here. But when i click back button on browser, the data is somehow posted/display back as FORM. How to delete/prevent this when click on back.
Thing i have read:
1) Clear input for form. But where is reading input because form is created using function submitRowAsForm when button is clicked
2) Disable autocomplete. But this doesnt seem relevant and not work.
$(document).ready(function update(){
$.ajax({
url: 'getSchedule.php',
success: function(data,status)
{
createTableByForLoop(data);
},
async: true,
dataType: 'json'
}).then(function() { // on completion, restart
setTimeout(update, 30000); // function refers to itself
});;
});
function createTableByForLoop(data)
{
var table = "\
<div class='table-scrollable'>\
<table class=\"table table-responsive\">\
<thead >\
<tr>\
<th class=\"d-none\">ID</th>\
<th class=d-none>SHOWSCHEDULE_ID ID</th>\
<th>TITLE</th>\
<th>TIME</th>\
<th>ACTION</th>\
</tr>\
</thead>\
<tbody>"
for(var i=0; i<data.length;i++)
{
table += "<tr id=\""+i+"\">";
table += "<td class=d-none><input type=text class=\"form-control transparent-input\" name=SHOWSCHEDULE_ID value=\""+data[i]['SHOWSCHEDULE_ID']+"\" readonly></td>";
table += "<td><input type=text class=\"form-control transparent-input\" name=SHOWSCHEDULE_SHOWTITLE value=\""+data[i]['SHOWSCHEDULE_SHOWTITLE']+"\" readonly></td>";
table += "<td><input type=text class=\"form-control transparent-input\" name=SHOWBEGINTIME value=\""+data[i]['SHOWBEGINTIME']+"\" readonly> - <input type=text class=\"form-control transparent-input\" name=\""+data[i]['SHOWENDTIME']+"\" value=\""+data[i]['SHOWENDTIME']+"\" readonly></td>";
table += "<td><button onclick=submitRowAsForm("+i+") id=btnRoom class=\"btn btn-outline-success\" >Room</button>\
<button onclick=submitRowAsForm("+i+") id=btnBook class=\"btn btn-outline-warning\" >Book</button></td>";
table += "</tr>";
}
table +="</tbody></table></div>";
$('#idShowSchedule').html(table);
}
function submitRowAsForm(id) {
form=document.createElement('form');
form.method='POST';
form.action='OrderTicket.php';
$("#"+id).children().each(function() {
$(this).children().each(function(){
$(this).clone().appendTo(form);
});
});
document.body.appendChild(form);
form.submit();
}
To protect against the back button I would set a dirty flag on your page. That way when it's set you know you have done this before. So
Check to see if your dirty flag is set
If not set your dirty flag
Load data because this is a fresh loading of the page.
Make sure that you do this after the DOM Content has Loaded otherwise this will fail in some browsers.
Here is an example of how I didn't this in one of my old projects
document.addEventListener("DOMContentLoaded", function (event) {
var dirty = 0;
var dirtyEl = document.getElementById('page_is_dirty')
if (!dirty && dirtyEl && (dirtyEl.value != "0" && dirtyEl.value != "")) {
dirty = 1;
}
else if (dirtyEl) {
dirtyEl.value = '1';
}
var el = document.getElementById('ClickApp');
if (el && !dirty)
el.innerHTML = "<div id='ClickApp' ng-view='' click-main><br /><br /><center><h2>Loading Calendar</h2><br /><span id='ErrorInstructions'></span><br /><div class='ui-progress-bar ui-container' id='progress_bar' style='width: 350px'><div id='progressBar' class='ui-progress' style='width: 0%; float: left'></div></div><br /><br /><small><span id='LastError'></span></small></center></div>";
else {
var dirtyMEl = document.getElementById('page_is_dirty_message');
var dirtyMSG = dirtyMEl ? dirtyMEl.value : "You arrived here by hitting the back button. Please start over.";
el.innerHTML = "<div><br /><br /><center><h2>" + dirtyMSG + "</h2><br /><span id='ErrorInstructions'></span><br /><div class='ui-progress-bar ui-container' id='progress_bar' style='width: 350px'><div id='progressBar' class='ui-progress' style='width: 0%; float: left'></div></div><br /><br /><small><span id='LastError'></span></small></center></div>";
}
if (!dirty) {
//Do your data loading. Page transformaions, whatever
...
}});
I am trying to make a form which let you see boarding times per day.
The table is created with PHP. Having a row for every day of the week, except Saturday and Sunday:
<?php
for ($i = 1; $i < 8; $i++){
$d=strtotime("+".$i." Day");
if (date("l", $d) !== "Saturday" && date("l", $d) !== "Sunday" ){
$daydate = $dayOfWeek[date("l",$d)] . date(" d ", $d) . $month[date("F",$d)];
echo "<tr>";
echo "<td>". $daydate . "</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo '<tr class="BoardingTimeMorning">';
echo '<td style="padding-left: 30px">
<input class="form-check-input" type="radio" name="'.$dayOfWeek[date("l",$d)].'-to" class="BoardingMorning" value=""></input> <div class="time">0:00</div>
</td>
<td>
<input class="form-check-input" type="radio" name="'.$dayOfWeek[date("l",$d)].'-to" class="BoardingMorning" value=""></input><div class="time">0:00</div>
</td>
<td>
<input class="form-check-input" type="radio" name="'.$dayOfWeek[date("l",$d)].'-to" class="BoardingMorning" value=""></input><div class="time">0:00</div>
</td>
<td>
<input class="form-check-input" type="radio" name="'.$dayOfWeek[date("l",$d)].'-to" class="BoardingMorning" value=""></input><div class="time">0:00</div>
</td>
</tr>';
}
}
?>
When someone selects their destination, the corresponding times need to show in the table above. Selection is done with AJAX request and returns the times in an array. No more than 4 boarding times are possible.
I am trying to get it working with JQuery, but nothing happens.
JQuery:
$('#boardingplace').change(function() {
$.ajax({
type: 'POST',
url: 'reservePage.php',
dataType: 'JSON',
data: {
'station': this.value
},
success: function(response){
var len = response.length;
console.log("length: "+len);
$( ".BoardingTimeMorning" ).each(function() {
for (var j=0; j<4; j++){
//visibitly off
var tdContent = $(this).find('.BoardingMorning'+j);
console.log (tdContent);
tdContent.css("display", "none");
tdContent.next().html("");
//reset value
tdContent.attr('value', "");
if(j - len <= 0){
//show content according to amount of times
tdContent.css('display', "block");
tdContent.attr('value', response[j].slice(0,5));
tdContent.next().html(response[j].slice(0,5));
}
}
});
}
});
});
What is the best way to get it to work?
EDIT:
Turns out I use 2 class definition on the input element. The bootstrap class and another for selecting it. I put them together and all works..
Thanks for all you help people! Really love this community!
There is an easy solution:
you can create a response.php file like this:
url used for example:
response.php?station=162
<?php
//response.php
$station=$_GET['station'];
$data_from_database=// your sql Request...
?>
<html>
<div class="datas">
<?php
foreach ($data_from_database as $data) {
echo "<span>".$data['time']."</span>";echo "<br>";
echo "<span>".$data['name']."</span>";echo "<br>";
}
?>
</html>
in your javascript you can load data from response.php file like this:
<script type="text/javascript">
$('#boardingplace').change(function() {
var station= this.value;
$.ajax({
type: 'GET',
url: 'response.php?station'+station,
success: function(response){
$(".BoardingTimeMorning").html(response);
}
});
});
</script>
and data will be loaded in div with class="BoardingTimeMorning"
I hope this could help you.
I have created a dynamic table using jquery as follows:
$.ajax({
data : data,
type : "get",
url : url,
dataType : "json",
error : function(resp){
alert("Error !!");
},
success : function(resp){
table = '';
$.each(resp,function(indx,obj){
table += '<tr>';
table += '<td>'+parseInt(indx+1)+'</td>';
table += '<td>'+'<input type="text" value="'+obj.ServiceDetail.service_code+'">'+'</td>';
table += '<td>'+'<input type="text" value="'+obj.ServiceDetail.name+'">'+'</td>';
table += '<td>'+'<input type="text" value="'+obj.ServicePrice.discount_price+'">'+'</td>';
table += '</tr>';
});
$("tbody#sevice_table_body").append(table);
}
});
and a button :
<input type="button" class = "btn btn-success btn-sm" value="submit" >
now i want to get all input value in a array by click submit button so that can be inserted in a database table using jquery ajax.
You can use this code for cycling the input and add them to an array
var arrayOfVar = []
$.each($("input[type='text']"),function(indx,obj){
arrayOfVar.push($(obj).val());
});
You can use .serializeArray() it Encode elements as an array of names and values.
Find below fiddle for more info
$(function() {
var data = $("#tbl2 :input").serializeArray(); // For converting it to array
//If needed below code is converting it to object
var obj = {};
for (var i = 0, l = data.length; i < l; i++) {
obj[data[i].name] = data[i].value;
}
console.log(data); // Print Array in Console
console.log(obj);// Print Object in Console
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbl2">
<tr>
<td>
<input type="text" name="tb3" value="1" />
</td>
</tr>
<tr>
<td>
<input type="text" name="tb4" value="2" />
</td>
</tr>
<tr>
<td>
<input type="text" name="tb5" value="3" />
</td>
</tr>
<tr>
<td>
<input type="text" name="tb6" value="4" />
</td>
</tr>
</table>
Add attribute name with name and array .
$.ajax({
data : data,
type : "get",
url : url,
dataType : "json",
error : function(resp){
alert("Error !!");
},
success : function(resp){
table = '';
$.each(resp,function(indx,obj){
table += '<tr>';
table += '<td>'+parseInt(indx+1)+'</td>';
table += '<td>'+'<input type="text" name="service_code[]" value="'+obj.ServiceDetail.service_code+'">'+'</td>';
table += '<td>'+'<input type="text" name="name[]" value="'+obj.ServiceDetail.name+'">'+'</td>';
table += '<td>'+'<input type="text" name="discount_price[]" value="'+obj.ServicePrice.discount_price+'">'+'</td>';
table += '</tr>';
});
$("tbody#sevice_table_body").append(table);
}
});
Here you go with a solution
var inputData = [];
$('button[value="submit"]').click(function(){
$('input[type="text"]).each(function(){
inputData.push($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Hope this will help you.
In your table section, add name attribute like this:
$.each(resp,function(indx,obj){
table += '<tr>';
table += '<td>'+parseInt(indx+1)+'</td>';
table += '<td>'+'<input name="services[' + indx + '][code]" type="text" value="'+obj.ServiceDetail.service_code+'">'+'</td>';
table += '<td>'+'<input name="services[' + indx + '][name]" type="text" value="'+obj.ServiceDetail.name+'">'+'</td>';
table += '<td>'+'<input name="services[' + indx + '][price]" type="text" name="service[' + indx + '][price]" value="'+obj.ServicePrice.discount_price+'">'+'</td>';
table += '</tr>';
});
This will produce names like services[0][code], services[0][name], etc.,
Now you can access the input values as an array in PHP:
$services = $_POST['services'];
foreach ($services as $index => $service) {
$code = $service['code'];
$name = $service['name'];
$price = $service['price'];
echo "$index : $code, $name, $price \n";
}
I've an ajax form and a table.
This is my ajax code :
$(function () {
$(".submitann").click(function () {
var title = $("#title").val();
var announcement = $("#announcement").val();
var dataString = $('#annform');
if ((title == '') || (announcement == '')) {
alert("Please Fill In The Fields");
} else {
$.ajax({
type: "POST",
dataType: "json",
data: dataString.serialize(),
url: "http://www.domain.com/formprocess.php",
success: function (data) {
//insert table code here
});
}
return false;
});
});
I tried but failed. In the php code, I've done this :
$data = '<tr class="odd gradeX">
<td>
<input type="checkbox" class="checkboxes" value="1" />
</td>
<td><a href="#" class="anntitle" data-type="text" data-pk="'.$id.'" data-original-title="Enter title"
data-name="title">'.$_POST["title"].'</a>
</td>
<td><a class="delete" href="javascript:;">Delete</a>
</td>
</tr>';
echo json_encode($data);
It works with many problems.
Firstly, it goes to the last row.
Second, the x-editables does not work.
How to add a row to the without refreshing the page?
first you need to give the id to your <table id="mytable">.than on success of ajax, add just like this:
var row_data = "";
row_data +="<tr class='odd gradeX'>
<td><input type='checkbox' class='checkboxes' value='1' /></td>
<td><a href='#' class='anntitle' data-type='text' data-pk=''.$id.'' data-original-title='Enter title' data-name='title' >'.$_POST['title'].'</a></td>
<td><a class='delete' href='javascript:;'>Delete</a></td>
</tr>";
$("#mytable").append(row_data);