0

I'm working on a validation script right now.

<?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>

However, I'm getting this :

Notice: Undefined index: process in on line 2

It's a fairly obvious error, but what should I throw in to stop this line of code running thus preventing the error?

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
user2646567
  • 43
  • 10

3 Answers3

0
if (isset($_POST['process']) && $_POST['process'] == 1) {   
Danny
  • 1,185
  • 2
  • 12
  • 33
0

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

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

0

use isset or array_key_exists before you use the variable

exussum
  • 18,275
  • 8
  • 32
  • 65