Mailing form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ninJo
Forum Newbie
Posts: 1
Joined: Wed Dec 14, 2011 4:00 am

Mailing form

Post by ninJo »

Hello, this is my code:

Code: Select all

<?php

  $name = $_REQUEST['name'] ;
  $fam = $_REQUEST['fam'] ;
  $email = $_REQUEST['email'] ;
  $country = $_REQUEST['country'] ;
  $tel = $_REQUEST['tel'] ;
  $city = $_REQUEST['city'] ;
  $message = $_REQUEST['message'] ;
  

$message = "
		name:		$name  \n
		last-name:		$fam  \n
		mail:		$email  \n
		country:		$country  \n
		city:		$city  \n
		phone:		$tel  \n
		message:	$message \n";

 if($result == 'success')
{ 

mail( "my_mail@mail.com", "New message",
$message, "From: $email");	

  } 

if ( ! empty($_POST['contact']))
{
	$valid = array
	(
		'name'    => array('/(.+){2,}/', 'Error.'),
		'fam'    => array('/(.+){2,}/', 'Error.'),
		'tel'    => array('/(.+){5,}/', 'Error'),
		'city'    => array('/(.+){3,}/', 'Error'),
		'email'   => array('/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD', 'Error.'),
		'message' => array('/(.+){10,}/', 'Error.'),
	);
	
	$errors = array();
	
	foreach ($valid as $field => $data)
	{
		$regex = $data[0];
		$message = $data[1];
		
		$input = trim($_POST[$field]);
		
		if (empty($input) OR ! preg_match($regex, $input))
		{
			$errors += array($field => $message);
		}
	}
	
	$result = empty($errors) ? 'success' : 'errors';
	
	echo json_encode(array
	(
		'result' => $result,
		'errors' => $errors,
	));
	exit;
}

?>
It's for this mailing form (http://coreyworrell.com/blog/article/cr ... -ajax-form)

But it doesn't send mails.. What I am doing wrong?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Mailing form

Post by social_experiment »

Have you tried moving this code to the bottom of the page

Code: Select all

<?php
 if($result == 'success')
{ 

mail( "my_mail@mail.com", "New message",
$message, "From: $email");      

  } 
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply