简体   繁体   English

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

[英]Ajax cannot load data from php to div

This is the ajax function 这是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);
                }
            });
        });
    });   

this is the php file need to receive data, it worked perfect. 这是需要接收数据的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
        }
    }   
?>         

i want load to this div 我想加载到这个div

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

the css style is css风格是

#tableContent {

            width:100%;
            height:400px;



        }

The input box is below 输入框在下面

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

I used php to get data from form before. 我之前使用php从表单中获取数据。 after using Ajax, I deleted "form" tag. 使用Ajax后,我删除了“form”标签。

Thank you so much. 非常感谢。

You're not sending the searchBtn parameter, which the PHP script is checking for. 您没有发送PHP脚本正在检查的searchBtn参数。 Add it to the data: option. 将其添加到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);
            }
        });
    });
});

Or remove that check from the PHP script, and test if (isset($_POST['suburb_id'])) instead. 或者从PHP脚本中删除该检查,然后测试if (isset($_POST['suburb_id']))

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

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