简体   繁体   English

使用ajax如何在成功后显示值而不刷新页面

[英]using ajax how to show the value after success without refreshing the page

i am adding the value to database by using ajax after adding i want to display the value in front end but now after success i am using window.location to show the data because of this the page getting refresh,i don't want to refresh the page to show the data ,anyone guide me how to do this. 我在添加后使用ajax将值添加到数据库中我希望在前端显示值但是现在成功后我使用window.location来显示数据因为这个页面得到刷新,我不想刷新显示数据的页面,任何人都指导我如何做到这一点。

below is my ajax 下面是我的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 Some 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 that you are using to post the data needs to return some meaningful data, JSON is useful for this, but it can be HTML or other formats. 您用于发布数据的代码需要返回一些有意义的数据,JSON对此很有用,但它可以是HTML或其他格式。

To return your response as JSON from PHP, you can use the json_encode() function: 要从PHP返回JSON响应,可以使用json_encode()函数:

$return_html = '<h1>Success!</h1>';
$success = "true";

json_encode("success" => $success, "html_to_show" => $return_html);

In this piece of code, you can set your dataType or JSON and return multiple values including the HTML that you want to inject into the page (DOM): 在这段代码中,您可以设置dataType或JSON并返回多个值,包括要注入页面的DOM(DOM):

$.ajax({
    type: "POST",
    url: "supplierpriceexport/insert.php",
    data: dataString,
    cache: false,

    //Set the type of data we are expecing back
    dataType: json

    success: function(return_json){

        // Check that the update was a success
        if(return_json.success == "true")
        {
            // Show HTML on the page (no reload required)
            $("#display").after(return_json.html_to_show);
        }
        else
        {
            // Failed to update
            alert("Not a success, no update made");
        }
});

You can strip out the window.location altogether, else you won't see the DOM update. 您可以完全删除window.location,否则您将看不到DOM更新。

Just try to return the values that you need from the ajax function.Something like this might do. 只是尝试从ajax函数返回所需的值。这样的事情可能会这样。

In your insert.php 在你的insert.php

echo or return the data at the end of the function that needs to be populated into the page 在需要填充到页面中的function末尾echo or return data

$.ajax({
    type: "POST",
    url: "supplierpriceexport/insert.php",
    data: dataString,
    cache: false,
    success: function(data){
             //Now you have obtained the data that was was returned from the function
             //if u wish to insert the value into an input field try
           $('#input_field').val(data); //now the data is pupolated in the input field 

     }
    });

Don't use window.location = "?action=suppliertargetpiceexport"; 不要使用window.location = "?action=suppliertargetpiceexport";

This will redirect to the page suppliertargetpiceexport 这将重定向到页面suppliertargetpiceexport

$.ajax({
    type: "POST",
    url: "supplierpriceexport/insert.php",
    data: dataString,
    cache: false,
    success: function(html){
        $('#your_success_element_id').html(html); // your_success_element_id is your element id where the html to be populated
        $("#flash").hide();
    }
});

your_success_element_id is your element id where the html to be populated your_success_element_id是要填充html的元素id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ajax成功后动态更新/刷新html,而无需重新加载页面 - updating/refreshing in html dynamically after ajax success without page reload 使用ajax如何在不刷新页面的情况下显示特定td的值 - using ajax how to show the values in particular td without refreshing the page 成功后如何使用ajax在td中显示值和图像 - how to show the value and image in td using ajax after success 使用PHP AJAX显示成功消息而不显示页面刷新后,再次显示Form div - Show Form div again after displaying success message without Page Refresh Using PHP AJAX jQuery FullCalendar如何在没有刷新页面的情况下调用ajax后显示数据 - jQuery FullCalendar how to show data after ajax call without refreshing page 显示数据库记录而无需使用ajax刷新页面 - database records show without refreshing the page using ajax Ajax成功完成的更改未刷新就无法加载到页面中 - Changes done in ajax success not loading in page without refreshing 在AJAX内成功刷新DATATABLE而不刷新页面 - Reload DATATABLE inside AJAX success without refreshing page ajax成功后如何在不重新加载页面的情况下更改url - How to change url after success in ajax without page reload 如何在不刷新页面的情况下使用jquery ajax请求通过浏览器url传递参数值 - how to pass parameter value through browser url using jquery ajax request without refreshing page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM