0

I am trying do is save/delete data in (mysql) database on checkbox check/uncheck respectively. i have dynamically generated checkboxes from that i want to save data of particular checkbox in database. simultaneously if uncheck that particular checkbox data will erase from database. can anybody tell me is this possible using jquery or any other scripting language in php. thanks in advance.

i am adding some code what i tried this my "table_edit_ajax.php" code

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");


if($_POST[first_input_.$i])
{
$firstname=mysql_escape_String($_POST[first_input_.$i]);
$sql = "INSERT INTO fullnames(firstname)VALUES('$firstname')";
mysql_query($sql);
}
?>

this is my main file in which i am generating checkboxes called "test.php"

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$('.edit_tr').on('change', function(){
    // saving or deleting?
    var savedelete;
    if ($(this).is(':checked')) savedelete='save';
    alert ("save test");
    else savedelete='delete';
    alert ("delete test");
    // what am I changing?
    var name=$(this).attr('first_input_<?php echo $i; ?>');

    // call the server
    $.ajax({
      type: "POST",
      url: 'table_edit_ajax.php'
      data: {"do": "saveordelete", "whichone": savedelete,  "name": name   },
      success: function(data){
        if (data=='true'){ // is string not boolean
            // it worked
            alert ("true");
        }else{
            // didn't work
            alert ("false");
        }
      }
    });

});
</script>
<style>
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}

td
{
padding:7px;
}
.editbox
{
font-size:14px;
width:270px;
/*background-color:#ffffcc;*/

border:solid 1px #000;
padding:4px;
}

th
{
font-weight:bold;
text-align:left;
padding:4px;
}
.head
{
background-color:#333;
color:#FFFFFF

}

</style>
</head>

<body bgcolor="#dedede">
<div style="margin:0 auto; width:750px; padding:10px; background-color:#fff; height:800px;">


<table width="100%">
<tr class="head">
<th>First Name</th>
</tr>
<?php
$i=1;
while($i<=5)
{
if($i%2)
{
?>
<tr id="lovetr<?php echo $i; ?>" class="edit_tr">
<?php } else { ?>
<tr id="trlove<?php echo $i; ?>" bgcolor="#f2f2f2" class="edit_tr">
<?php } ?>
<td width="50%" class="edit_td">
<input type="checkbox" value="firstname<?php echo $i; ?>" class="editbox" name="first_input_<?php echo $i; ?>" />
</td>
</tr>

<?php
$i++;
}
?>

</table>
</div>
</body>
</html>
4
  • Yeah it's possible, but you'll have to ask a much more specific question Commented Mar 15, 2013 at 0:32
  • @ExplosionPills can you tell me any example related with this... Commented Mar 15, 2013 at 1:03
  • @ExplosionPills please help to fixed my problem, i edited my question and insert my code too can please help to sort out this prob. Commented Mar 16, 2013 at 1:02
  • ($_POST[first_input_.$i]) undefined $i. if ($(this).is(':checked')) savedelete='save'; alert ("save test"); else savedelete='delete'; alert ("delete test"); Syntax error else with no if because you've put an alert where there should be an else. if (data=='true') you're not sending anything on success or fail. $(this).attr('first_input_<?php echo $i; ?>'); again there is no $i Commented Mar 16, 2013 at 3:57

2 Answers 2

2

JQuery will talk to the server using $.ajax, preferably POST.

The server will try to delete the database row using PHP and MySQLi.
If successful or unsuccessful PHP echo and die a useful response for jQuery to interpret. This could simply be true and false

The ajax waits for that response and when it arrives would leave the checkbox as is, or switch it to what it was if the deletion failed. You might like to confirm the action was successful.

SO isn't here for people to write your code, you need to dig into this. For a seasoned developer it's easy but if you don't try you won't learn. Feel free to ask further questions if you hit problems with programming.

Example jQuery Not checked

<label><input type="checkbox" value="1" name="apples" class="savedelete" />Save/Delete</label>
<label><input type="checkbox" value="1" name="pears" class="savedelete" />Save/Delete</label>

jQuery

$('.savedelete').on('change', function(){
    // saving or deleting?
    var savedelete;
    if ($(this).is(':checked')) savedelete='save';
    else savedelete='delete';
    // what am I changing?
    var name=$(this).attr('name');

    // call the server
    $.ajax({
      type: "POST",
      url: 'index.php'
      data: {"do": "saveordelete", 
        "whichone": savedelete,
        "name": name
      },
      success: function(data){
        if (data=='true'){ // is string not boolean
            // it worked
        }else{
            // didn't work
        }
      }
    });

});
Sign up to request clarification or add additional context in comments.

3 Comments

can you please tell me any example because i am new for ajax.
have added some example code. you must read this api.jquery.com/jQuery.post
i tried a lot i don't where i am going wrong can you please tell me correction in my code. if you are not busy.
0
 <?php
 include('../dbcon.php');
 $id = $_POST['id'];
 mysql_query("delete from files where file_id = '$id' ")or die(mysql_error());
 ?>

you cak make id selector using checbox and add to $_POST['selector'];

   <?php
        include('../dbcon.php');
        if (isset($_POST['backup_delete'])){
        $id=$_POST['selector'];
        $N = count($id);
        for($i=0; $i < $N; $i++)
        {
            $result = mysql_query("DELETE FROM teacher_backpack where file_id='$id[$i]'");
        }
        header("location: backack.php");
        }
        ?>

1 Comment

Please give some explanation for your example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.