dongtong0796 2016-02-05 13:57
浏览 24
已采纳

使用ajax和php显示文本

I'm just trying to do a simple thing in ajax but it does not work. I just want to display a text when i click on the input button

ajax.js

jQuery(document).ready(function($) {
$('#insertForm').change(function(){
//on recupere la valeur de l'attribut value pour afficher tel ou tel resultat
var req=$('#insertForm').val();
//requête ajax, appel du fichier function.php
$.ajax({
  type: "POST",
  url: "lib/function.php",
  data: "insertForm="+req,
  dataType : "html",
  //affichage de l'erreur en cas de problème
  error: function(XMLHttpRequest, textStatus, errorThrown) {
          alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
      },
      //function s'il n'y a pas de probleme
  success:function(data){
    $('.coucou').empty();
    $('.coucou').prepend(data.coucou)
  }
});
});
}); 

html.php

<div class="coucou"></div>
<button type="button" class="btn btn-success btn-xs" name="insertForm" id="insertForm" style="margin-top: 5px;;">
    <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
</button>

function.php

if(isset($_POST['insertForm'])){
    echo "coucou";
}
  • 写回答

2条回答 默认 最新

  • douqi3064 2016-02-05 14:14
    关注
    jQuery(document).ready(function($) {
        $('#insertForm').click(function(){
        //on recupere la valeur de l'attribut value pour afficher tel ou tel resultat
            var req=$('#insertForm').val();
            //requête ajax, appel du fichier function.php
            $.ajax({
              type: "POST",
              url: "lib/function.php",
              data: "insertForm="+req,
              dataType : "html",
              //affichage de l'erreur en cas de problème
              error: function(XMLHttpRequest, textStatus, errorThrown) {
                      alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
                  },
                  //function s'il n'y a pas de probleme
              success:function(data){
                $('.coucou').empty();
                $('.coucou').text(data);
              }
            });
        });
    }); 
    

    You need to use .click() and not .change() and also, in the success, you receive only a string not an object so you just need to write data directly (with .text())

    Copy my code because i have also add ; that you had forgotten. (It's work, I tested)

    Another point you use .val() on #insertForm but there is no value attribute in your html. So var req is an empty string

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongzhui4927 2016-02-05 14:04
    关注

    You have a button element not input type button and button element does have text content, you can change to this:

    var req=$('#insertForm').text();
    

    but still i feel you don't have any text in the button then what do you want to post exactly.


    Problems:

    1. button element has text content instead of value.
    2. datatype:"html" is expecting some html in the response.
    3. data.coucou in the success function is expecting json data.
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java的代码出问题了
  • ¥15 yolov8 如何调cfg参数
  • ¥15 这个四人抢答器代码哪儿有问题?仿真程序怎么写?
  • ¥15 burpsuite密码爆破
  • ¥15 关于#ubuntu#的问题,如何解决?(相关搜索:移动硬盘)
  • ¥15 scikit安装之后import不了
  • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊
  • ¥15 人脸识别相关算法 YOLO,AI等
  • ¥15 spark问题方便加V17675797079
  • ¥15 Python代码不打印的原因