Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda