I have a form on my home page for people to subscribe to a mailing list, this works and the content of my email address field gets stored correctly within my sqlsrv database.
Now I am trying to add email validation so have copied a bit of code from my registration script but this doesn't work and still allows me to just submit "test".
The code is as follows:
I have added this function just under the database connection details:
Code:
function isEmail($email){
return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;
}
I have then added the following in the process of what happens when submit is clicked:
The problem is definitively not in the parts of code you provided. Everything is correct there. The function isEmail is probably called in the wrong place or time. You should provide more code to locate the problem
<?php
$server="#"; // set database host
$username="#"; // set database user
$password="#"; // set database password
$database="#"; // set database name
function isEmail($emailAD){
return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $emailAD) ? TRUE : FALSE;
}
?>
$query="INSERT INTO enquiries (EmailAddress, DateTimeEntered) VALUES ('$emailAD',GetDate())";
but yes I agree that I could change
Code:
if(!isEmail($data['emailAD'])) {
to
Code:
if(!isEmail($emailAD)) {
And i'm not too sure what the $data is from, I just copied this bit of code from my registration page, not sure if it has anything to do with the following which I just found within an include from the registration page, not sure what this is doing though:
Code:
function filter($data) {
$data = trim(htmlentities(strip_tags($data)));
if (get_magic_quotes_gpc())
$data = stripslashes($data);
$data = mysql_real_escape_string($data);
return $data;
}
Have tried this but I still just keep getting redirected to the subscribed page with the content getting put in the database, and im only typing "test".
Right, done that and it works (i think) because if I type in what looks like an email address, I go through to the subscribed page, and if I do test, it looks like its refreshing my index page but the error message doesnt display?
Well, that would be a second question Basically you assign an error message to an array $err[] but then you redirect to another page - and that means all the variables, arrays etc are lost
You need to find a way to pass the value (error message) to another page, either by using $_GET and passing it via URL, or using sesions, which is a bit more complicated
So would it be easier to just have a prompt pop up when the user clicks submit to say "your email address is not valid" and then the user has to click OK and correct their email, and if so, how is this done?
The code I have taken from my registration script to use for this works there so there must be a way, when someone fills out the registration page it uses the same validation stuff and if the email is not right, returns the registration page with a message under the input sayin "email is not valid", this is near enough to same script but doesnt display the message.
Bookmarks