1

I have php code which displays a table, the contents of which are displayed from a table in the database. One of the fields is a status field. There is a "Change" button which should change an "Inactive" status to "Active" and vice versa.

I need to know how to go about writing the code for the "change" button. Whether it should be written in jquery or it can be written in php also.

The code is as follows:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="indexStyle.css">
<title>User List</title>
</head>
<body>
<table border="2" bgcolor="#50D5CE">
<tr>
    <td>SL. No</td>
    <td>Name</td>
    <td>Mobile Number</td>
    <td>Status</td>    
 </tr>
<?php include ('header.php');
include ('includes/connect.php');
$dbconn =  new connect();
$fields = "first_name,last_name,mobile_number,status";
$user_details = $dbconn->select($fields,"user_details","1");
$c=1;
for($i = 0; $i < count($user_details);$i++){
?>
<tr>
<td><?php echo $c ?></td>
<td><?php echo $user_details[$i][0]." ".$user_details[$i][1]; ?></td>
<td><?php echo $user_details[$i][2] ?></td>
<td><?php if($user_details[$i][3] == 0) {
echo "Inactive"; } 
else echo "Active"; ?>
<input type="button" name="change_status" id ="change_status" value="Change"/></td>
</tr>
<?php $c++;
} ?>
</table>

</body>
</html>
1
  • The "change" button has to trigger a AJAX call (or the sorts) to the PHP code saying something like "update ID" (where ID is number) and the PHP code has to connect to the db and update the value of ID to whatever you want. Commented Aug 4, 2014 at 11:44

3 Answers 3

2

jQuery is Javascript and runs on the user's computer, while PHP runs on the server. The thing is that the user has to trigger the database update and the server has to execute it. As a result, you need to send an AJAX request using jQuery and your server-side has to execute the update.

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

Comments

1
  change input: ↓
 <input type="button" name="change_status" id ="change_status" value="Change" onclick="update_status(<?php echo $user_details[$i][2]; ?>,<?php echo $user_details[$i][3]; ?>)"/>

  <script>
           function update_status(mob_no,sta)
           {

                  $.ajax({
                        type: "POST",
                        url: "update_status.php",
                        data: {mobno:mob_no,status:sta},
                        success: function(data){
                            // whatever you do here after success
                        }
           }
  </script>

UPDATE_STATUS.PHP

     <?php
           if(isset($_POST['mobno']) && isset($_POST['status']))
           {
                 $mono=  $_POST['mobno'];
                 $temp_status = 0;
                 if($_POST['status']==0)
                     $temp_status=1;

                mysql_query("update user_details set status='$temp_status' where mobile_number ='$mono'");

           }
     ?>

1 Comment

1. If we are using jquery script then why do we need to use the onClick() event in the input tag? 2. There is no form used, can I still pass the data through ajax?
0

You can change it with PHP code or with jQuery AND PHP code.

This because you'll need a server-side langage (in your case PHP) to access your DB. So the server-side script is needed.

To run the server-side script, you can call a php page directly, or you can call it from an AJAX request (in your case jQuery).

Comments

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.