0

Hi

I have a basic PHP contact form using JS validation for empty fields, format checks, etc, which all work fine. I now need to add a mail() using the user input.

I've made a version work, using some basic validation in PHP and comment out the JS, but I would like to keep the JS because (a) it's client-side, and (b) it took me a while to get it to work and I don't want to waste my efforts!

My <FORM> statement has "onsubmit="return validateForm(this)" and my Submit button is of type "submit".

The 'validateForm' script works fine, and returns "true" once the form is clean.

I now need to send a mail message when the form has been validated.

I'm not sure whether I should/can include the mail send in the JS script (before returning true), or add a mail() statement somewhere in the PHP code.

I really don't want to mess around with this any more than I have to, so can I please get a straightforward, clear response to what is probably is a much-asked question?

Thank you

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Monster Killer
0

One of the many possible answers is to use AJAX to send the email data back to your server and then have a server side script format and sent the email.

You have a couple of advantages in doing it this way, you can revalidate the sent values, and you can limit the number of emails send.

You Server side script can be run synchronously or asynchronously and you can provide the client with error messages or email sent messages.

0

I use javascript validation for PHP mail. This is what is use:

//Javascript validation
<head>
function validate_form1 ( ) { 
    valid = true;
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   	var address = document.form.Email.value;
	if(document.form.Name.value == "") {
      alert('Please enter your name');
      return false;
   } else if ( document.form.Email.value == "" ) { 
	alert ( "Please enter your email address" ); 
	valid = false;
	} else if(reg.test(address) == false) {
      alert('Invalid Email Address');
      return false;
   } else if(document.form.subject.value == "") {
      alert('Please enter the message subject');
      return false;
   } else if(document.form.message.value == "") {
      alert('Please enter the message you wish to send');
      return false;
   } 
	return valid;
}


</script>
</head>
<body>
//The form
			 <h1>Form</h1> <br />
<?PHP
//Show the form
$show_form=true;
//Check is the form was submitted
if(isset($_POST['Submit']))
{
//Set var's
	$namefield = $_POST['Name'];
	$emailfield = $_POST['Email'];
	$subjectfield = $_POST['subject'];
	$messagefield = $_POST['message'];
	$priorityfield = $_POST['priority'];
	$IPADDR = $_SERVER['REMOTE_ADDR'];
	$date = date("H:i:s D d M Y");
	
//The email body
	$body = <<<EOD
$date
Name: $namefield
Email: $emailfield
Subject: $subjectfield
Message: $messagefield
Priority: $priorityfield
$IPADDR
EOD;

//Email info
$to      = 'admin@yourdomain.com';
$subject = $_POST['subjectfield'];
$headers = 'From: support@yourdomain.com' . "\r\n" .
    'Reply-To: ' . $emailfield . '' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

//Send the mail
$success = mail($to, $subject, $body, $headers);
//Success text	
        echo "<h2>Form Submited! <br /> Thank You for contacting support. We will get back to you as soon as we can.</h2>";
		//Hide the form
        $show_form=false;       
    }
//Check if the form should be shown
if(true == $show_form)
{
?>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" onsubmit="return validate_form1 ( );">
Your name: <br />
<input type="text" name="Name" size="30" maxlength="30" />
<br /> 
Email address: <br />
<input type="text" name="Email" size="30" maxlength="30" />
<br /> 
Subject: <br />
<input type="text" name="subject" size="30" maxlength="50" />
<br />
Message:<br />
<textarea rows="8" cols="80" name="message"></textarea>
<br />
Priority: <br />
<select id="priority" name="priority">
<option value=" High">High</option>
<option value=" Medium">Medium</option>
<option value=" Low" selected="selected">Low</option>
</select>
<br /> <br />
<span class="red">All Fields Required *</span><br /> <br />
<p><input type="submit" name="Submit" class="formbutton" value="Send" /></p>
</form>
<?PHP
}//true == $show_form
?>
</body>

What does it validates all fields and checks for a valid email address. Once the form is submitted, the form it's self is hidden and the success text is displayed.

Hope this is what you were looking for.

Edited by Monster Killer: n/a

0

Woops, did a minor mistake:

//Email info
$to = 'admin@yourdomain.com';
$subject = $_POST['subject']; //changed subjectfield to subject
$headers = 'From: support@yourdomain.com' . "\r\n" .
'Reply-To: ' . $emailfield . '' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.