PHP重定向无法正常工作

编程入门 行业动态 更新时间:2024-10-24 10:19:35
本文介绍了PHP重定向无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经有了这个注册脚本,该脚本将信息放入mysql数据库中.现在一切正常,当有人做错了事时会说出错误(例如未定义用户名")

Ive got this register script that puts the information into a mysql database. now it all works fine and when someone does something wrong its says the error (e.g. "Username not defined")

但是当它出错时,它看起来不是很好,因为它只在空白页面上显示消息,所以我想我将使其重定向到表单页面并在此处显示消息.

but when it goes wrong it does not look very good because it just displays the message on an empty page, so i thought i would make it redirect to the form page and display the message there.

这是工作脚本

$forename = $_POST['forename']; $surname = $_POST['surname']; $email = $_POST['email']; $password = $_POST['password']; $username = $_POST['username'];

$errors = array();

if(!$username) { $errors[] = "Username is not defined"; } if(!$password) { $errors[] = "Password is not defined"; }

,然后继续.

现在我只是以为我可以做到

now i just thought i could do this

$errors = array(); if(!$username) { $errors[] = header( 'Location: localhost/muiltabledistractions/#!/page_register_error-Username-is-not-defined' ) ; } if(!$password) { $errors[] = "Password is not defined"; }

但不,它所做的就是忽略它.

but no, all it does is ignore it.

有人可以帮我吗

如果需要,请随时要求更多脚本

please feel free to ask for more of the script if you need it

非常感谢康纳

推荐答案

它看起来不是很好,因为它只是在空白页上显示消息

it does not look very good because it just displays the message on an empty page,

出什么问题了? 为什么不再次显示该表单?字段已填写. 这将是一个用户友好的界面.

What's the problem? Why not to show the form again? with fields already filled. This is going to be a user-friendly interface.

只需将您的表单包含在已填充字段的同一页面中即可. 这比重定向到空白表格更常见.

Just include your form in the same page with fields populated. That's more common way than your redirects to blank form.

这称为 POST/重定向/GET 模式,这里简短介绍的例子:

This is called POST/Redirect/GET pattern and here goes a short example of it:

代码

<? if ($_SERVER['REQUEST_METHOD']=='POST') { $err = array(); //performing all validations and raising corresponding errors if (empty($_POST['name']) $err[] = "Username field is required"; if (empty($_POST['text']) $err[] = "Comments field is required"; if (!$err) { // if no errors - saving data // and then redirect: header("Location: ".$_SERVER['PHP_SELF']); exit; } else { // all field values should be escaped according to HTML standard foreach ($_POST as $key => $val) { $form[$key] = htmlspecialchars($val); } } else { $form['name'] = $form['comments'] = ''; } include 'form.tpl.php'; ?>

模板

<? if ($err): ?> <? foreach($err as $e): ?> <div class="err"><?=$e?></div> <? endforeach ?> <? endif ?> <form> <input type="text" name="name" value="<?=$form['name']?>"> <textarea name="comments"><?=$form['comments']?></textarea> <input type="submit"> </form>

更多推荐

PHP重定向无法正常工作

本文发布于:2023-10-12 17:22:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   重定向   工作   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!