首页 > 解决方案 > 我正在尝试使用 ajax POST 方法从 html 页面将数据发送到我的 PHP 页面,但它给出了错误,注意:未定义的索引:

问题描述

<head>
    <title>Document</title>
    <script>
        $(document).ready(function () {
            $("#search").on("keyup", function () {
                var search_term = $(this).val();
                console.log('value--', search_term)
                $.ajax({
                    url: "ajax-live-search.php",
                    type: "POST",
                    data: { search: search_term },
                    success: function (ajaxresult) {
                        $("table-data").html(ajaxresult);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <div id="search-bar">
        <label>Search</label>
        <input type="text" id="search" autocomplete="off">
    </div>
    <div id="table-data">
    </div>
</body>

PHP 页面

$search_input = $_POST["search"];
echo $search_input;

错误

注意:未定义索引:在第 3 行的 C:\xampp\htdocs\ajax\ajax-live-search.php 中搜索

标签: javascriptphpjqueryajax

解决方案


将“类型”更改为“方法”,如下所示:

   $.ajax({
            url: "ajax-live-search.php",
            method: "POST",
            data: { search: search_term },
            success: function (ajaxresult) {
                     $("table-data").html(ajaxresult);
                 }
           });

推荐阅读