当我使用Jquery $ .post时查询不起作用

时间:2022-05-13 04:48:47

I have this query running fine when I send the form information via post to the process page. It renders the information expected. But when I send the post information through a $.post call through jquery I get nothing. If I omit the part starting with registered_datetime in the query, it works as expected.

当我通过post将表单信息发送到流程页面时,我的查询运行正常。它呈现预期的信息。但是当我通过jquery通过$ .post调用发送帖子信息时,我什么都没得到。如果我在查询中省略了以registered_datetime开头的部分,它将按预期工作。

I have to believe it's something I'm overlooking.

我不得不相信这是我忽略的东西。

Here is the query:

这是查询:

SELECT * FROM leads 
WHERE (first_name LIKE '" . $_POST['name'] . "%' 
OR last_name LIKE '". $_POST['name'] ."%') OR
registered_datetime BETWEEN '". date_fix($_POST['from_date']) ."' AND '" . date_fix($_POST['to_date']) . "'";

Here is the index page that has the jquery and form:

这是包含jquery和form的索引页面:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </script>
    <script>
        $(document).ready(function(){
            $('#search').keyup(function(){
                $('#test_form').submit();
            });
            $('#test_form').submit(function(){
                $.post(
                        $(this).attr('action'),
                        $(this).serialize(),
                        function(data){
                            $('#results').html(data.html);
                        },
                        "json"
                    );
                return false;
            });
            $('#test_form').submit();
        });
    </script>
</head>
<body>
    <form id="test_form" action="process.php" method="post">
        Name: <input type="text" name="name" id="search">
        From: <input class="datepicker" type="text" name="from_date" id="from_date">
        To: <input class="datepicker" type="text" name="to_date" id="to_date">
    </form>
    <div id="results"></div>
</body>

Here is the process page:

这是流程页面:

require_once('connection.php');
    function date_fix($date) {
        $fix = explode('/', $date);
        $new_date = $fix[2] . '-' . $fix[0] . '-' . $fix[1];
        return $new_date;
    }
    // var_dump($_POST);
    $query =  "SELECT * FROM leads 
               WHERE (first_name LIKE '" . $_POST['name'] . "%' 
               OR last_name LIKE '". $_POST['name'] ."%') OR
               registered_datetime BETWEEN '". date_fix($_POST['from_date']) ."' AND '" . date_fix($_POST['to_date']) . "'";
    // var_dump($_POST);
    // var_dump($query);
    $users = fetchAll($connection, $query);
    $data = array();
    $html = "
    <table>
        <thead>
            <tr>
                <th>User ID</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Registered Datetime</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
        ";

    foreach ($users as $user) {
        $html .= 
            '<tr>
                 <td>' . $user['id']  . '</td>
                 <td>' . $user['first_name']  . '</td>
                 <td>' . $user['last_name']  . '</td>
                 <td>' . $user['registered_datetime']  . '</td>
                 <td>' . $user['email']  . '</td>
            </tr>';
    }
    $html .= '</tbody></table>';

    $data['html'] = $html;
    // echo $data['html'];
    echo json_encode($data);

1 个解决方案

#1


0  

I figured out the issue. It actually was the fact I was submitting the form at the end of the script therefore resetting the information being submitted.

我想出了这个问题。实际上,我在脚本末尾提交表单,因此重置了提交的信息。

 <script>
        $(document).ready(function(){
            $('#search').keyup(function(){
                $('#test_form').submit();
            });
            $('#test_form').submit(function(){
                $.post(
                        $(this).attr('action'),
                        $(this).serialize(),
                        function(data){
                            $('#results').html(data.html);
                        },
                        "json"
                    );
                return false;
            });
            $('#test_form').submit(); // IF I OMIT THIS IT WILL WORK AS EXPECTED.
        });

#1


0  

I figured out the issue. It actually was the fact I was submitting the form at the end of the script therefore resetting the information being submitted.

我想出了这个问题。实际上,我在脚本末尾提交表单,因此重置了提交的信息。

 <script>
        $(document).ready(function(){
            $('#search').keyup(function(){
                $('#test_form').submit();
            });
            $('#test_form').submit(function(){
                $.post(
                        $(this).attr('action'),
                        $(this).serialize(),
                        function(data){
                            $('#results').html(data.html);
                        },
                        "json"
                    );
                return false;
            });
            $('#test_form').submit(); // IF I OMIT THIS IT WILL WORK AS EXPECTED.
        });