i have been looking around to try and work out how to pass 2 variables arcoss the page thru ajax i have at this moment a page where a check box dynamailcy saves its value to the database on the click of it but i need it to be able to only edit rows with a certian id at thi moment i have gotten this far but i am stuck. any help would be greatly appricated
$(function()
{
$("input[type='checkbox']").on('click', function(){
var chkName = $(this).attr('checked');
if($(this).is(":checked")) {
var checkVal = $(':checkbox[checked='+chkName+']').attr('value');
}
else{
var checkVal =$("#frm input:checkbox").attr("id");
}
var userid = $(':checkbox[checked='+chkName+']').attr('name');
$.ajax({
type: "GET",
url: 'request.php?uname=' + checkVal '&id'= + userid ,// this is where i cant send the &id varible across but i can send the checkVal
success: function(data) {
if(data == 1){//Succues
alert('Data was saved in db!');
}
if(data == 0){//Faliure
alert('Data was NOT saved in db!');
}
}
});
});
});
</script>
</head><body>
<div class="content">
<table border="0.02px" class="mytable">
<thead>
<th><strong>Paid</strong></th>
<th><strong>Repaired</strong></th>
<th><strong>Returned</strong></th>
</thead>
<tr>
<form id="frm" name="frm">
<td><input type="checkbox" value="Paid" id="Waiting on Payment" name="29" /></td>
<td><input type="checkbox" value="Repaired" id="Waiting on Repairs" name="uname" /></td>
<td><input type="checkbox" value="With Student" id="Awaiting Pickup" name="uname" /></td>
</form>
</tr>
</table>
</div>
Below line in your code is incorrect:
url: 'request.php?uname=' + checkVal '&id'= + userid ,
Should be:
url: 'request.php?uname=' + checkVal +'&id=' + userid,
Also consider using var isChecked = $(this).prop('checked');
to get the checked status of checkbox.
In line var chkName = $(this).attr('checked');
, value of chkName would be 'undefined', while the checkbox is unchecked and so as the linevar userid = $(':checkbox[checked='+chkName+']').attr('name');
.
If I understand your code correctly, then you may try this:
$("input[type='checkbox']").on('click', function() {
var $this = $(this);
var isChecked = $this.prop('checked');
var checkVal = isChecked ? $this.attr('value') : $this.attr("id");
var userid = $this.attr('name');
$.ajax({
type: "GET",
//url: 'request.php?uname=' + checkVal '&id' = +userid,
url: 'request.php?uname=' + checkVal +'&id=' + userid,
// this is where i cant send the &id varible across but i can send the checkVal
success: function(data) {
if (data == 1) { //Succues
alert('Data was saved in db!');
}
if (data == 0) { //Faliure
alert('Data was NOT saved in db!');
}
}
});
});
I think you are looking for .serialize from jquery. You can check the api here. It has the example as well. Please go through the example how to pass serialized data.
Later in server end, you can have $_POST['<your input name here>']
to get the value. In addition, I see you try to get back the value from server. You can always echo json_encode(<your data object here>)
to send to server end.
Currently there is an error with the url variable you are trying to send
Current:
url: 'request.php?uname=' + checkVal '&id'= + userid,
Corrected:
url: 'request.php?uname=' + checkVal + '&id=' + userid,