Passing variables using jquery and ajax php - php

i'm following this tutorial, so my issue is:
I have this code here, which are some tables.
<tr id="<?php echo $id; ?>" class="edit_tr">
<td class="edit_td">
<span id="first_<?php echo $id; ?>" class="text"><?php echo $kodi; ?></span>
<input type="text" value="<?php echo $kodi; ?>" class="editbox" id="first_input_<?php echo $id; ?>" />
</td>
<td class="edit_td">
<span id="last_<?php echo $id; ?>" class="text"><?php echo $vlera; ?></span>
<input type="text" value="<?php echo $vlera; ?>" class="editbox" id="last_input_<?php echo $id; ?>"/>
</td>
</tr>
Now, this is the script:
<script type="text/javascript">
$(document).ready(function()
{
$(".edit_tr").click(function()
{
var ID=$(this).attr('id');
$("#first_"+ID).hide();
$("#last_"+ID).hide();
$("#first_input_"+ID).show();
$("#last_input_"+ID).show();
}).change(function()
{
var ID=$(this).attr('id');
var kodi=$("#first_input_"+ID).val();
var vlera=$("#last_input_"+ID).val();
var dataString = 'id='+ ID +'&kodi='+kodi+'&vlera='+vlera;
$("#first_"+ID).html('<img src="load.gif" />'); // Loading image
if(first.length>0&& last.length>0)
{
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$("#first_"+ID).html(first);
$("#last_"+ID).html(last);
I'm not passing variables right maybe..because it won't save them, the query won't excecute.
Thanks

The easiest way in jQuery to pass your form fields in ajax is by using serialize:
data: $("form").serialize();
An additional problem is that you are using two variables, first and last but you are not defining them anywhere.

Try this code:
$(function() { // This makes the same than $(document).ready();
$(".edit_tr").each(function() { // Instead of assign the click event, we loop through each element
var $element = $(this);
var id = this.id;
var $first = $('#first_' + id);
var $last = $('#last_' + id);
var $firstInput = $('#first_input_' + id);
var $lastInput = $('#last_input_' + id);
$element.click(function() {
$first.hide();$last.hide();
$firstInput.show();$lastInput.show();
}).change(function() {
$first.html('<img src="load.gif" />');
// Here you have an if, but those elements are not defined in the script...
// if (first.length > 0 && last.length > 0) {
$.post("table_edit_ajax.php", {
id : id,
kodi : $firstInput.val(),
vlera : $lastInput.val()
}, function(html) {
// Here again the two vars not defined
//$first.html(first);
//$last.html(last);
});
// }
});
});
});

Related

How make an array only selected input text values and send array via ajax?

i want to add values in array and want to send on click
i want to store value in array only those user input text i have click not all values store in array.
when i store all value in array its tacking time to update
so i want to update only those value on which user have click or change in usert input text
This is my html code. When I click in one input text then it shows only that value and then I click on another input text then it store first input text value and second input text value.
Meaning, I want an array which can store input text value in an array() whenever I try to store value in array it shows undefined of array
Now I want this array to send via ajax. How can I do this so that all array data goes via ajax
I know array is a collection of similar datatype. So I want to store id in which all ids, reps_value of array store all reps_value and reps_percent of array store all reps_percent value
And I want to submit my form using submit button on click
this is not working on click using seralizeArray() its giving me all input values when i click only two user input text
$(document).on('Click', '.submit_prescription,input', function (e) {
var x = $(this).parent().find("input").serializeArray();
var val[] = $(this).val();
var g[] = $(this).attr("name");
var id[] = $(this).attr("id");
//i am trying to store data in an array i using each loop but its not working properly how to set this loop to sore in array
/* alert(g);
alert(val);
alert(id);
*/
// $.each(x, function(key, values) {
// console.log(key, values.value);
// alert(key, values.value);
// });
// }
// var val = $("input:id").val();
// var val = $("input").val();
var g = $(this).attr("name");
//alert(g);
if (g == 'reps_value') {
// alert('reps_valwwue');
var f = $(this).attr("id");
var d = $(this).val();
var g = $(this).attr("name");
var data2 = "id=" + f + "&reps_value=" + d + "&type=" + g;
$.ajax({
type: "POST",
url: "prescriptionUpdate1",
data: data2,
cache: false,
success: function (data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function (err) {
// //alert(err);
}
});
} else {
// alert('reps_percentage');
var f = $(this).attr("id");
var d = $(this).val();
//var data : { reps_percent : d, id : f },
var data2 = "id=" + f + "&reps_percent=" + d + "&type=" + g;
$.ajax({
type: "POST",
url: "prescriptionUpdate1",
data: data2,
cache: false,
success: function (data) {
$("#message").html(data);
$("p").addClass("alert alert-success");
},
error: function (err) {
//alert(err);
}
});
}
});
============================================================
public function prescriptionUpdateData1($data) {
#$id = $data['id'];
#$reps_percent = $data['reps_percent'];
#$reps_value = $data['reps_value'];
$type = $data['type'];
if ($data['type'] == 'reps_value') {
$result = $this - > db - > query("UPDATE dev_prescription SET `reps_value`= '".$reps_value.
"'
WHERE id = '".$id."'
");
}
else {
$result = $this - > db - > query("UPDATE dev_prescription SET `reps_percent`= '".$reps_percent.
"'
WHERE id = '".$id."'
");
}
if ($result) {
return TRUE;
} else {
return $this - > db - > _error_message();
}
}
<td align="center" valign="middle" bgcolor="#F2F2F2" sdval="20" sdnum="1033;"> <font face="Agency-Roman" color="#000000">
<input style="width:90%" type="text" name="reps_value" id="<?php echo $location[2] ?>" value="<?php echo $location[0] ?>" class="" /> </font></td>
<td align="center" valign="middle" bgcolor="#F2F2F2" sdnum="1033;0;0.0%">
<font face="Agency-Roman" size="1" color="#FF0000"> <input style="width:38px" type="text" name="reps_percent" id="<?php echo $location[2] ?>" value="<?php echo $location[1] ?> "class="" /><input style="width:38px" type="hidden" name="id" id="id" value="<?php echo $location[2] ?>" class="" /> </font>
</td> <td align="left" valign="bottom" bgcolor="#FFFFFF"><font color="#000000"><br></font></td>
<td align="center" valign="middle" bgcolor="#F2F2F2" sdval="20" sdnum="1033;">
<font face="Agency-Roman" color="#000000">
<input style="width:90%" type="text" name="reps_value" id="<?php echo $location[5] ?>" value="<?php echo $location[3] ?>"
class="" />
</font>
</td>
<td align="center" valign="middle" bgcolor="#F2F2F2" sdnum="1033;0;0.0%">
<font face="Agency-Roman" size="1" color="#FF0000">
<input style="width:38px" type="text" name="reps_percent" id="<?php echo $location[5] ?>" value="<?php echo $location[4] ?>"
class="" />
<input style="width:38px" type="hidden" name="id" id="id" value="<?php echo $location[5] ?>" class="" />
</font>
</td>
<td align="left" valign="bottom" bgcolor="#FFFFFF">
<font color="#000000"><br></font>
</td>

Checkboxes inside a table with php foreach

I have a pretty much simple issue with a table i create with PHP foreach (in codeigniter).
One column inside the table has 2 checkboxes. One true and one false so when i create the table has some names and infos and then 2 checkboxes. Whenever the user clicks the true I create an AJAX POST call to my database to change the state of a column. But when the user has clicked true and then he clicks false I want the true checkbox to be unchecked.
Here is the code
<table class="responstable">
<tr>
<th>ID</th>
<th><span>Name</span></th>
<th>number1</th>
<th>number2</th>
</tr>
<?php
foreach($students as $column => $data ) { ?>
<tr>
<td><?php echo $data[0]->Userproperties_UserAM; ?></td>
<td><?php echo $data[0]->Userproperties_UserFullName; ?></td>
<td><?php echo $data[0]->UserAbsence; ?></td>
<td> <input class="true" id="<?php echo $data[0]->Userproperties_UserId; ?>" type='checkbox' value="<?php echo $data[0]->Userproperties_UserAM; ?>" />
<label for='true'>
<span></span>
True
</label>
<input class="false" id="<?php echo $data[0]->Userproperties_UserId; ?>" type='checkbox' value="<?php echo $data[0]->Userproperties_UserAM; ?>" />
<label for='false'>
<span></span>
False
</label></td>
</tr>
<?php } ?>
</table>
And the ajax code
$(document).ready(function() {
$('.true').change(function() {
if($(this).is(":checked")) {
$('.false').attr('checked', false);
var moduleid = <?php echo $moduleid; ?>;
var teacherid = <?php echo $teacherid; ?>;
var studentid = $(this).attr('id');
var weeknum = $("#dropi").val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/TeacherParousies/send_parousia",
data: {
moduleid: moduleid,
studentid: studentid,
teacherid: teacherid,
parousia: 1,
weeknum: weeknum
},
success: function(res) {
},
error: function(err) {
}
});
}
});
$('.false').change(function() {
if($(this).is(":checked")) {
$('.true').attr('checked', false);
var moduleid = <?php echo $moduleid; ?>;
var teacherid = <?php echo $teacherid; ?>;
var studentid = $(this).attr('id');
var weeknum = $("#dropi").val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/TeacherParousies/send_parousia",
data: {
moduleid: moduleid,
studentid: studentid,
teacherid: teacherid,
parousia: 0,
weeknum: weeknum
},
success: function(res) {
},
error: function(err) {
}
});
}
});
});
The problem here is that when i check the 2nd row of the table's checkbox lets say false, there is a change on the 1st row for true if it is checked.
Although I have stated
if($(this).is(":checked"))
Any idea?
In your case it .false and .true will uncheck all. You just need to check the nearest element.
Kindly replace the code $('.true').attr('checked', false); with this code $(this).closest( "td" ).find('.true').attr('checked', false);
and do same with for $('.false').attr('checked', false); this $(this).closest( "td" ).find('.false').attr('checked', false);

Get key from input id jQuery

I have list with several inputs
<input type="hidden" id="elevens_id_ea" value="<?php echo $_GET['elev_id']; ?>" />
<input type="hidden" id="arskursen_ea" value="<?php echo $arskursen; ?>" />
<?php
if ($extraanpassning_hamta['atgard'] == true){
?>
<input name="extraanpassning" id="knapp[<?php echo $amnets_id['amne_id']; ?>]" type="button" class="btn-u rounded btn-u-red btn-sm" value="Ja">
<?php } else { ?>
<input name="extraanpassning" id="knapp[<?php echo $amnets_id['amne_id']; ?>]" type="button" class="btn-u rounded btn-u-green btn-sm" value="Nej">
<?php } ?>
The main problem is how to "catch" the value in the two latest inputs with:
id="knapp[<?php echo $amnets_id['amne_id']; ?>]"
If knapp[4] -> how do I get the 4?
The code above is a part of a button that changes value when the user presses it (without refreshing the page).
The JS
<script>
$(document).ready(function(){
$('input[type="button"]').click(function(){
var extraanpassningVal = $(this).attr("value");
var amne_id_ea = $(this).attr("id");
var elevens_id_ea = $("#elevens_id_ea").val(); //värdet av elev_id
var arskursen_ea = $("#arskursen_ea").val(); //värdet av elev_id
$.ajax({
type: "POST",
url: "iup_extraanpassning_byta.php",
data: {extraanpassningType: extraanpassningVal, amne_id_ea: amne_id_ea, elevens_id_ea: elevens_id_ea, arskursen_ea: arskursen_ea},
success: function() {
location.reload();
}
})
});
});
</script>
EDIT:
The main problem is how to get the key from a input with an id like knapp[4].
How do get the key within knapp[]?
Update (thanks to user:SpYk3HH)
<script>
$(document).ready(function(){
$('input[type="button"]').click(function(){
var key = this.id.replace(/knapp|\[|\]/g, ''), // <---They key
extraanpassningVal = $(this).attr("value"),
amne_id_ea = $(this).attr("id"),
elevens_id_ea = $("#elevens_id_ea").val(),
arskursen_ea = $("#arskursen_ea").val();
if (this.ajax) this.ajax.abort(); // helps prevent multiple ajaxing (multiclicking)
this.ajax = $.ajax({
type: "POST",
url: "iup_extraanpassning_byta.php",
data: {extraanpassningType: extraanpassningVal, amne_id_ea: amne_id_ea, elevens_id_ea: elevens_id_ea, arskursen_ea: arskursen_ea},
success: function() {
location.reload();
}
})
})
});
</script>
I think I get it? You want the key when calling the button in JS? So like say: key = this.id.replace(/knapp|\[|\]/g, '')
Update, I didn't see the brackets before
$('input[type="button"]').click(function(){
var key = this.id.replace(/knapp|\[|\]/g, ''), // <---They key
extraanpassningVal = $(this).attr("value"),
amne_id_ea = $(this).attr("id"),
elevens_id_ea = $("#elevens_id_ea").val(),
arskursen_ea = $("#arskursen_ea").val();
if (this.ajx) this.ajx.abort(); // helps prevent multiple ajaxing (multiclicking)
this.ajx = $.ajax({/* options */});
})
Does that help?
FYI, $(this).attr("id") and this.id are the same thing
$('[name=test]').each(function(i) { $('#bob').text(this.id.replace(/knapp|\[|\]/g, '')) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input id="knapp4" name="test" value='my id is "knapp[4]"' />
<hr />
key: <span id="bob"></span>
Try this.
var amne_id_ea = "knapp[4]",
value = amne_id_ea.substring(amne_id_ea.lastIndexOf("[")+1,amne_id_ea.lastIndexOf("]"));
alert(value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Hope you are expecting this.

PHP with jquery datatables with CRUD just can edit visible record

I'm using jquery datatables but there's something problem.
I have total 11 records. Data tables show me 10 entries of record from database and I add column for edit and delete
But when I click pagination button , data tables show me 1 record (record number 11), but the button of edit and delete is not work
Can you help me ?
This my code
<?php
while($row_dosen = mysql_fetch_assoc($q_dosen)){
?>
<tr>
<td><?php echo $row_dosen['nik'];?></td>
<td><?php echo $row_dosen['nama']; ?></td>
<td align="center"><span class="label label-success2" style="font-size:14px;">
<?php echo $row_dosen['jurusan_nama']; ?><span><td>
<td align="center">
<?php echo $row_dosen['status_name']; ?>
</td>
<td align="center">
<?php echo $row_dosen['email']; ?>
</td>
<td align="center">
<?php echo $row_dosen['no_hp']; ?>
</td>
<td align="left">
<?php
//$pizza = mysql_query("SELECT dosen.skill_id, dosen.skill_name, skill.skill_id, skill.skill_name FROM dosen, skill WHERE dosen.skill_id = skill.skill_id ",$conn);
$pieces = explode(",", $row_dosen['skill_id']);
$j = 1;
for($i = 0; $i < count($pieces) ; $i++){
//echo ''.$pieces[$i].'<br/>';
$pizza = mysql_query("SELECT skill_id, skill_name FROM skill WHERE skill_id = '".$pieces[$i]."'",$conn);
while($p = mysql_fetch_assoc($pizza)){
echo '<b>'.$j.'</b>. '.$p['skill_name'].'<br/>';
}
$j++;
}
?>
</td>
<td align="center">
<input type="hidden" id="<?php echo $row_dosen['nik']; ?>" name="id_dosen" value="<?php echo $row_dosen['nik']; ?>"/>
<button id="<?php echo $row_dosen['nik']; ?>" class="btn btn-warning edtlec" type="button" data-toggle="modal" data-target="#editDosen"><i class="fa fa-gear"></i> <small>Edit</small></button>
</td>
<td align="center">
<button class="btn btn-danger delbutton" type="button" id = "<?php echo $row_dosen['nik']; ?>"><i class="fa fa-times"></i> <small>Delete</small></button>
</td>
</tr>
<?php
}
?>
MY javascript
$(document).ready(function(){
$("#lec_reg").click(function(){
var nik = $("#nik").val();
var name = $("#name").val();
var jurusan = $("#jurusan").val();
var status = $("#status").val();
var email = $("#email").val();
var phone = $("#phone").val()
var skill = $("#skill").val();
$.ajax({
type: "POST",
url:"inc_/regist_dosen.php",
data:"nik=" + nik +
"&name=" + name +
"&jurusan=" + jurusan +
"&status=" + status +
"&email=" + email +
"&phone=" + phone +
"&skill=" + skill,
success: function(data){
$("#loading").html('<center>Validating data... <img src="../../img/ajax-loader.gif"/></center>').show();
setTimeout(function() {
$("#loading").fadeIn();
setTimeout(function() {
$("#loading").slideUp('slow');
},2000);
$("#loading").html(data);
},1000);
}
});
});
$(function() {
$(".delbutton").click(function(){
//Save the link in a variable called element
var element = $(this);
//Find the id of the link that was clicked
var del_id = element.attr("id");
//Built a url to send
var info = 'id=' + del_id;
if(confirm("Are you sure you want to delete this Lecturer ? NIK : "+del_id)){
$.ajax({
type: "POST",
url: "inc_/delete_dosen",
data: info,
success: function(data){
$(".loading2").html('<center>Loading data... <img src="../../img/ajax-loader.gif"/></center>').show();
setTimeout(function() {
$(".loading2").fadeIn();
setTimeout(function() {
$(".loading2").slideUp('slow');
},4000);
$(".loading2").html(data);
},1000);
}
});
}
return false;
});
});
$(function() {
$(".edtlec").click(function(){
//Save the link in a variable called element
var element = $(this);
//Find the id of the link that was clicked
var edit_id = element.attr("id");
//Built a url to send
var info = 'id_nik=' + edit_id;
$.ajax({
type: "POST",
url: "inc_/edit_dosen",
data: info,
success: function(data){
$('#editDosen').modal('show');
$("#response").html('<center>Loading data... <img src="../../img/ajax-loader.gif"/></center>').show();
setTimeout(function() {
$("#response").html(data);
},1000);
}
});
return false;
});
});
Finally my script now work on page 2 ++
The answer is
change to
$( document ).on( "click", ".delbutton", function() {
Because DataTables removes nodes from the DOM, event's might not be applied
credit
http://datatables.net/faqs#events
Thanks for all , especially Allan Jardin From datatables forum :)

Php Js Ajax I need some assistance

I am trying to create a social dating site and I have my problem in the Add as Buddy Button. When you click it, my site should be sending the uid of the sender (from_uid) and the uid of the receiver (to_uid) to the database. It sends out the from_uid successfully but the to_uid always sends 0.
PHP
<div class="member" data-user="<?php echo $member['xmpp_user']; ?>" data-uid="<?php echo $member['uid']; ?>">
<input type="hidden" id="hiddenuid" value="<?php echo $member['uid']; ?>">
<img src="https://s3.amazonaws.com/wheewhew/user/<?php echo $member['uid']; ?>/photos/<?php echo $member['profile_pic']; ?>" />
<div class="member_name"><?php echo $member['firstname']." ".$member['lastname']; ?></div>
<div id="addbutton"><button type="submit" class="add"> Add as Buddy </button></div>
</div>
Javascript
<script type="text/javascript">
var BOSH_SERVICE = 'http://wheewhew.com:5280/http-bind';
var connection = null;
var xmpp_user = "<?php echo $xmpp_user; ?>#wheewhew.com/default";
var xmpp_pass = "<?php echo $xmpp_password; ?>";
var uid = "<?php echo $uid; ?>";
$(document).ready(function () {
$('#btn-logout').click(logout);
$('.add').click(addBuddy);
connectXMPP();
//updateLastSeen();
});
</script>
jabber.js
function addBuddy(){
var xmpp_user = $(this).parent().attr('data-user')+'#wheewhew.com/default';
var to_uid = $(this).parent().attr('data-uid');
$.ajax({
type: "POST",
url: "./ajax/addBuddy",
data: "from_uid="+uid+"&to_uid="+to_uid,
success: function(data) {
var ret = eval('('+data+')');
if(ret.status == 'success'){
connection.send($pres({to:xmpp_user,type:'subscribe'}).tree());
}
}
});
}
Your addBuddy function doesnt have a reference for what $(this) is. Try this out:
$('.add').click(function(){
addBuddy($(this));
});
And then in your function:
function addBuddy($btn){
var xmpp_user = $btn.parent().attr('data-user')+'#wheewhew.com/default';
var to_uid = $btn.parent().attr('data-uid');
// ajaxy stuff
}
That should help. At the very least, we can continue the discussion here rather than in the comments for the OP.

Categories