简体   繁体   English

HTML表单POST到AJAX,然后POST到PHP页面

[英]HTML form POST to AJAX and then POST to PHP page

I have a PHP page with a search function and I use AJAX for pagination. 我有一个带有搜索功能的PHP页面,并且使用AJAX进行分页。 When I submit my search input, AJAX should grab the input and pass it to the PHP page which will further query the database. 当我提交搜索输入时,AJAX应该获取输入并将其传递到PHP页面,该页面将进一步查询数据库。 The last bit does not work and I have been struglling to understand why. 最后一点不起作用,我一直在努力理解原因。 Could anyone help please. 任何人都可以帮忙。 The code I have is below: 我的代码如下:

PHP/HTML page with form: PHP / HTML页面,格式为:

<form action="" id="postData" method="post">
<p><b> Search all videos in database below: </b></p>
<ul><center> &nbsp Keywords: </center>
    <input type="text" id="input" name="input" size="50" maxlength="64">
</ul>
<ul>
    <center><input type="submit" name = "submit" value ="Search"></center>
</ul>
</form>

Javascript with AJAX: 带有AJAX的Javascript:

$(document).ready(function(){
    function loading_show(){
        $('#loading').html("<img src='../images/loading.gif'/>").fadeIn('fast');
    }
    function loading_hide(){
        $('#loading').fadeOut('fast');
    }                
    function loadData(page){
        loading_show();                    
        $.ajax
        ({
            type: "POST",
            url: "search_videos.php",
            data: { 'page': page, 'input': $('#postData').serialize()},
            success: function(msg)
            {
                $("#container").ajaxComplete(function(event, request, settings)
                {
                    loading_hide();
                    $("#container").html(msg);
                });
            }
        });
    }
    loadData(1);  // For first time page load default results
    $('#container .pagination li.active').live('click',function(){
        var page = $(this).attr('p');
        loadData(page);
    });           
    $('#go_btn').live('click',function(){
        var page = parseInt($('.goto').val());
        var no_of_pages = parseInt($('.total').attr('a'));
        if(page != 0 && page <= no_of_pages){
            loadData(page);
        }        
    });
});

PHP to query and print the results: PHP查询和打印结果:

<?php
 if($_POST['page'])
 {
 $page = $_POST['page'];
 $cur_page = $page;
 $page -= 1;
 $per_page = 5;
 $previous_btn = true;
 $next_btn = true;
 $start = $page * $per_page;

include"core/database/connect.php";

// Get the input of the selected video
$input = $_POST['input'];
//$input = 'video';
echo $input; // CANNOT get input variable from the initial form!

... The mysql query and rest of the code continued ...

HTML: HTML:

<div>
<p><b> Search all videos in database below: </b></p>
<ul>
    <li>
        <label for="keywords">Keywords</label>
        <input type="text" id="keywords" name="keywords" size="50" maxlength="64" />
    </li>
    <li>
        <button onClick="loadData()">Search</button>
    </li>
</ul>
</div>
<div id="container">
</div>

Javascript: Javascript:

$(document).ready(function(){
    // ...              
    function loadData()
    {
        loading_show();
        $.ajax
        ({
            type: "POST",
            url: "http://yourwebsite.com/search_videos.php",
            data: { 'keywords': $('#keywords').val() },
            success: function(msg)
            {
                loading_hide();
                $("#container").html(msg);
            }
        });
    }
});

PHP: PHP:

echo $_POST['keywords'];

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

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