how can i update multiple use php/ajax in datatable checkbox? - php

I have a datatable filled in by SALARIES with checkbox and button update_all and dropdownlist contains CHANTIER_IDs, I want to do multiple update of CHANTIER_IDs using php / ajax .CHANTIER_IDs of SALARIES cheked in datatable will be modified by value select in dropdownlist.
I want when I click on button update all the values of chantire_ids of cheked SALARIES will be modified by the value selected in dropdownlist
affectation.php
<div class="container" id="app">
<h3>affecter salarie a chantier</h3>
<div class="form-group col-md-3 ">
<select class="form-control" id="chantier">
<?php
include "../conn/connexion.php";
$query = "SELECT id,chantier FROM chantier";
$result = mysqli_query($con,$query);
$n = mysqli_num_rows($result);
for ($i=0; $i <$n ; $i++) {
$row = mysqli_fetch_array($result);
?>
<option value="<?php echo $row[0]; ?>" ><?php echo $row[1];
} ?></option>
</select>
</div>
<button class="btn btn-success update-all" data-url="">Update All</button>
<button style="margin: 5px;" class="btn btn-danger btn-xs delete-all" data-url="">Delete All</button>
<table class="table table-bordered">
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>S.No.</th>
<th>nom & prenom</th>
<th>cin</th>
<th>matricule</th>
<th>chantier_ids</th>
</tr>
<?php
$query1 = "SELECT id,nom,prenom,cin,matricule,chantier_id
FROM salaries";
$result1 = mysqli_query($con,$query1);
$n1 = mysqli_num_rows($result1);
for ($i=0; $i <$n1 ; $i++) {
$row1 = mysqli_fetch_array($result1);
?>
<tr id="tr_<?php $row[0] ?>">
<td><input type="checkbox" class="checkbox" data-id="<?php $row[0] ?>"></td>
<td><?php echo $row1[0]; ?></td>
<td><?php echo $row1[1]; ?> <?php echo $row1[2]; ?></td>
<td><?php echo $row1[3]; ?></td>
<td><?php echo $row1[4]; ?></td>
<td><?php echo $row1[5]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
javascript
<script type="text/javascript">
$(document).ready(function () {
$('#check_all').on('click', function(e) {
if($(this).is(':checked',true)) {
$(".checkbox").prop('checked', true);
} else {
$(".checkbox").prop('checked',false);
}
});
$('.checkbox').on('click',function(){
if($('.checkbox:checked').length == $('.checkbox').length){
$('#check_all').prop('checked',true);
}else{
$('#check_all').prop('checked',false);
}
});
$('.update-all').on('click', function(e) {
if(confirm("Are you sure you want to delete this?")){
var id = [];
}
$(':checkbox:checked').each(function(i){
id[i] = $(this).val();
});
if(id.length === 0) //tell you if the array is empty
{
alert("Please Select atleast one checkbox");
}else{
$.ajax({
url:'delete.php',
method:'POST',
data:{id:id},
success:function(){
for(var i=0; i<id.length; i++){
$(this).parents("tr")
.children("td:last-child")
.text($('#app')
.children("option:selected")
.text());
}
}
});
}
});
});
</script>
updateall.php
<?php
include "conn/connexion";
if(isset($_POST["id"])) {
foreach($_POST["id"] as $id) {
$query = "UPDATE salaries
SET chantier_id = ". $id ."
WHERE id = '".$id."'";
mysqli_query($con, $query);
}
}
?>

Related

delete multiple row in php table data using cheque box

I have this script where the delete function is not working. The row gets removed from the table as the same was called for.. but the same re-appears as the same is not been removed from the actual database..
My table's :
This is given for the delete button:
<th >Follow Up Date</th>
<th >Name SM</th>
<th >Entered Date</th>
<th >Edit</ht>
<th ><button type="button" name="delete_all" id="delete_all" class="btn btn-danger btn-xs">Delete</button></th>
The second where the there is check box:
<td><?php echo $row["NameSM"]; ?></td>
<td><?php echo $row["EnteredDate"]; ?></td>
<td><input type="button" name="edit" value="Edit" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs edit_data" /></td>
<td><input type="checkbox" class="delete_checkbox" value="'.$row["id"].'" /></td>
The Delete Script :
<style>
.removeRow
{
background-color: #FF0000;
color:#FFFFFF;
}
</style>
<script>
$(document).ready(function(){
$('.delete_checkbox').click(function(){
if($(this).is(':checked'))
{
$(this).closest('tr').addClass('removeRow');
}
else
{
$(this).closest('tr').removeClass('removeRow');
}
});
$('#delete_all').click(function(){
var checkbox = $('.delete_checkbox:checked');
if(checkbox.length > 0)
{
var checkbox_value = [];
$(checkbox).each(function(){
checkbox_value.push($(this).val());
});
$.ajax({
url:"delete.php",
method:"POST",
data:{checkbox_value:checkbox_value},
success:function()
{
$('.removeRow').fadeOut(1500);
}
});
}
else
{
alert("Select atleast one records");
}
});
});
</script>
The delete.php page :
<?php
//database_connection.php = $connect = new PDO("mysql:host=localhost;dbname=u771775069_data", "root", "");
include('database_connection.php');
if(isset($_POST["checkbox_value"]))
{
for($count = 0; $count < count($_POST["checkbox_value"]); $count++)
{
$query = "DELETE FROM data WHERE id = '".$_POST['checkbox_value'][$count]."'";
$statement = $connect->prepare($query);
$statement->execute();
}
}
?>

Insert data by AJAX checkbox value and radio button value in PHP

Everything is working, but I want to insert $result['id']; into the database with a radio button value.
JavaScript:
<script>
$(document).ready(function() {
$('input[type="radio"]').change(function() {
var stu_ans = $(this).val();
var stu_id = "<?php echo $_GET['sid'];?>";
var q_id = "<?php echo $result['id'];?>";
$.ajax({
url: "insert1.php",
method: "POST",
data: {
stu_ans: stu_ans,
stu_id: stu_id,
q_id: q_id
},
success: function(data) {
$('#result').html(data);
}
});
});
});
</script>
HTML:
<form role="form" id='formid' method="post" action="result.php?s_id=<?php echo $_GET['sid'];?>">
<?php
$number_question = 1;
$row = mysqli_query( $con, "select * from question where subject_id='$category' ORDER BY RAND()");
$rowcount = mysqli_num_rows( $row );
$remainder = $rowcount/$number_question;
$i = 0;
$j = 1; $k = 1;
?>
<h4>Total Question:-<?php echo $rowcount;?></h4>
<?php
while ( $result = mysqli_fetch_assoc($row) ) {
if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";
?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>" style="font-size:20px;font-weight:bold;color:green"> (<?php echo $k?>).<?php echo $result['que'];?></p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<style>
input[type="radio"] {
-ms-transform: scale(1.5); /* IE 9 */
-webkit-transform: scale(1.5); /* Chrome, Safari, Opera */
transform: scale(1.5);
}
</style>
<tr>
<td>(A)<?php echo trim($result['o1']);?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o1'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'></td>
</tr>
<tr>
<td>(B)<?php echo $result['o2'];?>
</td>
<td colspan="5"><input type="radio" value="<?php echo $result['o2'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/></td>
</tr>
<tr>
<td>(C)<?php echo $result['o3'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o3'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
<tr>
<td>(D)<?php echo $result['o4'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o4'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
</tbody>
</table>
</div>
<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
<br/>
</div>
<?php
$i++;
if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
echo "<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
} elseif ( $rowcount > $number_question ) {
if ( $j == 1 && $i == $number_question ) {
echo "<button id='".$j."' class='next btn btn-success' type='button'>Next</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $k == $rowcount ) {
echo " <button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $j > 1 && $i == $number_question ) {
echo "<button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='button' >Next</button>";
echo "</div>";
$i = 0;
$j++;
}
}
$k++;
}
?>
</form>
living aside SQLinjections and security stuff in your code...
Change
var stu_ans=$(this).val();
for:
var stu_and =$("input[name='<name_of_radio>']:checked").val();
this is the way to take values from selected radio buttons.
and please!!! take a look around security issues.
hope this code help you!

online quiz Question id not insert in to database

Hey Guys i am new here anyone help me ...my problem is that i am working on a online quiz exam with php and ajax ..problem is that i am inserting Question ID into database so anyone help me solve this problem ...i show you my codes..
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var stu_ans=$(this).val();
var stu_id ="<?php echo $_GET['sid'];?>";
var q_id = $('#qname').val();
$.ajax({
url:"insert1.php",
method:"POST",
data:{stu_ans:stu_ans, stu_id:stu_id,q_id:q_id},
success:function(data){
$('#result').html(data);
}
});
});
});
</script>
<script>
$('.cont').addClass('hide');
$('#question_splitter_1').removeClass('hide');
$(document).on('click','.next',function(){
last=parseInt($(this).attr('id')); console.log( last );
nex=last+1;
$('#question_splitter_'+last).addClass('hide');
$('#question_splitter_'+nex).removeClass('hide');
});
$(document).on('click','.previous',function(){
last=parseInt($(this).attr('id'));
pre=last-1;
$('#question_splitter_'+last).addClass('hide');
$('#question_splitter_'+pre).removeClass('hide');
});
</script>
<style>
input[type="radio"] {
-ms-transform: scale(1.5); /* IE 9 */
-webkit-transform: scale(1.5); /* Chrome, Safari, Opera */
transform: scale(1.5);
}
</style>
<div class="container-fluied">
<div class="col-xs-12">
<hr>
<form role="form" id='formid' method="post" action="result.php?s_id=<?php echo $_GET['sid'];?>">
<?php
$number_question = 1;
$row = mysqli_query( $con, "select * from question where subject_id='$category' ORDER BY RAND()");
$rowcount = mysqli_num_rows( $row );
$remainder = $rowcount/$number_question;
$i = 0;
$j = 1; $k = 1;
?>
<h4>Total Question:-<?php echo $rowcount;?></h4>
<?php while ( $result = mysqli_fetch_assoc($row) ) {
if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>" style="font-size:20px;font-weight:bold;color:green"> (<?php echo $k?>).<?php echo $result['que'];?></p>
<input type="hidden" value="<?php echo $result['que'];?>" name="qname<?php echo $j;?>" id="qname">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>(A)<?php echo trim($result['o1']);?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o1'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'></td>
</tr>
<tr>
<td>(B)<?php echo $result['o2'];?>
</td>
<td colspan="5"><input type="radio" value="<?php echo $result['o2'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/></td>
</tr>
<tr>
<td>(C)<?php echo $result['o3'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o3'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
<tr>
<td>(D)<?php echo $result['o4'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o4'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
</tbody>
</table>
</div>
<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
<br/>
</div>
<?php
$i++;
if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
echo "<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
} elseif ( $rowcount > $number_question ) {
if ( $j == 1 && $i == $number_question ) {
echo "<button id='".$j."' class='next btn btn-success' type='button'>Next</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $k == $rowcount ) {
echo " <button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $j > 1 && $i == $number_question ) {
echo "<button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='button' >Next</button>";
echo "</div>";
$i = 0;
$j++;
}
}
$k++;
} ?>
</form>
</div>
</div>
<?php
session_start();
include('db_connect.php');
include('db_connection.php');
$stu_id=$_SESSION['sid'];
echo $gender=$_POST['stu_ans'];
echo $que=$_POST['q_id'];
$q=mysqli_query($con,"SELECT * FROM question where id='$que'");
$ro=mysqli_fetch_array($q);
//$que=$ro['id'];
$date=date('d/m/y');
$right_ans=$ro['right_ans'];
////////////////////////Ajax Data Saved//////////////////////////////
$q1=mysqli_query($con,"SELECT * FROM student_ans where q_id='$que' AND stu_id='$stu_id'");
$ro1=mysqli_num_rows($q1);
if($ro1==''){
$sql = mysqli_query($con,"INSERT INTO student_ans(stu_ans,q_id,stu_id,date,right_ans) value('$gender','$que','$stu_id','$date','$right_ans')");
}else{
$sql1 = mysqli_query($con,"update student_ans set stu_ans='$gender' where q_id='$que' AND stu_id='$stu_id'");
}
?>
This code working like when i am click radio button so ajax fuction working and save the answer into database but i want that when inserting answer into database with answer insert Question ID

How to call alert after ajax success data request?

view:
<script>
$(document).ready(function(){
$(".user_id").click(function(){
jid = $(".jid").attr('id');
var uids = [];
$("input[name='user_id']").map(function() {
uids.push({id: this.id, value: this.checked ? 1 : 0});
});
$.ajax({
type:"POST",
data:{"jid":jid, "uid":uids},
url:"<?php echo base_url(); ?>shortlist",
success:function(data){
$(".short_emp").html(data);
}
});
});
$(document).on('click' ,".user_idd" , function(){
jidd = $(".jidd").attr('id');
var uidss = [];
$("input[name='user_idd']").map(function() {
uidss.push({id: this.id, value: this.checked ? 1 : 0});
});
$.ajax({
type:"POST",
data:{"jidd":jidd, "uidss":uidss},
url:"<?php echo base_url(); ?>interview",
success:function(data){
alert(data);
//$(".short_interview").html(data);
}
});
});
});
</script>
<div class="wizard_horizontal">
<h2>Received</h2>
<section>
<table class="table table-hover js-basic-example dataTable table-custom m-b-0">
<thead>
<tr>
<th>Name</th>
<th>Shortlist</th>
</tr>
</thead>
<tbody>
<?php
$this->db->select('*');
$this->db->from('upload_detail');
$this->db->where('share_with_emp','1');
$sqll = $this->db->get();
if($sqll->num_rows() > 0)
{
$resultt = $sqll->result_array();
foreach($resultt as $roww)
{
?>
<tr>
<td>
<h6><?php echo $roww['fname']; ?></h6>
</td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jid" class="jid form-control" id="<?php echo $roww['jid']; ?>"/>
<input type="checkbox" name="user_id" id="<?php echo $roww["uid"]; ?>" class="user_id" <?php if($roww['shortlist']=='1'){ echo 'checked'; }?>><span>Shortlist</span>
</label>
</div>
</td>
</tr>
<?php
}
}
else
{
echo "<p>No data Found</p>";
}
?>
</tbody>
</table>
</section>
<h2>Shortlisted</h2>
<section>
<table class="table table-hover js-basic-example dataTable table-custom m-b-0">
<thead>
<tr>
<th>Name</th>
<th>Shortlist</th>
</tr>
</thead>
<tbody class="short_emp">
<?php
$this->db->select('*');
$this->db->from('upload_detail');
$this->db->where('shortlist','1');
$sql_short = $this->db->get();
$result_short = $sql_short->result_array();
foreach($result_short as $fetch)
{
?>
<tr>
<td>
<h6><?php echo $fetch['fname']; ?></h6>
</td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jidd" class="jidd form-control" id="<?php echo $fetch['jid']; ?>"/>
<input type="checkbox" name="user_idd" id="<?php echo $fetch["uid"]; ?>" class="user_idd" <?php if($fetch['interview']=='1'){ echo 'checked'; }?>><span>Shortlist</span>
</label>
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</section>
</div>
controller:
<?php
public function shortlist()
{
$jid = $this->input->post('jid');
$uid = $this->input->post('uid');
foreach($uid as $user)
{
$data = array('shortlist'=> $user['value']);
$where = 'jid="'.$jid.'" and uid="'.$user['id'].'"';
$this->db->where($where);
$query = $this->db->update('upload_detail',$data);
}
if($query==true)
{
$this->db->select('*');
$this->db->from('upload_detail');
$where = "jid='".$jid."' and shortlist='1'";
$this->db->where($where);
$sql_short = $this->db->get();
if($sql_short->num_rows() > 0)
{
$result_short = $sql_short->result_array();
foreach($result_short as $fetch)
{
?>
<tr>
<td>
<h6><?php echo $fetch['fname']; ?></h6>
</td>
<td>
<div class="fancy-checkbox">
<label>
<input type="hidden" name="jidd" class="jidd form-control" id="<?php echo $fetch['jid']; ?>"/>
<input type="checkbox" name="user_idd" id="<?php echo $fetch["uid"]; ?>" class="user_idd" <?php if($fetch['interview']=='1'){ echo 'checked'; }?>><span>Shortlist</span>
</label>
</div>
</td>
</tr>
<?php
}
}
else
{
echo "<p>No data Found</p>";
}
}
else
{
echo "<p>Unable to Proceed</p>";
}
}
public function interview()
{
$jidd = $this->input->post('jidd');
$uidd= $this->input->post('uidss');
echo $jidd;
print_r($uidd);
}
?>
In my code, I have table first with heading Recieved where I got data. Now, what am I doing when I click on class="user_id" it run successfully and load content over $(.short_emp).html(data) which is the part of heading shortlist inside the <tbody class="short_emp"> but when I click on $(".user_idd").click(function(){ then no alert are showing I don't know why? So How can I resolve this issue? Please help me.
Thank You
try to add attribute data-id in an input field and call that in a function
<input type="checkbox" name="user_idd" id="<?php echo $fetch["uid"]; ?>" data-id="<?php echo $fetch["uid"]; ?>" class="user_idd" <?php if($fetch['interview']=='1'){ echo 'checked'; }?>><span>Shortlist</span>
$(document).on("click", ".user_idd", function(){
var attr = $(this).attr('data-id');
alert(attr);
});
hope it will work for you :)

Store Value from On/Off Button in MySQL Database Using PHP Ajax

I found a tutorial for this with single ID. enter link description here
i have modified as per my knowledge for multiple id, i cannot able to get the id using ajax. i am not good with ajax. I have attached both code. can anyone tell how i can fix it for multiple id
i also attached a screen shot screenshot
index.php
<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
include("connection1.php");
$result = mysql_query("SELECT * FROM mytable ORDER BY id");
?>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<div>
<table id="datatables" class="display">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Text</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["text"]; ?></td>
<td><script type="text/javascript">
$(document).ready(function(){
$('#myonoffswitch<?php echo $row["id"]; ?>').click(function(){
var myonoffswitch<?php echo $row["id"]; ?>=$('#myonoffswitch<?php echo $row["id"]; ?>').val();
if ($("#myonoffswitch<?php echo $row["id"]; ?>:checked").length == 0)
{
var a<?php echo $row["id"]; ?>=myonoffswitch<?php echo $row["id"]; ?>;
}
else
{
var a<?php echo $row["id"]; ?>="off";
}
$.ajax({
type: "POST",
url: "ajax.php",
data: "value="+a<?php echo $row["id"]; ?>,
success: function(html){
$("#display").html(html).show();
}
});
});
});
</script><div class="onoffswitch">
<input type="hidden" value="<?php echo $row["id"]; ?>" name="ids" />
<input type="checkbox" name="onoffswitch<?php echo $row["id"]; ?>" class="onoffswitch-checkbox" id="myonoffswitch<?php echo $row["id"]; ?>"
<?php
$query3=mysql_query("select * from mytable where id=$row[id]");
$query4=mysql_fetch_array($query3);
if($query4['text']=="off")
{
echo "checked";
}
?>>
<label class="onoffswitch-label" for="myonoffswitch<?php echo $row["id"]; ?>">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<div id="display"></div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Ajax.php
<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
if(isset($_POST['value']))
{
$value=$_POST['value'];
$id=$_POST['ids'];
mysql_query("update mytable set text='$value' where id=2");
echo "<h2>You have Chosen the button status as:" .$value."</h2>";
}
?>
For your own sake ;) :
pls organize your code
add an output if something cant be loaded from your db, look therefor in the php example
why the 2. Query, you already loaded all data from 'mytable' with "select *"?
nevertheless here an example based on your code (not tested):
index.php
<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
include("connection1.php");
$result = mysql_query("SELECT * FROM mytable ORDER BY id");
?>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<div>
<table id="datatables" class="display">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Text</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["text"]; ?></td>
<td>
<div class="onoffswitch">
<input type="hidden" value="<?php echo $row["id"]; ?>" />
<input type="checkbox" class="onoffswitch-checkbox"
<?php
if($row['text']=="off")
{
echo "checked";
}
?>>
<label class="onoffswitch-label" for="myonoffswitch<?php echo $row["id"]; ?>">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<div id="display">
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function(){
$('.onoffswitch').click(function(){
var hiddenValueID = $(this).children(':hidden').val();
if ($(this).children(':checked').length == 0)
{
var valueData = 'on';
}
else
{
var valueData = 'off';
}
$.ajax({
type: "POST",
url: "ajax.php",
data: {value: valueData, id: hiddenValueID} ,
done: function(html){
$("#display").html(html).show();
}
});
});
});
</script>
</div>
ajax.php
<?php
$query=mysql_connect("localhost","pma","pmapass");
mysql_select_db("testdb",$query);
if(isset($_POST['value']))
{
$value=$_POST['value'];
$id=$_POST['id'];
mysql_query("update mytable set text='$value' where id=$id");
echo "<h2>You have Chosen the button status as:" .$value."</h2>";
}
?>
You can look in your browsers-console if you get an answer from your ajax-request.

Categories