HTML表单PHP发布自我验证或提交到新页面 [英] HTML form PHP post to self to validate or submit to new page

查看:119
本文介绍了HTML表单PHP发布自我验证或提交到新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向前道歉。今天是我使用php的第一天,我终于想出了如何让我的页面回到自己(我的页面是.html,而不是.php),但现在我很难弄清楚在表单验证完成后将数据提交到新页面。我一直在努力工作了很长一段时间,而且我很油炸。这里有一个简单的例子:

 <!DOCTYPE HTML> 
< html>
< head>
< style>
.error {color:#FF0000;}
< / style>
< / head>
< body>

<?php
//初始化变量并设置为空字符串
$ firstName = $ lastName =;
$ firstNameErr = $ lastNameErr =;

//验证输入和消毒
如果($ _SERVER [ 'REQUEST_METHOD'] == POST){
如果(空($ _ POST [ 名字]) ){
$ firstNameErr =名字是必需的;
}
else {
$ firstName = test_input($ _ POST [firstName]);

if(empty($ _ POST [lastName])){
$ lastNameErr =需要姓氏;
}
else {
$ lastName = test_input($ _ POST [lastName]);



//净化数据
函数test_input($ data){
$ data = trim($ data);
$ data = stripslashes($ data);
$ data = htmlspecialchars($ data);
返回$ data;
}
?>

< h2>查找客户< / h2>
< p>< span class =error> *必填< / span>< / p>
< form action =<?php echo htmlspecialchars($ _ SERVER ['PHP_SELF']);?>方法= POST >
名字:其中;输入类型= 文本 名称= 名字 值= ?< PHP的回声$的firstName;> 中><跨度类= 错误 > * LT; ?php echo $ firstNameErr; ?>< /跨度><峰; br><峰; br>
姓:其中;输入类型= 文本 名称= 姓氏 值=><跨度类= 错误 > * LT; < PHP的回声$ lastName的;;>? ?php echo $ lastNameErr; ?><峰; br><峰; br>
< input type =submit>
< / form>

< / body>
< / html>

好的。所以上面,你会看到表单发回自己,所以它可以验证。好。现在,考虑到所有的事情都是有效的,我怎么发布到另一个脚本(action =otherAction.php,也许?),以便实际处理数据?

此外,任何安全建议表示赞赏。我尽最大努力考虑到安全问题。当你满足所有条件时,你可以使用头('Location:http:mywebsite .com / otherAction.php')

  //验证输入并清理
if ($ _SERVER ['REQUEST_METHOD'] ==POST){
$ valid = true; //您的状况指标,实际上取决于您的需求。我只习惯这种方法。

if(empty($ _ POST [firstName])){
$ firstNameErr =名字是必需的;
$ valid = false; // false
}
else {
$ firstName = test_input($ _ POST [firstName]);

if(empty($ _ POST [lastName])){
$ lastNameErr =需要姓氏;
$ valid = false;
}
else {
$ lastName = test_input($ _ POST [lastName]);
}

// if if valid then redirect
if($ valid){
header('Location:http://mywebsite.com/otherAction.php' );
exit();




$ b $ p $在我的一些作品中,我的设置就像这样但我在这里学到了不好的东西。当您在提交表单后刷新页面时,POST值仍然存在并可能用于重复条目。哪个不好IMO。


Upfront apology. Today is my first day working with php and I finally figured out how to get my page to post back to itself (I'd had the page as .html, instead of .php), but now I'm having trouble figuring out how to take the data to a new page after the form has been validated. I've been working on it for quite a while and I'm fried. Here's a simple example:

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// Initialize variables and set to empty strings
$firstName=$lastName="";
$firstNameErr=$lastNameErr="";

// Validate input and sanitize
if ($_SERVER['REQUEST_METHOD']== "POST") {
   if (empty($_POST["firstName"])) {
      $firstNameErr = "First name is required";
   }
   else {
      $firstName = test_input($_POST["firstName"]);
   }
   if (empty($_POST["lastName"])) {
      $lastNameErr = "Last name is required";
   }
   else {
      $lastName = test_input($_POST["lastName"]);
   }
}

// Sanitize data
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<h2>Find Customer</h2>
<p><span class="error">* required</span></p>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
First Name: <input type="text" name="firstName" value="<?php echo $firstName; ?>"><span class="error">* <?php echo $firstNameErr; ?></span><br><br>
Last Name: <input type="text" name="lastName" value="<?php echo $lastName; ?>"><span class="error">* <?php echo $lastNameErr; ?><br><br>
<input type="submit">
</form>

</body>
</html>

OK. So above, you'll see that the form posts back to itself so it can validate. Good. Now, considering all things are valid, how do I post to another script (action="otherAction.php", maybe?) so the data can actually be processed?

Also, any security suggestions are appreciated. I did my best to take security into account. Thanks.

解决方案

When all your conditions are met you can use header('Location: http:mywebsite.com/otherAction.php')

// Validate input and sanitize
if ($_SERVER['REQUEST_METHOD']== "POST") {
   $valid = true; //Your indicator for your condition, actually it depends on what you need. I am just used to this method.

   if (empty($_POST["firstName"])) {
      $firstNameErr = "First name is required";
      $valid = false; //false
   }
   else {
      $firstName = test_input($_POST["firstName"]);
   }
   if (empty($_POST["lastName"])) {
      $lastNameErr = "Last name is required";
      $valid = false;
   }
   else {
      $lastName = test_input($_POST["lastName"]);
   }

  //if valid then redirect
  if($valid){
   header('Location: http://mywebsite.com/otherAction.php');
   exit();
  }
}

In some of my works, my setup is like this but I learned something not good here. That's when you refresh the page after submitting the form , POST values still remains and possible for duplicating entries. Which is not good IMO.

这篇关于HTML表单PHP发布自我验证或提交到新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆