I would like to update the status using AJAX method in php. In the php codes I have added select command to change the status to completed and the value is passed in update.php file. But no changes is happen

AJAX Code:
<script>
function updatestatus(status) {
    if(str == '') {
        document.getElementById("res").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        } else { // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (this.readyState==4 && this.status==200) {
            document.getElementById("res").innerHTML=this.responseText;
        }
    }
    xmlhttp.open("GET","update.php?id=$row[id]"+str,true);
    xmlhttp.send();
}

PHP Code : I need to update the status from assigned to complete, plz help me in this regard.

while ($row = mysqli_fetch_array($res)) {
    echo "<tr>";
        echo "<td>".$row['project'];"</td>";
        echo "<td>".$row['date'];"</td>";
        echo "<td>".$row['tl_name'];"</td>";
        echo "<td>".$row['subject'];"</td>";
        echo "<td>".$row['details'];"</td>";
        echo "<td>  
                <form method='POST' action=''>
                <select name='status' id='status'>
                    <option value='Assigned'>Assigned</option>
                    <option value='Completed'>Completed</option>
                </select>
                </form>
            </td>";
        echo "<td><input type='submit' id='button' name='button' onsubmit='updatestatus(this.value)' value='UPDATE'></td>"; 
    echo "</tr>";   
    }
In the update.php am using the below code:


    $status = $_POST['status'];
   $id = $_GET['id'];
   $sel = "update workassign set status ='$status' where id ='$id'";
   $res = mysqli_query($conn,$sel);

I need to update the status from assigned to complete, plz help me in this regard.

1 Answers

0
Nipun On Best Solutions

You are doing many things wrong. Your <input type=submit> is unnecessory and you are passing a wrong value to updatestatus method. You can do it simply by using a button. And in your ajax there is no variable str declared. In your update.php, you are trying to retrieve a variable that is not being sent. Here is a simpler code for you.

<form>
    <select name='status' id='status'>
        <option value='Assigned'>Assigned</option>
        <option value='Completed'>Completed</option>
    </select>
</form>
<input type='button' id='button' name='button' onclick='updateStatus()' value='UPDATE'>

ajax

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script>
    function updateStatus() {
        var status = $('#status').val();
        $.ajax({ 
            url: "update.php?id=<?php echo $row['id'] ?>&status="+status,
            success: function(res) { 
                  console.log(res);
            }
        });
    }
</script>

In your update.php retrieve values as follows

$status = $_GET['status'];
$id = $_GET['id'];
// perform your updation