duannaikuang1301 2017-10-05 02:07
浏览 58
已采纳

Ajax函数,用于显示特定div中提交的结果

So I'm working on storing appointments of a calendar inside my db and I need to check the db for the name of the person before finishing the modal.

The code of the field, button and div where I need to show the result:

<div class="alinhar-esquerda"> 
   <input class="form-control" name="cpf_paciente" id="cpf_paciente" placeholder="" type="text" value="">
</div>
<div class="alinhar-direita">
   <button type="submit" class="btn btn-primary" id="searchButton">Pesquisar</button>
</div>
<div class="controls controls-row" id="resultado" style="margin-top:5px;">
</div>
<?php
include("database2.php");
   if(isset($_POST["cpf_paciente"])){
      $cpf_paciente = $_POST["cpf_paciente"];
      $pesquisa = $connection->query("SELECT `id`, `cpf_paciente`, `nome_paciente`, `sobrenome_paciente` FROM  `pacientes` WHERE cpf_paciente='$cpf_paciente'");
      while($row = $pesquisa->fetch(PDO::FETCH_ASSOC)) {
         echo $row['nome_paciente'] . " " . $row['sobrenome_paciente'];
      }
   }
?>  
</div>
</div>

I need an Ajax code to receive the data from #submitButton input and then go through the php code inside the div #resultado without reloading the page, because all this is inside a modal which I'll send the results after getting the name of the person.

Tried something like this but no success:

$('#searchButton').submit(function() { // catch the data from submit event
$.ajax({ // create an AJAX call...
  data: $(this).serialize(), // get the data
  type: $(this).attr('POST'), // POST
  url: $(this).attr(''), // no file to call
  success: function(response) { // on success..
      $('#resultado').html(response); // update the DIV
  }
});
});
  • 写回答

3条回答 默认 最新

  • douyi4991 2017-10-05 02:17
    关注

    It should be:

    $('#searchButton').click(function() { // catch the data from submit event
        $.ajax({ // create an AJAX call...
          data: { cpf_paciente: $("#cpf_paciente").val() },
          type: "POST",
          url: '',
          success: function(response) { // on success..
              $('#resultado').html(response); // update the DIV
          }
        });
    });
    

    You can't use $(this).serialize() unless this is a <form>. It doesn't work with just a button (how would it know which inputs to serialize?).

    The type: should just be 'POST, since the button doesn't have a POST attribute.

    The URL should be '' to go to the same URL as the current page, $(this).attr('') is meaningless.

    And the submit event only applies to a form. For a button you use .click().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多