0

我现在正在编写验证脚本。

<?php
  if ($_POST['process'] == 1) {    
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    if (empty($first_name) && empty($last_name)){
      echo "Howdy, stranger";
    }else{
      echo "Hello there, ".$first_name." ".$last_name;
    }
  }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Form validation example</title>
  <style>
    input{display:block; margin-bottom:10px;}
  </style>
</head>

<body>
  <h1>Form Validation Example</h1>
  <h2>Please enter your name</h2>

  <form action="validation_example.php" method="post">
    <label for="first_name">First name:</label>
    <input name="first_name" id="first_name" type="text" />

    <label for="last_name">Last name:</label>
    <input name="last_name" id="last_name" type="text" />

    <input type="hidden" name="process" value="1" />
    <input type="submit" value="Submit" />
  </form>
</body>
</html>

但是,我得到了这个:

Notice: Undefined index: process in on line 2

这是一个相当明显的错误,但我应该投入什么来阻止这行代码运行从而防止错误?

4

3 回答 3

0
if (isset($_POST['process']) && $_POST['process'] == 1) {   
于 2013-08-02T20:27:43.600 回答
0

if(array_key_exists('process', $_POST) && $_POST['process'] == 1)

http://php.net/manual/es/function.array-key-exists.php

于 2013-08-02T20:31:48.510 回答
0

使用issetarray_key_exists 在使用变量之前

于 2013-08-02T20:33:35.200 回答