0

我在用 javascript(和 jquery)和 php 编写的应用程序中有一个 html 表。表的内容存储在 MySql 表中。

当我希望用户向表中添加一些数据时,他们单击一个按钮并显示一个 jquery ui 对话框,其中包含一个供用户填写的表单。

当用户填写表单时,点击保存,表单被提交到一个纯php的页面,将数据保存到表格中,然后重定向到原来的页面打开表格,使用

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

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

exit();     

我这样做是因为我不想要这样的信息:

确认重新提交表单 - 您正在查找的页面使用了您输入的信息。返回页面可能会导致您重复执行的任何操作。你想继续吗?

显示用户是否出于任何原因点击刷新。

但是,由于我这样做的方式,如果保存不成功,我正在努力提供一种向用户提供反馈的方法。

当用户在 jquery ui 对话框中点击保存时,提交表单时该框已关闭,因此我无法在那里提供反馈,因为尚未发生错误。

当我们在 php 页面中时,一旦我们重定向回原始页面并打开表格,任何拾取的错误都会丢失。

任何人都可以提供任何建议吗?

4

4 回答 4

2

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

于 2013-04-25T17:19:40.053 回答
0

一个简单的 GET 参数怎么样?

类似的东西:

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

在那个页面上$url,您首先要查找 的值$_GET['success']。如果不是"success",则回显附加的 HTML,说明它不成功。

if ($_GET['success'] == "true") {
    echo "<p>SUCCESS!</p>";
    ..
} else {
    echo "<p>FAILED!</p>";
    ..
}
于 2013-04-25T10:41:45.213 回答
0

考虑通过 AJAX 提交表单以避免页面刷新。

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

解决方案(未测试)

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代码:

//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(); 
于 2013-04-25T10:41:46.673 回答
0

另一个想法是通过 url 中的哈希传输消息:

if (isset($_POST['submit'])) {
    ...
    header("Location: ".$_SERVER["REQUEST_URI"]."#".urlencode($msg));
    ...
}
于 2013-04-25T10:51:06.090 回答