Log in

View Full Version : Php contact form help



volkantr19
01-08-2014, 01:45 PM
Hallo everybody;

I have this php contach form.I want to change warning item.
For example: If somebody want to send empty email,they see warnings (for example:please enter your name,please enter your email adress etc.)

In this form,i think something wrong.If i want to change warning text for $name,automaticly change $email at the same time too.

Please help me to resolve this problem.

Thank you so much in advance...




<?php
if(!session_id()) {
session_start();
}
error_reporting(0);

if (isset($_REQUEST['action'])) {
if ($_REQUEST['action'] == "contact_form_request") {

$ourMail = "v.koretskiy@gmail.com";

$required_fields = array("name", "email", "message");
$pre_messagebody_info = "";
$errors = array();
$data = array();
parse_str($_REQUEST['values'], $data);

//check for required and assemble message

if (!empty($data)) {
foreach ($data as $key => $value) {
$name = strtolower(trim($key));
if (in_array($name, $required_fields)) {
if (empty($value)) {
$errors[$name] = "Please enter your " . $name . "!";
}
}

if ($name == "email") {
if (!check_email_address($value)) {
$errors[$name] = "You've entered invalid email address!";
}
}
}
}

//***

$verify = $_SESSION['verify'];
if ($verify != md5($data['verify'])) {
$errors["verify"] = "The verification code you've entered is wrong!";
}

//***
$result = array(
"is_errors" => 0,
"info" => ""
);
if (!empty($errors)) {
$result['is_errors'] = 1;
$result['info'] = $errors;
echo json_encode($result);
exit;
}

$pre_messagebody_info.="<strong>Name</strong>" . ": " . $data['name'] . "<br />";
$pre_messagebody_info.="<strong>E-mail</strong>" . ": " . $data['email'] . "<br />";

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers.= "From: ".$data['email']."\r\n";

$after_message = "\r\n<br />--------------------------------------------------------------------------------------------------\r\n<br /> This mail was sent via contact form";

if (mail($ourMail, "Email from contact form", $pre_messagebody_info .="<strong>Message</strong>" . ": " . $data['message'] .$after_message, $headers)) {
$result["info"] = "success";
} else {
$result["info"] = "server_fail";
}

echo json_encode($result);
exit;
}
}

function check_email_address($email) {
// First, we check that there's one @ symbol,
// and that the lengths are right.
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// Email invalid because wrong number of characters
// in one section or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if
(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
в†Є'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
// Check if domain is IP. If not,
// it should be valid domain name
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if
(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
в†Є([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
?>

traq
01-08-2014, 03:38 PM
In this form,i think something wrong.If i want to change warning text for $name,automaticly change $email at the same time too...
// snip

$required_fields = array("name", "email", "message");
$pre_messagebody_info = "";
$errors = array();
$data = array();
parse_str($_REQUEST['values'], $data);

//check for required and assemble message

if (!empty($data)) {
foreach ($data as $key => $value) {
$name = strtolower(trim($key));
if (in_array($name, $required_fields)) {
if (empty($value)) {
$errors[$name] = "Please enter your " . $name . "!";
}
}

if ($name == "email") {
if (!check_email_address($value)) {
$errors[$name] = "You've entered invalid email address!";
}
}
}
}I'm not quite sure what you mean by this. Can you explain further? Are you trying to give each field its own error message?

Note that $name is not the "name" field, it's the name of each field that you're looping through. The way you have it set up right now, all fields will get the empty($value) check, and the "email" field will also get the check_email_address check.

~~~~~~~~~~
As a side note, that's one of the more involved email checks I've seen. I am not sure why the characters в†Є are in there…? There are two other things you might want to consider:

1) The ereg functions have been deprecated for quite some time; relying on them is highly discouraged.
2) There is a much simpler and more complete way to validate email addresses:
<?php
filter_var( $value,FILTER_VALIDATE_EMAIL );The only thing you might want to add is to check that the email address has a TLD (i.e., that it's an address routable over http and not a local email address):
preg_match( "#\.[^@]*$#",$value )

volkantr19
01-08-2014, 04:37 PM
Yes,you understood my problem.
I am trying to give each field its own error message.

Ok,i send all files to SERVER and you can see what i mean.

volkantr19
01-08-2014, 05:33 PM
Here you can see what i mean.

http://ornek5.antalyabalikcilik.com.tr/iletisim.html

If you click SEND button you see this errors:
Please enter your name!
You've entered invalid email adress!
Please enter your message!
The verification code you've entered is wrong!




//check for required and assemble message

if (!empty($data)) {
foreach ($data as $key => $value) {
$name = strtolower(trim($key));
if (in_array($name, $required_fields)) {
if (empty($value)) {
$errors[$name] = "Please enter your " . $name . "!";
}
}

if ($name == "email") {
if (!check_email_address($value)) {
$errors[$name] = "You've entered invalid email address!";
}
}
}
}



~~~~~~~~~AFTER I CHANGE ERROR MESSAGES~~~~~~~~~~~


And i change the error message for name filed.
You can see the result here.
If you click SEND button you see this errors:
How are you name!
You've entered invalid email adress!
How are you message!
The verification code you've entered is wrong!

http://ornek7.antalyabalikcilik.com.tr/iletisim.html




//check for required and assemble message

if (!empty($data)) {
foreach ($data as $key => $value) {
$name = strtolower(trim($key));
if (in_array($name, $required_fields)) {
if (empty($value)) {
$errors[$name] = "How are you " . $name . "!";
}
}

if ($name == "email") {
if (!check_email_address($value)) {
$errors[$name] = "You've entered invalid email address!";
}
}
}
}


~~~~~~~~~~~~~~

The problem is:
If i want to change error message for ''name field'' ; message field become ''same error message'' automaticly.

traq
01-08-2014, 07:05 PM
Here you can see what i mean …
~~~~~~~~~AFTER I CHANGE ERROR MESSAGES~~~~~~~~~~~

And i change the error message for name filed.
You can see the result here.
If you click SEND button you see this errors:
How are you name!
You've entered invalid email adress!
How are you message!
The verification code you've entered is wrong!



Note that $name is not the "name" field, it's the name of each field that you're looping through.

You did not "change the error message for [the] name field"; you changed the error message for all (empty) fields.

To work with the "name" field specifically, you need to check what $name is. For example:
if( $name === "name" && empty( $value )){
$errors[$name] = "How are you $name!";
}

Also, "How are you name!" seems like an unusual error message…?

volkantr19
01-08-2014, 07:31 PM
Also, "How are you name!" seems like an unusual error message…?

Of course it's unusual error message.It was just an example to show you how is going.

volkantr19
01-08-2014, 07:33 PM
I have a hard question to you.
In this php form there is no error message field for ''messege''.


error message for name:


if (!empty($data)) {
foreach ($data as $key => $value) {
$name = strtolower(trim($key));
if (in_array($name, $required_fields)) {
if (empty($value)) {
$errors[$name] = "Please enter your " . $name . "!";
}
}




error message for email:



if ($name == "email") {
if (!check_email_address($value)) {
$errors[$name] = "You've entered invalid email address!";
}
}
}
}




error message for verification code:



$verify = $_SESSION['verify'];
if ($verify != md5($data['verify'])) {
$errors["verify"] = "The verification code you've entered is wrong!";
}



where is error message for ''message field'' ?

traq
01-08-2014, 08:26 PM
Of course it's unusual error message.It was just an example to show you how is going.
Okay; I figured it might be something like that.


I have a hard question to you.
In this php form there is no error message field for ''messege''.

error message for name:


foreach ($data as $key => $value) {
$name = strtolower(trim($key));
if (in_array($name, $required_fields)) {
if (empty($value)) {
$errors[$name] = "Please enter your " . $name . "!";
}
}
}

No. Please re-read me responses above. This code applies to all form fields, in turn. (read about foreach loops (http://php.net/foreach).)

You have shown no code that applies only, specifically to the "name" field. To do so, see the code example I gave in my post ( #5 ) above.

volkantr19
01-08-2014, 08:35 PM
I read all of them but i couldn't fix up.

My last question.

Is it possible to give each field its own error message seperatly?

For example:
Name: ''Please write your name!''
Email: ''Please write your email!''
Message: ''Please enter your message!''
Verify: ''Please write true verify number!''

traq
01-09-2014, 05:09 AM
Is it possible to give each field its own error message seperatly?
Of course. That's the example code I pointed out in my prior post.


// inside your foreach loop

// to check a specific field, check for the field name inside your loop.
// this, for example, checks the "name" field specifically to see if its value is empty.
if( $name === "name" && empty( $value )){
$errors[$name] = "Please write your name!";
}

// do the same for "email", "verify", etc.

volkantr19
01-09-2014, 08:32 AM
Of course. That's the example code I pointed out in my prior post.


// inside your foreach loop

// to check a specific field, check for the field name inside your loop.
// this, for example, checks the "name" field specifically to see if its value is empty.
if( $name === "name" && empty( $value )){
$errors[$name] = "Please write your name!";
}

// do the same for "email", "verify", etc.


If i add this code,shall i delete some code from orginal php code ?

traq
01-09-2014, 11:44 PM
It depends on what you want your script to do. For example, by looping through each field, the current code allows you to check that all three fields are filled out without actually having to write that code three times. If you're going to check each field individually anyway, then you might get rid of the loop. On the other hand, if you don't want to check anything special (you just want special error messages), then you might as well leave the code inside the loop and just have a list of pre-defined messages for each field.

So, what do you want to do? Start by listing each field on your form, and then deciding what errors you want to check for, and what messages to use in each case. Then, we can look at how best to organize them.