javascript - display text using ajax and php -


i'm trying simple thing in ajax not work. want display text when click on 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 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"; } 

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 pas de probleme           success:function(data){             $('.coucou').empty();             $('.coucou').text(data);           }         });     }); });  

you need use .click() , not .change() , also, in success, receive string not object need write data directly (with .text())

copy code because have add ; had forgotten. (it's work, tested)

another point use .val() on #insertform there no value attribute in html. var req empty string


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -