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><br>
<input type="text" name="user"></p>
<p><strong>E-mail:</strong><br>
<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:<br><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

$_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 called functions.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)) {
}

wow, that really helped, thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.