简体   繁体   English

用来自PHP文件的内容替换div内容

[英]Replace div contents with content from PHP file

This is the php file containing the new content, to replace .hotels when the form is submitted. 这是包含新内容的php文件,用于在提交表单时替换.hotels。

    <?php
     if(isset($_POST['submit'])){
        $pdo = new PDO('mysql:host=localhost;dbname=CSV_DB', 'root', 'root');

        $inputType = $_POST['type'];
        $inputCategory = $_POST['category'];
        $type = $pdo->query("SELECT * FROM Hotels WHERE Type LIKE '%$inputType%' AND Price_Range= '$inputCategory'");
        foreach($type as $row){
            echo"<div id='newHotels'>";
            echo "<div class='hotelImage'></div>";
            echo'<h4>'.$row['Hotel_Name']." ".$row['Rating']."*".'</h4>'." ".'<p>'.$row['Description'].'</p>'." "."Location: ".$row['Location']." ".'<h4 style="font-weight: bold">'."£".$row['Price']."p/p".'</h4>';
            echo "</div>";}
        }

    ?>

Here is the original div that displays all the hotels in the database when the page loads and that I want to replace with the new content when the form is submitted: 这是原始div,它在页面加载时显示数据库中的所有酒店,并且在提交表单时我想用新内容替换:

    <div id="hotels">
    <?php
        $pdo = new PDO('mysql:host=localhost;dbname=CSV_DB', 'root', 'root');
        $display = $pdo->query('SELECT Hotel_Name, Description, Location, Rating, Price FROM Hotels');
      foreach ($display as $row){
        if($row)
            echo"<div id='hotel'>";
            echo "<div class='hotelImage'></div>";
            echo'<h4>'.$row['Hotel_Name']." ".$row['Rating']."*".'</h4>'." ".'<p>'.$row['Description'].'</p>'." "."Location: ".$row['Location']." ".'<h4 style="font-weight: bold">'."£".$row['Price']."p/p".'</h4>';
            echo "</div>";}

    ?>

   </div>

And here is the Ajax that is supposed to change the content 以下是应该更改内容的Ajax

        $(function () {
            $('form').on('submit', function (e) {
            e.preventDefault();

            $.ajax({
               type: 'post',
               url: 'form.php',
               data: $('form').serialize(),
               success: function(data) {
                   $(".hotels").html(data);
                   }
               });

           });

       });

I need help replacing the contents of .hotels with the search results of my form when the form submits however the div just becomes empty. 当表单提交时,我需要帮助将.hotels的内容替换为我的表单的搜索结果但是div才变空。 I think I need to make data equal to the search results but not sure how to go about it. 我认为我需要使数据与搜索结果相同但不确定如何进行。

submitted the console log says data is not defined. 提交控制台日志表示数据未定义。

You need to define data in the success function. 您需要在success函数中定义数据。

$(function () {
    $('form').on('submit', function (e) {
    e.preventDefault();

    $.ajax({
       type: 'post',
       url: 'form.php',
       data: $('form').serialize(),
       success: function(data) { /* you missed variable data here */
           $("#hotel").html(data);
           }
       });

   });

 });

The console log saying "data" is not defined means there is no variable named "data". 没有定义“数据”的控制台日志意味着没有名为“data”的变量。 to solve this issue, you could either create a global variable called "data", or pass "data" as an argument to the success function. 要解决此问题,您可以创建一个名为“data”的全局变量,或者将“data”作为参数传递给success函数。 This would then look somewhat like this: 这看起来有点像这样:

$(function () {
        $('form').on('submit', function (e) {
        e.preventDefault();

        $.ajax({
           type: 'post',
           url: 'form.php',
           data: $('form').serialize(),
           success: function(data) {
               $("#hotel").html(data);
               }
           });

       });

   });

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

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