javascript - using ajax how to show the value after success without refreshing the page -


i adding value database using ajax after adding want display value in front end after success using window.location show data because of page getting refresh,i don't want refresh page show data ,anyone guide me how this.

below ajax

$(function() {     $(".supplierpriceexport_button").click(function() {      var pricefrom = $("#pricefrom").val();     var priceto =  $("#priceto").val();     var tpm =  $("#tpm").val();     var currency =  $("#currency").val();       var datastring = 'pricefrom='+ pricefrom +'&priceto='+priceto+'&tpm='+tpm+'&currency='+currency;       if(pricefrom=='')     {     alert("please enter text");     }     else     {     $("#flash").show();     $("#flash").fadein(400).html;      $.ajax({     type: "post",     url: "supplierpriceexport/insert.php",     data: datastring,     cache: false,     success: function(html){     $("#display").after(html);      window.location = "?action=suppliertargetpiceexport";     $("#flash").hide();     }     });     } return false;     });     }); 

the code using post data needs return meaningful data, json useful this, can html or other formats.

to return response json php, can use json_encode() function:

$return_html = '<h1>success!</h1>'; $success = "true";  json_encode("success" => $success, "html_to_show" => $return_html); 

in piece of code, can set datatype or json , return multiple values including html want inject page (dom):

$.ajax({     type: "post",     url: "supplierpriceexport/insert.php",     data: datastring,     cache: false,      //set type of data expecing     datatype: json      success: function(return_json){          // check update success         if(return_json.success == "true")         {             // show html on page (no reload required)             $("#display").after(return_json.html_to_show);         }         else         {             // failed update             alert("not success, no update made");         } }); 

you can strip out window.location altogether, else won't see dom update.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -