1

This Question was created as my previous question contained 2 question instead of narrowing it down to 1

Aim

When the user selects three variables to access a data, the user will then be able to click on a button to change one of the particulars of that data.

SQL - Database Table (SchoolData)

+-----+--------+------------+------------+----------------+-----------+
|  ID |  Class |   Teacher  |  YearMonth |   Description  |   Status  |
+-----+--------+------------+------------+----------------+-----------+
|  1  |  Alpha |    Sara    |  2017/01   |  Good & Clean  |  Pending  |
+-----+--------+------------+------------+----------------+-----------+
|  2  |  Alpha |    Sara    |  2017/01   |  Has 30 Chairs |  Pending  |
+-----+--------+------------+------------+----------------+-----------+
|  3  |  Alpha |    Sara    |  2017/01   |  Has 30 Tables |  Pending  |
+-----+--------+------------+------------+----------------+-----------+
|  4  |  Alpha |    Sara    |  2017/01   |   5 Subjects   |  Pending  |
+-----+--------+------------+------------+----------------+-----------+
|  5  |  Beta  |    John    |  2016/11   |   Big & Clean  |  Official |
+-----+--------+------------+------------+----------------+-----------+
|  6  |  Beta  |    John    |  2016/11   |   New Student  |  Official |
+-----+--------+------------+------------+----------------+-----------+
|  7  |  Beta  |    John    |  2016/11   | Injured Student|  Official |
+-----+--------+------------+------------+----------------+-----------+
|  8  |  Beta  |    John    |  2016/11   |   6 Subjects   |  Official |
+-----+--------+------------+------------+----------------+-----------+

Webpage

+---------------------------------------------------------------+
|                                                               |
| |>Select Class<|   |>Select Teacher<|   |>Select Year/Month<| |
|                                                               |
|                         (Search)                              |
|                                                               |
|                                                               |
|>>>>>>>>>>>>>>>>>>>>>>>>>>INPUT<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<|
|                                                               |
|      {Class}           {Teacher}           {Description}      |
|                                                               |
|                                                               |
|                        (Update)                               |
|                                                               |
+---------------------------------------------------------------+
  • Legend

    Drop down List - |><|

    Button - ()

    Text Input - {}


Webpage Description

The Update button will change the "Status" of the selected data to "Pending". So if lets say the user searches for | Beta | John | 2016/11 | and clicks on the Update button, the "Status' for that Data will be changed from "Official" to "Pending"

But if the staff searches for a data which "Status' is already "Pending" for example, | Alpha | Sara | 2017/01 |, the Update button will be disabled.


HTML Table

<!--Those data are setting for checking data existance in the database -->
<input type="hidden" name="class" value=" <?php echo $class;  ?>" >
<input type="hidden" name="date" value=" <?php echo  $getDate;  ?>">
<input type="hidden" name="teacher" value=" <?php echo  $teacher;  ?>" >
<input type="hidden" id="inputStatus" name="status" value="">

Update Button (HTML)

<td colspan="1" valign="bottom" align="left">
    <button type="button" class="btn btn-lg update" id="btnUpdate" name="update"> Enable Re-Submit </button>
</td>

Update Button Function(JavaScript)

$(function (){
    $("button#btnUpdate").on('click', function (e){
        e.preventDefault();
        $("#inputStatus").val("update");
        $.ajax({
            type: 'post',
            url: 'changeClassStatus.php'
        });
    }
}

changeClassStatus.php (PHP)

<?php
  $class  = trim($_POST['calss']);
  $teacher = trim($_POST['teacher']);
  $date   = $_POST['date'];
  $date   = $date."/01";
  $status = $_POST['status'];
  $empId = $_POST['empId'];

    if($status == 'update'])){
        $sqlUpdate = mysqli_query($conn,"UPDATE SchoolData SET Status='Pending' WHERE (Class=$class AND Teacher=$teacher AND ID='".$id[$i]."' AND YEAR(MonthYear)= YEAR('$date') AND MONTH(MonthYear)= MONTH('$date'));";
        $sqlExecute=$conn->query($sqlUpdate)or exit("Error code ({$conn->errno}): {$conn->error}");

        echo "Data Status Changed";
    }
?>

Problem Description

I have implemented the codes but they are not updating the status of the searched data. When I do click on the Update button, the data is not updated and nothing happens (No Changes and No Echo Message)

Additional Notes

This question is linked to PHP - SQL - Disable, Hide or Enable Button based on Data. If there are any lacking or inaccurate information or problems with my question, please let me know. Thank you

9
  • what is error ? Commented Dec 18, 2017 at 4:09
  • 2
    In the code provided you don't send any data to the server. Nothing to update/ Commented Dec 18, 2017 at 4:13
  • 1
    Actually you need to send all data through the ajax post method so that php code will work. You are not sending any data Commented Dec 18, 2017 at 4:22
  • 1
    Please add table HTML (2-3 records html), so that we can tell you how you can grab all data and send to your PHP Commented Dec 18, 2017 at 4:23
  • 1
    $conn->query($sqlUpdate) will give you an error, since you already executed the query in the line before, and you are executing an update, so mysqli_query returns a boolean. Commented Dec 18, 2017 at 4:32

3 Answers 3

1

In your If statement lets start but trying to see if all of your variables are being passed into your query correctly you can do this by writing.

echo $class;
echo $teacher;
etc .... 

Inside of your if statement before your query.

I noticed $class = trim($_POST['calss']); is it supposed to be 'class'? or calss?

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

3 Comments

Yeah its a typo, it was supposed to be class. I'll let you know about the echo messages. I will be editing my question shortly. Thank you =D
There are no echo messages. That would mean nothing is being sent :(
Another line of code that doesnt look right is this one: if($status == 'update'])) Make sure you fix your syntax errors.
1

Let me explain to you with your code itself

$("button#btnUpdate").on('click', function (e){
        e.preventDefault();
        $("#inputStatus").val("update");
        $.ajax({
            type: 'post',
            url: 'changeClassStatus.php'
        });
    }

In server-side, PHP expecting the below variables from the AJAX post request

$class  = trim($_POST['calss']);
  $teacher = trim($_POST['teacher']);
  $date   = $_POST['date'];
  $date   = $date."/01";
  $status = $_POST['status'];
  $empId = $_POST['empId'];

So your AJAX request should be rewrtitten in the below given syntax.

The above AJAX request does not contain any post parameters. You should add data attribute to it. See the example below

$.ajax({
    url: 'url',
    type: 'GET',
    data: { field1: "hello", field2 : "hello2"} ,
    contentType: 'application/json; 
    charset=utf-8'
});

1 Comment

I will be editing my question shortly. Thank you for your reply. I will try it out and get back to you =D
0

In jQuery ajax call, pass the field variables to data property in JSON format as your php script gets the field variable values using $_POST, use type as post.

$.ajax({
    url: 'changeClassStatus.php',
    type: 'POST',
    data: { calss: "hello", teacher : "hello2", date : "01/01/2017", status: "Active", empId : 12 },
    contentType: 'application/json; charset=UTF-8',
    success: function (response) {
        console.log(response.status);
    },
    error: function () {
        alert("error");
    }
}); 

Here, your php script will get values if passed in data property in ajax call. In if condition, check for empty variables and update only if values are not empty.

 <?php
      $class  = trim($_POST['calss']);
      $teacher = trim($_POST['teacher']);
      $date   = $_POST['date'];
      $date   = $date."/01";
      $status = $_POST['status'];
      $empId = $_POST['empId'];

if((!empty($status) && $status == 'update') && !empty($class) && !empty($teacher) && !empty($date) && !empty($empId)){
        $sqlUpdate = mysqli_query($conn,"UPDATE SchoolData SET Status='Pending' WHERE (Class=$class AND Teacher=$teacher AND ID='".$id[$i]."' AND YEAR(MonthYear)= YEAR('$date') AND MONTH(MonthYear)= MONTH('$date'));";
        $sqlExecute=$conn->query($sqlUpdate)or exit("Error code ({$conn->errno}): {$conn->error}");

        echo "Data Status Changed";
    }

    ?>

1 Comment

Thank you, I will try this out and get back to you. I will also be editing my question shortly =D

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.