[英]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+'¤cy='+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.