1

我有一个将表单发送到 PHP 脚本的页面。我想在脚本执行时以某种方式将成功信息返回到页面。现在我不能使用 jQuery AJAX 请求,因为我的表单将文件提交到服务器,并且我想尽可能避免将 get 参数附加到 url。

我试过这样做:

$_REQUEST['status'] = 'File uploaded successfully';

但是当我检查我的页面上是否设置了这个变量时,我得到了错误。

这样做的最佳做法是什么?

4

3 回答 3

2

首先,你说I would like to avoid appending get parameters to the url。如果这是您唯一关心的问题,您可以使用jQuery.post(),没有任何查询参数。

但是,如果您解释正确,它只是发布到普通 php 脚本的普通表单。不是吗? 在这种情况下,只需回显成功/失败信息:

$succes = do_something($_POST['my_posted_var']);
if($success) {
    echo 'Yeah! It worked!';
} else {
    echo 'This is disappointing...';
}

或将用户重定向到您的感谢页面。

header('Location: '. thanks.php);

编辑好的,知道了(见评论)。在这种情况下,使用会话。您可以在请求之间传输数据,而无需使用某种参数。请阅读。总结一下,您可以执行以下操作:

=execution_script.php=
session_start();
// do your stuff
$_SESSION['status'] = 'succes'; // or failure for that matter
header('Location: status_view.php');

.

=status_view.php=
session_start();
echo 'Status: ' . $_SESSION['status']; 
于 2013-09-21T10:45:26.763 回答
0

1)。您可以在首页重写 url,如“index.php?msg=$success”,通过 $_REQUEST['msg'] 检索此消息。

2)。您可以使用大多数 MVC 框架正在使用的会话闪存消息尝试在谷歌上搜索它,您将获得有关它的更多信息

于 2013-09-21T10:46:53.140 回答
0

你可以这样做......试试看

header('Location: welcome.php?status=File Uploaded Successfully..!!');.

然后使用显示成功消息

<?php if(isset($_REQUEST['status'])) echo $_REQUEST['status'];
 header('Location: welcome.php');. ?>

或者你可以简单地这样做

header('Location: welcome.php?status=1');.
<?php if(isset($_REQUEST['status']=='1')) echo 'File Uploaded Successfully..!!';
于 2013-09-21T10:49:33.203 回答