I was trying to add some snippets of code I have used to validate forms on the server-side using php. I know the code works and had used it in the exact same scenario for a contact page identical to the one I am adding it to now.
Here is my code I am having problems with:
<? //contact form validation $reqField = array("fName" => "Your First Name", "lName" => "Your Last Name", "email" => "Email Address", "message" => "Message"); foreach($reqField as $field => $label){ if (!$_POST[$field]){ $warnings[$field] = "«Required"; } } if ($_POST['fName'] && !ereg("^[[:alpha:]]{2,20}$", $_POST['fName'])){ $warnings['fName'] = "«Must be 2-20 letters no numbers or special characters."; } if ($_POST['lName'] && !ereg("^[[:alpha:]]{2,25}$", $_POST['lName'])){ $warnings['lName'] = "«Must be 2-25 letters no numbers or special characters."; } if ($_POST['email'] && !ereg("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$", $_POST['email'])){ $warnings['email'] = "«Invalid Email format. Ex: [email protected]"; } if ($_POST['phone'] && !ereg("^\([[:digit:]]{3}\)[[:digit:]]{3}-[[:digit:]]{4}$", $_POST['phone'])){ $warnings['phone'] = "«Invalid Phone Number. Ex: (123)456-7890"; } if (count($warnings) > 0){ ?> <form name="sendMessage" action="contact.php" method="post">
<table border="0">
<tr>
<td align="right">First Name:</td>
<td><input name="fName" type="text" tabindex="1" value="<? echo $_POST['fName']; ?>" size="15" maxlength="15" /> <? echo $warnings['fName']; ?></td>
</tr>
<tr>
<td align="right">Last Name:</td>
<td><input name="lName" type="text" tabindex="2" value="<? echo $_POST['lName']; ?>" size="15" maxlength="20" /> <? echo $warnings['lName']; ?></td>
</tr>
<tr>
<td align="right">E-mail:</td>
<td><input name="email" type="text" tabindex="3" value="<? echo $_POST['email']; ?>" size="15" maxlength="30" /> <? echo $warnings['email']; ?></td>
</tr>
<tr>
<td align="right">Phone:</td>
<td><input name="phone" type="text" tabindex="4" value="<? echo $_POST['phone']; ?>" size="15" maxlength="13" /> <? echo $warnings['phone']; ?></td>
</tr>
<tr>
<td align="right" valign="top">Message:</td>
<td><textarea name="message" cols="55" rows="7" tabindex="5"><? echo $_POST['message']; ?></textarea><? echo $warnings['message']; ?></td>
</tr>
<tr>
<td><input type="hidden" value="<? echo date("m/d/y G:i:s") ?>" name="msgDate" /></td>
<td align="right"><input type="submit" value="Send Message" /> <input type="reset" value="Reset Form" /></td>
</tr>
</table>
</form>
<?
}else{
if ($_SERVER['REQUEST_METHOD']=="POST"){
$conn= mysqli_connect("localhost",$mysqlUname,$mysqlPW, "cavaliero");
if (!$conn)
{
die('Could not connect: ' . mysqli_connect_error());
}
$fName=$_POST['fName'];
$fName=mysqli_real_escape_string($conn, $fName);
$fName=htmlspecialchars($fName);
$lName=$_POST['lName'];
$lName=mysqli_real_escape_string($conn, $lName);
$lName=htmlspecialchars($lName);
$phone=$_POST['phone'];
$phone=mysqli_real_escape_string($conn, $phone);
$phone=htmlspecialchars($phone);
$email=$_POST['email'];
$email=mysqli_real_escape_string($conn, $email);
$message=$_POST['message'];
$message=mysqli_real_escape_string($conn, $message);
$message=htmlspecialchars($message);
$date=$_POST['msgDate'];
$date=mysqli_real_escape_string($conn, $date);
$date=strtotime($date);
$sqlDate=date("Y/m/d H:i:s", $date);
$sql="INSERT INTO portfolioMessages (fName, lName, messageDate, phone, email, message) values ('". $fName . "', '" . $lName . "', '" . $sqlDate . "', '" . $phone . "', '" . $email . "', '" . $message . "')";
if (mysqli_query($conn, $sql)){
echo "Submit Confirmation
Thank you. Your message has been recieved. A respone will be mailed to you shortly.
"; mysqli_close($conn); }else{ echo "There was an error during your message request."; echo ""; echo $sql; echo "
"; echo mysqli_error($conn); mysqli_close($conn); } } } ?>
I get the following undefined index error messages:
[font=arial]Notice: Undefined index: fName in C:\Inetpub\wwwroot\php\cavaliero\portfolio\contact.php on line 55
Notice: Undefined index: lName in C:\Inetpub\wwwroot\php\cavaliero\portfolio\contact.php on line 55
Notice: Undefined index: email in C:\Inetpub\wwwroot\php\cavaliero\portfolio\contact.php on line 55
Notice: Undefined index: message in C:\Inetpub\wwwroot\php\cavaliero\portfolio\contact.phpon line 55
Notice: Undefined index: fName in C:\Inetpub\wwwroot\php\cavaliero\portfolio\contact.php on line 59
Notice: Undefined index: lName in C:\Inetpub\wwwroot\php\cavaliero\portfolio\contact.php on line 62
Notice: Undefined index: email in C:\Inetpub\wwwroot\php\cavaliero\portfolio\contact.php on line 65
Notice: Undefined index: phone in C:\Inetpub\wwwroot\php\cavaliero\portfolio\contact.php on line 68[/font]
[font=arial]The errors all have one thing in common that they all reference the POST collection.[/font]
[font=arial]This form is designed to be a post-back type form so when the page is loaded the post collect should be blank initially. Until the user submits the form. If the data from the form fields are either blank or do not match a regExp pattern the form should be represented to the user with the last post collection as the form field values so that they can edit their mistakes. [/font]
[font=arial]I used the whole page code on a different server (godaddy) and it worked fine. Is there some type of configurations setting that turns off being able to access the POST collection if it has not yet been defined? I looked at he pages side by side for hours and I just cant fix it. Please Help![/font]
[font=arial]and thanks in advance![/font]