0

I was trying to validate data before submitting them, but the page always prompts error said,"undefined index: first_name." here is the file:

<?php include '../view/header.php'; ?>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
 $name=$_POST["first_name"];

if (strlen($name)<1)
{$_SESSION['firstName']= "Name is too short.";}};?>

<main>

<!-- display a table of customer information -->
<h2>View/Update Customer</h2>
<form action="" method="POST" id="aligned">
    <input type="hidden" name="action" value="update_customer">
    <input type="hidden" name="customer_id" 
           value="<?php echo htmlspecialchars($customer['customerID']); ?>">

    <label>First Name:</label>
    <input type="text" name="first_name" 
           value="<?php echo htmlspecialchars($customer['firstName']); ?>">
    <span><?php //echo $error;?></span><br>

And data is always passed to be stored in database(mysql) via file 2 no matter it is valid or not:

  <?php
  require('../model/database.php');
  require('../model/customer_db.php');

  $action = filter_input(INPUT_POST, 'action');
  if ($action === NULL) {
    $action = filter_input(INPUT_GET, 'action');
      if ($action === NULL) {
    $action = 'search_customers';
     }
   }

   //instantiate variable(s)
  $last_name = '';
  $customers = array();

  if ($action == 'search_customers') {   
      include('customer_search.php');
  } else if ($action == 'display_customers') {
      $last_name = filter_input(INPUT_POST, 'last_name');
    if (empty($last_name)) {
    $message = 'You must enter a last name.';
   } else {
    $customers = get_customers_by_last_name($last_name);
   }
    include('customer_search.php');
   } else if ($action == 'display_customer') {
        $customer_id = filter_input(INPUT_POST, 'customer_id', FILTER_VALIDATE_INT);
   $customer = get_customer($customer_id);

     include('customer_display.php');
   } else if ($action == 'update_customer') {
     $customer_id = filter_input(INPUT_POST, 'customer_id', FILTER_VALIDATE_INT);
    $first_name = filter_input(INPUT_POST, 'first_name');


$last_name = filter_input(INPUT_POST, 'last_name');
$address = filter_input(INPUT_POST, 'address');
$city = filter_input(INPUT_POST, 'city');
$state = filter_input(INPUT_POST, 'state');
$postal_code = filter_input(INPUT_POST, 'postal_code');
$country_name = $_POST["selected"];
$phone = filter_input(INPUT_POST, 'phone');
$email = filter_input(INPUT_POST, 'email');
$password = filter_input(INPUT_POST, 'password');

$country_code = get_countryCode($country_name); 

update_customer($customer_id, $first_name, $last_name,
        $address, $city, $state, $postal_code, $country_code,
        $phone, $email, $password);

include('customer_search.php');
}
?>

I don't know how to make the form validate the data first, if not pass the validation stay in the same page, if passed then transfer the data in file2 to store them. Any suggestions? Thanks.

Jasmine
  • 17
  • 3
  • You may need to verify if your form actually sends the data to the backend. Verify it by using `echo file_get_contents("php://input")` just before the line you see the error. If it's empty or some fields are missing, it's likely to be same with this issue - http://stackoverflow.com/questions/5077969/php-some-post-values-missing-but-are-present-in-php-input – Chainat Nov 28 '16 at 00:41
  • I think you may need to use `if(isset($_POST['first_name'])){$name = $_POST['first_name'];}` and instantiate `$name` as in `$name = '';` as you did for $last_name;` in the second code block. That should stop the notice. You could also use `if(!empty($_POST['first_name']))...` – Steve Nov 28 '16 at 01:12
  • I tried "echo_file_get_contents("php://input"), it got:"action=display_customer&customer_id=1004". I dont know why it does not store or send the other data, like first_name, to the backend. – Jasmine Nov 28 '16 at 01:14
  • if(isset($_POST['first_name'])){$name = $_POST['first_name'];} did not work, the file always skip this part. – Jasmine Nov 28 '16 at 01:33

0 Answers0