-3

I did my reading on about using the ternary operator for setting initial default value for undefined variables. So thats what i tried to do at the top of my scripts as you will see.

Yet i still get the undefined variable errors on my contact form's input value's fields.

Can anyone explain why?

My code:

<?php 

  if (isset($_POST["submit"])) {

    $user_name = isset($_POST["name"]) ? $_POST["name"] : "";
    $user_email = isset($_POST["email"]) ? $_POST["email"] : "";
    $user_age = isset($_POST["age"]) ? $_POST["age"] : "";

    $validation_errors = "";
    $error_counter = null;


    if (empty($_POST["name"])) {
      $validation_errors .= "<li class='validation_errors'>Please input your name.</li>";
      $error_counter++;
    }

    if (empty($user_email)) {
      $validation_errors .= "<li class='validation_errors'>Please input your email.</li>";
      $error_counter++;
    }

    if (empty($user_age)) {
      $validation_errors .= "<li class='validation_errors'>Please input your age.</li>";
      $error_counter++;
    } elseif (intval($user_age) < 18) {
      $validation_errors .= "<li class='validation_errors'>You must be over 18 years old.</li>";
      $error_counter++;
    }


    if ($error_counter != 0) {

      // $validation_errors = "<li class='validation_success'>Message successfully sent!</li>";
      $error_design = "<hr> <p id='error_msg'>Please Fix Below Errors: <p>";

    };

  }

?>


<!DOCTYPE html>
<html>
<head>

  <meta charset="utf-8">

  <title>SandBox</title>

  <link rel="stylesheet" type="text/css" href="css/style.css">
  <link rel="stylesheet" href="css/animate.css">

  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/script.js"></script>
</head>
<body>

  <header>

  </header>

  <form action="" method="post">

    <div class="input_group">
      <label for="name">Name</label>
      <input type="text" name="name" value="<?php echo htmlspecialchars($user_name); ?>">
    </div>

    <div class="input_group">
      <label for="email">eMail</label>
      <input type="email" name="email" value="<?php echo htmlspecialchars($user_email); ?>">
    </div>

    <div class="input_group">
      <label for="age">Age</label>
      <input type="text" name="age" value="<?php echo htmlspecialchars($user_age); ?>">
    </div>

    <input type="submit" value="Send" name="submit" id="submit">

    <?php echo $error_design; ?>

    <div>

      <ul>

        <?php echo $validation_errors; ?>

      </ul>

    </div>

  </form>


  <footer></footer>

</body>
</html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Eli Himself
  • 834
  • 3
  • 8
  • 20
  • Could you edit your question to include the exact text of the error messages that you're getting, and indicate which lines of code are triggering the errors? – Kenster Sep 13 '15 at 13:59
  • all you need to do is just keep the variables blank at the start of php code. make ur user name, user email and user age empty at the start of php code – Gautam Sep 13 '15 at 14:11
  • Okay... But there will be some errors too... [link](http://www.webyzz.hu/test.php) ;) – csklrnd Sep 13 '15 at 14:17
  • @ l0rkaY what other errors? just make the variables blank at the beginning because they are not getting set when the page loads – Gautam Sep 13 '15 at 14:26
  • @Gautam You are right, if i just define all my variables before i checked if submit was pressed that totally worked. All error notices are gone now. – Eli Himself Sep 13 '15 at 14:45

1 Answers1

0

Put these variables before the IF statement:

$user_name = isset($_POST["name"]) ? $_POST["name"] : "";
$user_email = isset($_POST["email"]) ? $_POST["email"] : "";
$user_age = isset($_POST["age"]) ? $_POST["age"] : "";

Or just use:

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
csklrnd
  • 184
  • 8
  • Your answer is incorrect; it has nothing to do with that. Look at the OP's code again very carefully, including what they wrote: *"Yet i still get the undefined variable errors on my contact form's **input value's fields.**"* - The solution is a very simple one, but it isn't what you have provided. – Funk Forty Niner Sep 13 '15 at 14:05
  • I edited my answer 8 mins ago... Read again, I sent the solution ;) – csklrnd Sep 13 '15 at 14:07
  • *"Or just use: // Report all errors except E_NOTICE error_reporting(E_ALL & ~E_NOTICE);"* - I believe they are using error reporting, or their system is already setup to catch and display notices. Otherwise, the OP wouldn't have posted that they're getting undefined variable notices. – Funk Forty Niner Sep 13 '15 at 14:09
  • No.. :D And put before the IF these variables: – csklrnd Sep 13 '15 at 14:09
  • and.... how should it be fixed then? You need to edit your answer in order to show the OP what the "solution" is. – Funk Forty Niner Sep 13 '15 at 14:10
  • Then... Yet i still get the undefined variable errors on my contact form's input value's fields. What is this? :) What is undefined variable ERRORS? :) Maybe notice – csklrnd Sep 13 '15 at 14:10
  • *"I'm sorry, I do not have time for this... I send several possible solutions.. I think if he can't use it, he should go to learn programming"* - That isn't a good attitude at all and you're helping no one. If anyone who should learn programming, that would be you, since "you" are the one providing the answer to a problem "they" are having. Edit: You deleted that comment. – Funk Forty Niner Sep 13 '15 at 14:28