简体   繁体   English

HTML表单PHP发布到自我以验证或提交到新页面

[英]HTML form PHP post to self to validate or submit to new page

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. 今天是我使用php的第一天,我终于找到了如何让我的页面回复自己(我的页面为.html,而不是.php),但现在我无法弄清楚如何在验证表单后将数据带到新页面。 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? 现在,考虑到所有事情都是有效的,我如何发布到另一个脚本(action =“otherAction.php”,也许?),以便实际处理数据?

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') 满足所有条件后,您可以使用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. 当您在提交表单后刷新页面时,POST值仍然存在并且可能会复制条目。 Which is not good IMO. 哪个IMO不好。

Use javascript to validate, then send the post form to itself or to another page where it can do stuff with the data. 使用javascript进行验证,然后将帖子表单发送给自己或发送到其他可以处理数据的页面。

<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>
</head>

<body>
<form name="myForm" action="demo_form.php" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>

</html>

source : http://www.w3schools.com/js/js_form_validation.asp 来源: http//www.w3schools.com/js/js_form_validation.asp

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

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

// Control variables
$app_state = "empty";  //empty, processed, logged in
$valid = 0;

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

   if ($valid >= 2) {
      $app_state = "processed";
   }
}

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

if ($app_state == "empty") {
?>
<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>
<?php
}
elseif ($app_state == "processed") {
    if ($firstName == "Vincent") {
        $app_state = "Logged in";
    }
}

if ($app_state == "Logged in") {
    echo("Logged in<br> Hello Vincent</body></html>");
}
?>

You can check if there is any invalid data, if there is no invalid data, process, then redirect. 您可以检查是否存在任何无效数据,如果没有无效数据,则处理,然后重定向。 For example 例如

 if (empty($firstNameErr) && empty($lastNameErr)) {
       // process the data

       // redirect to other page.
       header('LOCATION: index.php');
       exit();
 }

Noted that you need to do those code before you output any HTML, or else you cannot do the redirection. 注意到在输出任何HTML之前需要执行这些代码,否则您无法执行重定向。 For example: 例如:

<?php
      // validate the data
      // no error, proccess, then, redirect
?>
html code here.

Relying on redirection like that is bad for SEO if you are about that stuff. 如果你是关于那些东西,依赖这样的重定向对SEO是坏的。 It would be better for SEO for your form to post to another page naturally. 搜索引擎优化可以让您的表单自然地发布到另一个页面。

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

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