1

i know this topic has been touched on so much, but a solution is still evading me. I keep getting an unidentified index error in this php file:

<?php
  $name = isset($_POST['name']) ? $_POST['name'] : '';
  $telephone = isset($_POST['telephone']) ? $_POST['telephone'] : '';
  $email = isset($_POST['email']) ? $_POST['email'] : '';
  $birthDate = isset($_POST["birthDate"]) ? $_POST['birthDate'] : '';
  $gender = isset($_POST['gender']) ? $_POST['email'] : '';
  $comments = isset($_POST['comments']) ? $_POST['comments'] : '';
?>

Thanks for submitting your application!<br>
The following is the information we received:<br>
Name: <?php echo $_POST['name'] ?><br>
Telephone: <?php echo $_POST['telephone']?><br>
E-Mail: <?php echo $_POST['email']?><br>
Birthday: <?php echo $_POST['birthDate '] ?><br>
Gender: <?php echo $_POST['gender'] ?><br>
When you first wanted to be a zookeeper: <?php echo $_POST['comments '] 
?>

</body>
</html>

here's the html form that this php file is getting it's values from:

<form id="zooKeeperForm" action="zoo.php" method="GET" onsubmit="return validateForm()">
  <p><i>Please complete the form. Mandatory fields are marked with a </i><em>*</em></p>
  <fieldset>
    <legend>Contact Details</legend>
      <label for="name">Name <em>*</em></label>
      <input id="name" placeholder="Jane Smith" autofocus required><br>
      <label for="telephone">Telephone</label>
      <input id="telephone" placeholder="(xxx) xxx-xxxx"><br>
      <label for="email">Email <em>*</em></label>
      <input id="email" type="email" required><br>
  </fieldset>
  <fieldset>
    <legend>Personal Information</legend>

      <label for="birthDate">Birth Date<em>*</em></label>
      <input id="birthDate" type="date" required><br>

      <label for="age">Age<em>*</em></label>
      <input id="age" type="number" min="0" max="120" step="0.1" required><br>
      <label for="gender">Gender</label>
      <select id="gender">
        <option value="female">Female</option>
        <option value="male">Male</option>
      </select><br>
      <label for="comments">When did you first know you wanted to be a zoo-keeper?<em>*</em></label>
      <textarea id="comments" oninput="validateComments(this)" required></textarea>
  </fieldset>
  <p><input type="submit" value="Submit Application"></p>
</form>

what am I doing wrong here?? I feel like an idiot. I keep going from getting the unidentified index error to where it prints out the format I want but it's just blank for all the values that should be getting plugged in.

1 Answer 1

2

First thing:

Change it to POST

<form id="zooKeeperForm" action="zoo.php" method="POST" onsubmit="return validateForm()">
                                             //  ^ POST not GET

Second. Name attributes is the one to be used not ids

<input id="name" placeholder="Jane Smith" autofocus required>
      // NOT ID but name="name"

This must be:

<input name="telephone" placeholder="(xxx) xxx-xxxx" id="telephone" />
<input name="email" type="email" required id="email" />
<input name="birthDate" type="date" required id="birthDate" />
<select name="gender" id="gender">
<textarea name="comments" oninput="validateComments(this)" id="comments" required>

Third:

The simple thing here is that, always process form input the form is submitted. Not upon initial load.

Catch the submission with something like this:

<input type="submit" name="zoo_submit" value="Submit Application" />

Then in PHP:

// simple initialization
$name = $telephone = $email = $birthDate = $gender = $comments = '';

if(isset($_POST['zoo_submit'])) {

  $name = isset($_POST['name']) ? $_POST['name'] : '';
  $telephone = isset($_POST['telephone']) ? $_POST['telephone'] : '';
  $email = isset($_POST['email']) ? $_POST['email'] : '';
  $birthDate = isset($_POST["birthDate"]) ? $_POST['birthDate'] : '';
  $gender = isset($_POST['gender']) ? $_POST['email'] : '';
  $comments = isset($_POST['comments']) ? $_POST['comments'] : '';

}
?>

Thanks for submitting your application!<br>
The following is the information we received:<br>

<!-- now you have already set the variables above, use them, not the POST values again -->
Name: <?php echo $name; ?><br>
Telephone: <?php echo $telephone; ?><br>
E-Mail: <?php echo $email; ?><br>
Birthday: <?php echo $birthDate; ?><br>
Gender: <?php echo $gender; ?><br>
When you first wanted to be a zookeeper: <?php echo $comments; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

boom! that edit was the trick. it's based on name instead of id. I swear, that needs to be posted on literally hundreds of php threads across the internet. thank you, sir!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.