简体   繁体   English

如何从HTML / AJAX输出PHP / SQL结果

[英]How do I output PHP/SQL results from HTML/AJAX

If I call the .php file directly it works great, so I believe the error is in how I am displaying the AJAX result. 如果直接调用.php文件,则效果很好,因此,我认为错误在于我如何显示AJAX结果。 I am new to AJAX. 我是AJAX的新手。 I have a form that the user can select dates to search on, etc and I would like to results to be displayed when returned. 我有一个表单,用户可以选择要搜索的日期,等等,我希望返回时显示结果。

$("#searchForm").on("submit", function (event) {
    event.preventDefault();

    $.ajax({
        type: "POST",
        url: "searchnow.php",
        data: $('#searchForm').serialize(),
        success : function (output) {
            console.log(output);
        }
    });
});


searchnow.php
 <div id="output">
   <div class="container-fluid">
    <div class='features'>
        <?php
        include ("config.php");
        $con = mysql_connect($server, $user, $password) or die('Sorry, could not connect to database server');
        mysql_select_db($database, $con) or die('Sorry, could not connect to database');

        $query = "SELECT * FROM hsevents WHERE 
            CURDATE()<=enddate AND 
            verified='Y' 
            ORDER BY location";

        $result = mysql_query($query) or die('Sorry, could not access the database at this time ');
        $SPACES = "</br><b>Description:</b>&nbsp;";

        if (mysql_num_rows($result) == 0)
        {
            echo "<h3>Sorry, there are no classes or events that meet your criteria.</h3>";
        } 
        else
        {
            $i = 1;
            while($row=mysql_fetch_array($result, MYSQL_ASSOC))
            {
                $unique = 'unique' . $i;
                $tempname=htmlentities($row[name]);
                $tempcontact=htmlentities($row[contact]);                   
                $tempbegdate = date("m-d-Y", strtotime($row[startdate]));
                $tempenddate = date("m-d-Y", strtotime($row[enddate]));


                ob_start();
                if ( ($i%4) === 0) {
                    echo "<div class='row row-padded row-bordered row-centered'>";
                }

                echo "
                <div class='col-md-3'> 
                    <div class='panel'>
                        <div class='panel-heading'>
                            <img src='images/$row[category]' class='img-responsive img-thumbnail img-hsb'>
                        </div>
                        <div class='panel-body text-center'>
                            $tempname</br>
                            Start Date: $tempbegdate</br>
                            End Date: $tempenddate</br>
                            Location: $row[location]</br>
                            Age Range: $row[lowerage] - $row[upperage]</br>
                            Contact: <a href=$tempcontact>$tempcontact</a></br>
                            <div id='$unique' class='collapse collapse-hsb'>
                                $tempdescription
                            </div>
                        </div>
                    </div>                          
                </div>
                ";

                if ( ($i%4) === 0) {
                    echo "</div>";
                }

                $classdata = ob_get_contents();
                ob_end_clean();
                ?>
                <?php echo $classdata ?>
                <?php
                    $i++;
            }   

            $classdata = ob_get_contents();
            ob_end_clean();
            echo $classdata;
        }
        ?>

    </div>
</div>
</div>

You just need to provide the DIV or any Selector inside the success function, to replace the DIV or selector html with the AJAX success output. 您只需要在成功函数中提供DIV或任何选择器 ,即可用AJAX成功输出替换DIV选择器 html。 Here i used #MyDivId which should be your html element in which you need to print the AJAX output. 在这里,我使用了#MyDivId ,它应该是您需要打印AJAX输出的html元素。

 $("#searchForm").on("submit", function (event) {
    event.preventDefault();

$.ajax({
    type: "POST",
    url: "searchnow.php",
    data: $('#searchForm').serialize(),
    success : function (output) {
        output = output.trim(); // Trim's unwanted white space around the output
        if (output != "") {
            $("#MyDivId").html(output); // This Adds the AJAX output inside #MyDivId
        }
    }
});

}); });

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

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