更新(编辑)按钮不能正常工作..?

时间:2017-01-26 04:18:48

标签: php ajax

此页面存在一个问题: - 我是一个基于Php& amp;的简单例子。基于数据库的ajax。 编辑按钮单击但不编辑数据库中的记录。

Ajaxpage.php

<!DOCTYPE html>
<html>
    <head>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
    </head>
    <body>
        <?php
        include 'conn1.php';
        $sql = "select * from tmp";
        $query = mysql_query($sql);
        $row = mysql_fetch_assoc($query)
        ?>
        <table border="">
            <tr>
                <td>FName: </td>  
                <td><input type="text" name="fname" value="<?php echo $row['fname']; ?>" id="fname"></td>
            </tr>
            <tr>
                <td id="lname1" >Lname: </td>  
                <td><input type="text" name="lname" id="lname" value="<?php echo $row['fname']; ?>"></td>
            </tr>
            <tr> <td><input type="button" name="submit" id="submit" value="submit"></td> </tr>                      
        </table>


        <p>Result:</p>
        <div id="result"></div>

        <script type="text/javascript">
            $(document).ready(function () {


                $("#submit").click(function () {
                    var fname = $("#fname").val();
                    var lname = $("#lname").val();
                    $.ajax({
                        "type": "POST",
                        "url": "<?php echo "insert.php" ?>",
                        data: {"fname": fname, "lname": lname},
                        success: function (data) {
                            $("#fname").val('');
                            $("#lname").val('');
                            show();
                        }
                    });
                });
                $.ajax({
                    "type": "POST",
                    "url": "<?php echo "insert.php"; ?>",
                    data: {"action": "show"},
                    success: function (response)
                    {
                        $("#result").append(response);
                    }
                });
                $("body").on("click", ".edit", function ()
                {

                    $.ajax({
                        url: 'insert.php',
                        type: 'POST',
                        data: {"action": "edit"},
                        datatype: 'html',
                        success: function (rsp) {
                            alert(rsp);
                        }
                    });
                });
            });
        </script>
    </body>

</html>

编辑按钮点击但不更新记录..... 所以请求帮助。

insert.php

 <?php
error_reporting(0);
include 'conn1.php';
if (isset($_POST['fname'])) {
    $name = $_POST['fname'];
    $lname = $_POST['lname'];
    mysql_query("insert into tmp(id,fname,lname) values('','$name','$lname')", $conn);
    echo "inserted successfully";
    exit;
}



if ($_POST['action'] == 'show') {
    $sql = "select * from tmp";
    $query = mysql_query($sql);

    echo "<table border='1'>";
    echo "<tr><th>id</th><th>fname</th><th>lname</th><th>edit</th></tr>";
    while ($row = mysql_fetch_assoc($query)) {
        echo "<tr><td> $row[id]</td>"
        . "<td> $row[fname]</td>"
        . "<td>$row[lname]</td>"
        . "<td><input type='button' name='edit' class='edit'  id='$row[id]' value='edit'/></td>"
        . "</tr>";
    }
    echo "</table>";
    exit;
}


if ($_POST['action'] == 'edit') {

    $id = $_POST['id'];
    extract($_POST);
    $update = "update tmp SET fname='$fname', lname='$lname' where id='$id'";
}
?>

0 个答案:

没有答案