Ajax无法将数据从php加载到div

时间:2015-09-23 16:18:08

标签: javascript php jquery ajax

这是ajax函数

$(document).ready(function() {
        $('#submit').click(function(e) {
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: 'searchphp.php',
                data: {suburb_id: $('#suburb_id').val()},
                success: function(data)
                {
                    $("#tableContent").html(data);
                }
            });
        });
    });   

这是需要接收数据的php文件,它工作得很完美。

<?php
//Check the form if submit by post
    if (isset($_POST["searchBtn"])) {
        $strInputSuburb = "";


        $strInputSuburb = $_POST["suburb_id"];


        //Check if the input box is empty or not
        //if BOTH "Suburb" AND "Street" is empty, it will display the error message.
        if(!empty($strInputSuburb))
        {
            //Connect to database server and table
            include("connection.php");
            @mysqli_select_db($conn, "db")
            or die ("Database not available");

            $querySql1 = "select * from Infringement 
                          where suburb like '%".mysqli_real_escape_string($conn, $strInputSuburb)."%' and Street1 like '%".mysqli_real_escape_string($conn, $strInputStreet)."%'
                          order by Suburb, Fines DESC";

            $result1 = mysqli_query($conn, $querySql1)
                or die ("No information return...");

            $count = mysqli_num_rows($result1);
            $i=1;
            if(!$count==0){
                //do stuff, like echo
            }
            else {
                //do stuff
            } 

            //Release the SQL clause
            mysqli_free_result($result1);
            //Close the connection to database
            mysqli_close($conn);
        }
        else {
        //do stuff
        }
    }   
?>         

我想加载到这个div

<div id="tableContent"></div>

css样式是

#tableContent {

            width:100%;
            height:400px;



        }

输入框位于

之下
<input type="textbox" class="form-control" name="suburb" placeholder="Suburb" id="suburb_id" >
<input type="submit"class="btn" name="searchBtn" id='submit' value="Search" />

我之前使用php从表单中获取数据。使用Ajax后,我删除了“form”标签。

非常感谢你。

1 个答案:

答案 0 :(得分:3)

您没有发送PHP脚本正在检查的searchBtn参数。将其添加到data:选项。

$(document).ready(function() {
    $('#submit').click(function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'searchphp.php',
            data: {
                suburb_id: $('#suburb_id').val(),
                searchBtn: 'Search'
            },
            success: function(data)
            {
                $("#tableContent").html(data);
            }
        });
    });
});

或者从PHP脚本中删除该检查,然后测试if (isset($_POST['suburb_id']))