How to Call a PHP File from a Form Action
- 1). Open your HTML document and locate the action parameter of your form. This parameter will have the form "action=""". Insert your PHP file name between the quotes. Include the action parameter if necessary. Your data will look something like this:
<form action="send_email.php" method="post" >
<p>email: <input name="vemail" type="text" /></p>
<p>Name: <input name="vname" type="text" /></p>
<p>Subject: <input name="vsubject" type="text" /></p>
<p>Message:
<textarea name="vmessage" cols="40" rows="5"></textarea></p>
<p> <input type="submit" name="Submit" value="Send email" /></p >
</form>
Note the action value. The form data is sent to the send_email.php file. The PHP file contains script for sending this information to you in an email. - 2). Open a new file. Save it as send_email.php and insert the following:
<?php
// The data to submit
$name = $_POST['vname'];
$email = $_POST['vemail'];
$subject = $_POST['vsubject'];
$message = $_POST['vmessage'];
$to = myeaddr@site.com' . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$email.. "\r\n";
$body = "Name: ".$name. "<br/>";
$body .= "Email: ".$email. "<br/>";
$body .= "Subject: ".$subject. "<br/>";
$body .= "Message: ".$message. "<br/>";
// Send
mail($to, $subject, $body, $headers);
header( 'Location: '.$_SERVER['HTTP_REFERER']);
?>
Replace the value "myeaddr@site.com" with your own email address. The data your visitor inputs is captured from the $_POST values, and the PHP mail function is used to send it to your email. The $_SERVER['HTTP_REFERER'] value in the header function causes your visitor to be redirected back to the HTML Web page that contains the form. - 3). Save your PHP file and upload both the HTML and PHP files to your server using your FTP client. Navigate to your Web page to test your form.
Source...