SpaghettiCode SpaghettiCode - 6 months ago 38
PHP Question

undefined index and undefined variable

Been having an issue with PHP, I'm building a form with valdiation but I keep getting this error:


The specified value "\u003Cbr /\u003E\n\u003Cb\u003ENotice\u003C/b\u003E: Undefined index: email in \u003Cb\u003EC:\xampp\htdocs\navigation.php\u003C/b\u003E on line \u003Cb\u003E120\u003C/b\u003E\u003Cbr /\u003E\n" is not a valid email address.


More error information:

<form class="form-horizontal" role="form" method="post" action="navigation.tphp">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<br />
<b>Notice</b>: Undefined index: name in <b>C:\xampp\htdocs\navigation.php</b> on line <b>113</b><br />
">
<br />
<b>Notice</b>: Undefined variable: errName in <b>C:\xampp\htdocs\navigation.php</b> on line <b>114</b><br />
<p class='text-danger'></p> </div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<br />
<b>Notice</b>: Undefined index: email in <b>C:\xampp\htdocs\navigation.php</b> on line <b>120</b><br />
">
<br />
<b>Notice</b>: Undefined variable: errEmail in <b>C:\xampp\htdocs\navigation.php</b> on line <b>121</b><br />
<p class='text-danger'></p> </div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><br />
<b>Notice</b>: Undefined index: message in <b>C:\xampp\htdocs\navigation.php</b> on line <b>127</b><br />
</textarea>
<br />
<b>Notice</b>: Undefined variable: errMessage in <b>C:\xampp\htdocs\navigation.php</b> on line <b>128</b><br />
<p class='text-danger'></p> </div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer" value="<br />
<b>Notice</b>: Undefined index: human in <b>C:\xampp\htdocs\navigation.php</b> on line <b>134</b><br />
">
<br />
<b>Notice</b>: Undefined variable: errHuman in <b>C:\xampp\htdocs\navigation.php</b> on line <b>135</b><br />
<p class='text-danger'></p> </div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- Will be used to display an alert to the user -->
<br />
<b>Notice</b>: Undefined variable: result in <b>C:\xampp\htdocs\navigation.php</b> on line <b>141</b><br />
</div>
</div>
</form>


The code it's referring too:

<form class="form-horizontal" role="form" method="post" action="navigation.tphp">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']); ?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>"; ?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer" value="<?php echo htmlspecialchars($_POST['human']); ?>">
<?php echo "<p class='text-danger'>$errHuman</p>"; ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- Will be used to display an alert to the user -->
<?php echo $result; ?>
</div>
</div>
</form>


The validation code:

if(!empty($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 'schlitzws@gmail.com';
$subject = 'New Contact Message';

$body = "From: $name\n E-mail: $email\n Message:\n $message";


//Check if name has been entered
if(!$_POST['name']) {
$errName = 'This Form requires a Name';
}

//Check if email has been entered and is valid
if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'This Form requires a valid Email';
}


//Check if message has been entered
if(!$_POST['message']) {
$errMessage = 'This Form requires a Message';
}
//Check if simple anti-spam is incorrect
if($human !== 5) {
$errHuman = 'Your Answer is in-correct';
}

//If there are no errors, send the email
if(!$errName && !$errEmail && !$errMessage && !$errHuman) {
if(mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}


Also the code is being included if that has something to do with it thank you in advance!

Answer

The problem is that you are trying to access a value in the POST super-global which doesn't exist. To fix this you will need to run an isset() check to see if the value exists.

eg.

if (isset($_POST["answer"])) { echo $_POST["answer"] }