Log in

View Full Version : Resolved PHP simplified code.



NickDimou
07-07-2013, 11:56 AM
Greeting to all. I have persistant unsolved problem. In my website I have a form that works fine:

<form method="POST" name="contactform" action="http://blablabla.com/contact.php">
<input type="text" size="18" maxlength="45">
<textarea name="message" rows="4" cols="25" size="18">
</textarea>
<p>
<input type="submit" value="Send" name="submit">
<input type="reset" value="Clear" name="submit">
</p>
</form>

In my .php I have this which works fine:

<?php
$myemail = 'blabla@blabla.com';
error_reporting(0);
$message = $_POST['message'];
$email = trim($_POST['email']);
$Ok = ereg("^([a-zA-Z0-9_\.-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $email);
if ($Ok) {
$to = $myemail;
$email_subject = "Contact Form blblblbaa.com.";
$email_body = "Email: $email \n Message: $message";

$headers = "From: $email\n";
$headers .= "Reply-To: $myemail";
mail($to,$email_subject,$email_body,$headers);

if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$UNameFrm))
{
?>
<script language = 'javascript'>
alert('Thank you.');
history.go(-1);
</script>
<?
exit();
}
}

else {
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$UNameFrm))
{
?>
<script language = 'javascript'>
alert('Please provide a valid email.');
history.go(-1);
</script>
<?
exit();
}
}

?>

Can you please help me get rid of the pop up windows created by the alerts in the .php? It would be much better if everything happens on the page.

Thank you for your time and help. :)

NickDimou
07-07-2013, 12:56 PM
Solved it!

traq
07-07-2013, 05:51 PM
glad you solved it. As a side note, the ereg (http://php.net/ereg) functions are deprecated:
Warning
This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

Additionally, your regex will reject some valid email addresses (including several of mine, in fact). A better way to validate an email address is by using filter_var (http://php.net/filter_var):
<?php
$Ok = filter_var( $_POST['email'],FILTER_VALIDATE_EMAIL );

// or, if you want to enforce a tld in the domain part (probable):
$Ok = filter_var( $_POST['email'],FILTER_VALIDATE_EMAIL ) && preg_match( '#\.[\w]{2,}$#',$_POST['email'] );

and finally...

If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].
If you have not already done so, you should also consider sharing the solution to your problem.
This may be beneficial to others who encounter similar issues in the future.

:)

letom
07-07-2013, 08:14 PM
Can you please help me get rid of the pop up windows created by the alerts in the .php? It would be much better if everything happens on the page.

I would like to recommend jQuery Ajax for displaying validation messages in same page itself, it's clean and rigid