Question about $_POST
Hello I am trying to write a form that checks user mail address by existence of "@" character. I wrote these codes below but when I enter information at the html file I get this warning:
Notice: Use of undefined constant mail - assumed 'mail' in C:\xampp\test\send_simpleform.php on line 2
Can you please tell me why am I getting this notice? Thanks a lot. Here is the html file
<html>
<head>
<title>A simple HTML form</title>
</head>
<body>
<form action="send_simpleform.php" method="POST">
<p><strong>Name:</strong>
<input type="text" name="user"></p>
<p><strong>E-mail:</strong>
<input type="text" name="mail"></p>
<p><input type="submit" value="send"></p>
</form>
</body>
</html>
and this is the send_simpleform.php file's code:
<?php
if(strstr($_POST[mail],"@")){
echo "<p>Welcome <b>$_POST[user]</b>!</p>";
//echo "<p>Your message is:<b>$_POST[message]</b></p>";
}
else
echo"<p>Your e-mail address seems invalid. Please go back and enter a valid address.</p>"
?>
Thank you very much
newbiecoder
Junior Poster in Training
60 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
$_POST[mail]
should be
$_POST['mail']
the single quotes. Your form though is not effective. Here is a simple function that checks if the entire email has been formatted correctly.
function ValidEmail($email) {//checks if the email is in a valid foramat
return preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i",trim($email));
}
basically put the function in a separate file calledfunctions.inc.php and call it from your contact page by putting the following on the first line of the php code at top of send_simpleform.php page
require_once 'functions.inc.php';
that way you can easily call the function with:
if(ValidEmail($email)) {
}
epicrevolt
Junior Poster in Training
94 posts since Oct 2010
Reputation Points: 16
Solved Threads: 6
wow, that really helped, thank you very much!
newbiecoder
Junior Poster in Training
60 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0