简体   繁体   English

在Php标头重定向后提供反馈

[英]Providing Feedback After a Php Header Redirect

I have a html table in an application written with javascript (and jquery) and php. 我在用javascript(和jquery)和php编写的应用程序中有一个html表。 The contents of the table are stored in a MySql table. 表的内容存储在MySql表中。

When I want the user to add some data to the table, they click a button and a jquery ui dialog is shown, with a form for the user to fill out. 当我希望用户向表中添加一些数据时,他们单击一个按钮并显示一个jquery ui对话框,其中包含一个供用户填写的表单。

When the user fills out the form, they click save, and the form is submitted to a page of pure php, which saves the data to the table and then redirects to the original page with the table on, using 当用户填写表单时,他们单击“保存”,表单将提交到纯php页面,该页面将数据保存到表中,然后重定向到打开表格的原始页面,使用

$url = BASE_URL . '/admin/pages/finance/pricing/pricing_schedule.php';

header('Location: '.$url);      //Redirect to the right page

exit();     

I am doing this because I don't want the message: 我这样做是因为我不想要这样的信息:

Confirm Form Resubmission - The page that you're looking for used information that you entered. 确认表单重新提交 - 您要查找的页面使用了您输入的信息。 Returning to the page might cause any action you took to be repeated. 返回页面可能会导致您重复执行任何操作。 Do you want to continue? 你想继续吗?

to show should the user hit refresh for whatever reason. 显示用户是否应该因任何原因点击刷新。

However, because of the way I am doing this, I am struggling to provide a way to give feedback to the user should the save be unsuccessful. 但是,由于我这样做的方式,如果保存不成功,我很难提供一种向用户提供反馈的方法。

When the user hits save in the jquery ui dialog box, the box is close when the form is submitted, so I can't provide feedback there because the error hasn't occurred yet. 当用户在jquery ui对话框中点击保存时,在提交表单时该框关闭,所以我无法在那里提供反馈,因为还没有发生错误。

When we are in the php page, as soon as we redirect back to the original page with the table on, any errors picked up are lost. 当我们进入php页面时,只要我们重新定向到打开表格的原始页面,就会丢失所拾取的任何错误。

Can anyone offer any suggestions? 有人可以提供任何建议吗?

您可以在$_SESSION变量中存储任何错误,并在重定向的页面上打印出来。

How about a simple GET parameter? 简单的GET参数怎么样?

Something on the lines of: 有点像:

header('Location: ' . $url . '?success=false&reason=blah');

And on that page at $url , you would first look for the value of $_GET['success'] . $url页面上,您首先要查找$_GET['success'] If it is not "success" , echo out additional HTML saying its not successful. 如果它不是"success" ,请回显其他HTML,说它不成功。

if ($_GET['success'] == "true") {
    echo "<p>SUCCESS!</p>";
    ..
} else {
    echo "<p>FAILED!</p>";
    ..
}

Consider submitting your form through AJAX to avoid the page refresh. 考虑通过AJAX提交表单以避免页面刷新。

Check out this tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/ 查看本教程: http//net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

Solution(Not Tested) 解决方案(未经测试)

jQuery Code: jQuery代码:

var form_data = $('#save_period_form').serialize(); alert(form_data);
$.ajax({
type: "POST",
url: "period_submit.php",
data: form_data,
dataType: 'html',
success: function(returninfo) {
  if(returninfo=='1'){
     alert('finish');
     //will redirect to pricing schedule page after the user has pressed 'ok' on the alert popup.
     window.location.assign('/admin/pages/finance/pricing/pricing_schedule.php');
 } else {
    alert('error occured');
 }
} 
});

PHP Code: PHP代码:

//Commented header redirection code because it's being done in the javascript code now.
//$url = BASE_URL . '/admin/pages/finance/pricing/pricing_schedule.php';

//header('Location: '.$url);      //Redirect to the right page

//Assuming your save has been successful, we echo value '1'. Else echo something else..
echo '1';
exit(); 

another idea is the transport of the message via the hash in the url: 另一个想法是通过url中的哈希传输消息:

if (isset($_POST['submit'])) {
    ...
    header("Location: ".$_SERVER["REQUEST_URI"]."#".urlencode($msg));
    ...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM