简体   繁体   English

AJAX表单发送信息,PHP无法执行

[英]AJAX form sends information, PHP doesn't execute

i had a issue with this exact form a fews days ago and was fixed with a different PHP thanks to you guys. 几天前,我遇到了一个与此确切形式有关的问题,感谢你们,使用其他PHP进行了修复。 Now i changed one thing in the form and it does not work anymore. 现在,我更改了表单中的一件事,并且不再起作用。 I have alerted all the variables being sent through AJAX and they are correct values that i need. 我已经警告所有通过AJAX发送的变量,它们是我需要的正确值。 Here are the code for the HTML form: 以下是HTML表单的代码:

 <?php
        include('php/connect.php');

        $chquery = "SELECT * FROM rooms";
        $chresult = mysqli_query($conn, $chquery);

        while($chrow = mysqli_fetch_array($chresult)){
            echo "<div class='edit_roomRow'>";
            echo "<h1> Rum " . $chrow['Roomnumber'] . "</h1>";
            echo "<form>";
            echo "Rumsnummer:<br> <input id='c-roomnumber' type='text' value = '" . $chrow['Roomnumber'] . "'><br>";
            echo "Beskrivning:<br> <input id='c-description' type='text' value = '" . $chrow['Description'] . "'>";
            echo "</form>";
            echo "<br>";
            *update* echo "<div class='btn btn-warning change-btn' data-value='". $chrow['ID'] . "' style='float: right; margin-top: 100px;'>Ändra</div>";
            echo "</div>";
            echo "<hr/>";
        }

        mysqli_free_result($chresult);

        mysqli_close($conn);

 ?>

The AJAX is here: AJAX在这里:

<script type="text/javascript">
    $(".change-btn").click(function(){
        var id = $(this).attr("data-value");
        var description = $("#c-description").val();
        var roomnumber = $("#c-roomnumber").val();
        //Call to ajax
        $.ajax({
            method:"GET",
            url: "php/changepost.php",
            data:{ id: id, description: description, roomnumber: roomnumber },
            success: function(){
                $("#c-description").val("");
                $("#c-roomnumber").val("");
                //Reload specific div with rooms, avoid full page reload
                $(".changerooms").load(location.href + " .changerooms");
            }
        })
    })
</script>

I have tried changing around the variables, in the php etc. 我试过改变变量,在php等中。

<?php
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    include('connect.php');

    if(isset($_GET['id'])){
        $stmt = mysqli_prepare($conn, "UPDATE rooms SET Roomnumber = ?, Description = ?, WHERE ID = ?");
        mysqli_stmt_bind_param($stmt, "isi", $_GET['roomnumber'], $_GET['description'], $_GET['id']);
        mysqli_stmt_execute($stmt);
    }

    mysqli_close($conn);

?>

The DB is setup like this: 数据库的设置如下:

ID | Description | Roomnumber| Cleaned | Cleaner | Time

The last three are irrelevant here but figued i show the full db setup. 最后三个在这里无关紧要,但是我显示了完整的数据库设置。 I can add new posts etc. with the same values but when it comes to changing something isn't right with this code. 我可以添加具有相同值的新帖子等,但是当涉及到更改时,此代码不正确。 Hope someone can help me with this :) 希望有人可以帮助我:)

You're reusing the same IDs in each DIV, you need to use classes instead. 您在每个DIV中重复使用相同的ID,而需要使用类。 Then use DOM traversal functions to get the value of the input in the same DIV as the button the user clicked on. 然后,使用DOM遍历函数以与用户单击的按钮相同的DIV获取输入的值。

This will require moving the </div> for the .edit_roomRow DIV to after the .change-btn DIV, so it's in the same DIV as the form. 这将需要将.edit_roomRow DIV的</div>移动到.change-btn DIV之后,因此它与表单位于同一DIV中。

PHP: PHP:

<?php
include('php/connect.php');

$chquery = "SELECT * FROM rooms";
$chresult = mysqli_query($conn, $chquery);

while($chrow = mysqli_fetch_array($chresult)){
    echo "<div class='edit_roomRow'>";
    echo "<h1> Rum " . $chrow['Roomnumber'] . "</h1>";
    echo "<form>";
    echo "Rumsnummer:<br> <input class='c-roomnumber' type='text' value = '" . $chrow['Roomnumber'] . "'><br>";
    echo "Beskrivning:<br> <input class='c-description' type='text' value = '" . $chrow['Description'] . "'>";
    echo "</form>";
    echo "<br>";
    echo "<div class='btn btn-warning change-btn' data-value='". $chrow['ID'] . "' style='float: right; margin-top: 100px;'>Ändra</div>";
    echo "</div>";
    echo "<hr/>";
}

mysqli_free_result($chresult);

mysqli_close($conn);

?>

JS: JS:

$(".change-btn").click(function(){
    var div = $(this).closest(".edit_roomRow");
    var id = $(this).attr("data-value");
    var description = div.find(".c-description").val();
    var roomnumber = div.find(".c-roomnumber").val();
    //Call to ajax
    $.ajax({
        method:"GET",
        url: "php/changepost.php",
        data:{ id: id, description: description, roomnumber: roomnumber },
        success: function(){
            div.find(".c-description").val("");
            div.find(".c-roomnumber").val("");
            //Reload specific div with rooms, avoid full page reload
            $(".changerooms").load(location.href + " .changerooms");
        }
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM