使用AJAX方法更新状态

时间:2019-05-04 09:53:16

标签: php mysql ajax

我想使用php中的AJAX方法更新状态。 在php代码中,我添加了select命令以将状态更改为完成,并将值传递到update.php文件中。但是没有改变

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);

我需要将状态从已分配更新为完成,请在这方面帮助我。

1 个答案:

答案 0 :(得分:0)

您做错了很多事情。您的<input type=submit>不必要,并且您向updatestatus方法传递了错误的值。您只需使用button即可完成此操作。而且在您的ajax中,没有声明变量str。在您的update.php中,您尝试检索未发送的变量。 这是给您的简单代码。

<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>

在您的update.php中,按如下所示检索值

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