Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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
     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 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

        $(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. I think I need to make data equal to the search results but not sure how to go about it.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

submitted the console log says data is not defined.

You need to define data in the success function.

$(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);
           }
       });

   });

 });
about 4 years ago · Santiago Trujillo Report

0

The console log saying "data" is not defined means there is no variable named "data". to solve this issue, you could either create a global variable called "data", or pass "data" as an argument to the success function. 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);
               }
           });

       });

   });
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!