Php 使用ajax post方法的变量不起作用

Php 使用ajax post方法的变量不起作用,php,jquery,mysql,ajax,twitter-bootstrap-3,Php,Jquery,Mysql,Ajax,Twitter Bootstrap 3,除了DeleteUser(id)之外的所有其他函数都不起作用,我只能使用post方法传递第一个变量,我想这是我尝试过多次的“id”,在addRecord.php脚本中它给了我 PHP注意:未定义索引:id1, PHP注意:未定义索引:date1 继续下去,只是为了 DeleteUser(id)它工作正常我尝试了本教程并替换了表的详细信息,它很好地显示了表和按钮,但我无法从中添加记录和更新记录 function addRecord() { // get values

除了DeleteUser(id)之外的所有其他函数都不起作用,我只能使用post方法传递第一个变量,我想这是我尝试过多次的“id”,在addRecord.php脚本中它给了我

PHP注意:未定义索引:id1, PHP注意:未定义索引:date1

继续下去,只是为了 DeleteUser(id)它工作正常我尝试了本教程并替换了表的详细信息,它很好地显示了表和按钮,但我无法从中添加记录和更新记录

function addRecord() {
        // get values
        var id = $("#id").val();
        var date = $("#date").val();
        var time = $("#time").val();
        var news = $("#news").val();

        // Add record
        $.post("ajax/addRecord.php", {
           id1:id,
            date1:date,
            time1:time,
    news1: news          //if i give news1:"test" it does not work
        }, function (data, status) {
            // close the popup
            $("#add_new_record_modal").modal("hide");

            // read records again
            readRecords();

            // clear fields from the popup
            $("#id").val("");
            $("#date").val("");
            $("#time").val("");
      $("#news").val("");
        });
    }


    $(document).ready(function () {
        // READ recods on page load
        readRecords(); // calling function
    });
    // READ records
    function readRecords() {
        $.get("ajax/readRecords.php", {}, function (data, status) {
            $(".records_content").html(data);
        });
    }


    function DeleteUser(id) {
        var conf = confirm("Are you sure, do you really want to delete User?");
        if (conf == true) {
            $.post("ajax/deleteUser.php", {
                    id: id
                },
                function (data, status) {
                    // reload Users by using readRecords();
                    readRecords();
                }
            );
        }
    }

    function GetUserDetails(id) {
        // Add User ID to the hidden field for furture usage
        $("#hidden_user_id").val(id);
        $.post("ajax/readUserDetails.php", {
                id:id
            },
            function (data, status) {
                // PARSE json data
                var user = JSON.parse(data);
                // Assing existing values to the modal popup fields
                $("#update_id").val(user.id);
                $("#update_date").val(user.date);
                $("#update_time").val(user.time);
     $("#update_news").val(user.news);
            }
        );
        // Open modal popup
        $("#update_user_modal").modal("show");
    }

    function UpdateUserDetails() {
        // get values
        var id = $("#update_id").val();
        var date = $("#update_date").val();
        var time = $("#update_time").val();
        var news = $("#update_news").val();

        // get hidden field value
        var id = $("#hidden_user_id").val();

        // Update the details by requesting to the server using ajax
        $.post("ajax/updateUserDetails.php", {
        id:id,
        news:news
            },
            function (data, status) {
                         // hide modal popup
                $("#update_user_modal").modal("hide");
                // reload Users by using readRecords();
                readRecords();
            }
        );
    }

    $(document).ready(function () {
        // READ recods on page load
        readRecords(); // calling function
    });
下面的代码用于显示表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP and MySQL CRUD Operations Demo</title>

    <!-- Bootstrap CSS File  -->
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
</head>
<body>

<!-- Content Section -->
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1>Demo: PHP and MySQL CRUD Operations using Jquery</h1>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="pull-right">
                <button class="btn btn-success" data-toggle="modal" data-target="#add_new_record_modal">Add New Record</button>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <h3>Records:</h3>

            <div class="records_content"></div>
        </div>
    </div>
</div>
<!-- /Content Section -->


<!-- Bootstrap Modals -->
<!-- Modal - Add New Record/User -->
<div class="modal fade" id="add_new_record_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Add New Record</h4>
            </div>
            <div class="modal-body">

                <div class="form-group">
                    <label for="id">ID</label>
                    <input type="text" id="id" placeholder="ID" class="form-control"/>
                </div>

                <div class="form-group">
                    <label for="date">DAte</label>
                    <input type="text" id="date" placeholder="DAte" class="form-control"/>
                </div>

                <div class="form-group">
                    <label for="time">Time</label>
                    <input type="text" id="time" placeholder="Time" class="form-control"/>
                </div>
<div class="form-group">
                    <label for="news">News</label>
                    <input type="text" id="news" placeholder="News" class="form-control"/>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="addRecord()">Add Record</button>
            </div>
        </div>
    </div>
</div>
<!-- // Modal -->

<!-- Modal - Update User details -->
<div class="modal fade" id="update_user_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Update</h4>
            </div>
            <div class="modal-body">

                <div class="form-group">
                    <label for="update_id">Id</label>
                    <input type="text" id="update_id" placeholder="Id" class="form-control"/>
                </div>

                <div class="form-group">
                    <label for="update_date">date</label>
                    <input type="text" id="update_date" placeholder="Date" class="form-control"/>
                </div>

                <div class="form-group">
                    <label for="update_time">Email Address</label>
                    <input type="text" id="update_time" placeholder="Email Address" class="form-control"/>
                </div>
<div class="form-group">
                    <label for="update_news">Email Address</label>
                    <input type="text" id="update_news" placeholder="Email Address" class="form-control"/>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="UpdateUserDetails()" >Save Changes</button>
                <input type="hidden" id="hidden_user_id">
            </div>
        </div>
    </div>
</div>
<!-- // Modal -->

<!-- Jquery JS file -->
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>

<!-- Bootstrap JS file -->
<script type="text/javascript" src="js/bootstrap.min.js"></script>

<!-- Custom JS file -->
<script type="text/javascript" src="js/script.js"></script>

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-75591362-1', 'auto');
    ga('send', 'pageview');

</script>
</body>
</html>

PHP和MySQL CRUD操作演示
演示:使用Jquery的PHP和MySQL CRUD操作
添加新记录
记录:
&时代;
添加新记录
身份证件
日期
时间
新闻
取消
添加记录
&时代;
更新
身份证件
日期
电子邮件地址
电子邮件地址
取消
保存更改
(函数(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]| |函数(){
(i[r].q=i[r].q | |[]).push(参数)},i[r].l=1*新日期();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(窗口,文档,“脚本”,“www.google-analytics.com/analytics.js”,“ga”);
ga(“创建”、“UA-75591362-1”、“自动”);
ga(‘发送’、‘页面浏览’);

用于插入的php脚本正在工作,我已经测试过了,但是他们没有得到post变量

 $.ajax({ 
url: 'ajax/addrecords.php', 
data: {'id1' : id, 'news1':news, 'time1':time, 'date1':date}, 
type: 'post',
 dataType:'json', 
success: function(output) { alert(output); }, 
error: function(request, status, error){ alert("Error: Could not delete"); } 
}); 

显示您的
addrecord.php
deleteuser.php
code plsIn addrecords.php这一行中的有效值“$query=”INSERT INTO tnews(date,time,news,valid)值(“$date”,“$time”,“$news”,“valid”)”`这是数据库中的一个字段。实际上,php显示未找到后可变索引,即id、日期、时间、新闻[01-Jun-2017 18:23:20 UTC]php通知:未定义索引:id1/ajax/addRecord.php第9行[01-Jun-2017 18:23:20 UTC]php通知:未定义索引:date1/ajax/addRecord.php第10行[01-Jun-2017 18:23:20 UTC]PHP通知:未定义索引:第11行的time1/ajax/addRecord.PHP[2017年6月1日18:23:20 UTC]PHP通知:第12行的未定义索引:news1 in/ajax/addRecord.PHP像这样包装,并再次在PHP文件
$.post(“ajax/addRecord.PHP”,“id1”:id,“date1”:date,“time1”:time,“news1”:news}中回显变量
同样的错误也会发生,当我尝试使用udpate函数时,会插入空值,但它也会为新闻提供未定义的索引,但它会获得正确的id并更新正确的行OP正在使用可接受的jQuery方法,
$.post()
用于AJAX调用。此代码没有改变任何内容。它给了我相同的错误,php文件名为addRecord.php,但仍然不希望它提供未填充索引