简体   繁体   中英

Call PHP form from HTML

contact-form.html

<form method="POST" name="contactform" action="contact-form-handler.php"> 
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>

contact-form-handler.php

<?php 
$errors = '';
$myemail = 'net.dev@spikyarc.net';
if(empty($_POST['name'])  || 
empty($_POST['email']) || 
empty($_POST['message']))
{
   $errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
  "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail; 
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".


$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
  } 
?>

This both file i have put in wwwroot folder but when i submit html form it gives error The page cannot be displayed . I cannot find problem. Thanks for helping me.

set the action in the HTML form to:

contact-form-handler.php

like:

<form method="POST" name="contactform" action="contact-form-handler.php"> 

UPDATE 1: And check this:

Change

$email_subject = "Contact form submission: $name";

to

$email_subject = "Contact form submission:" . $name;

UPDATE 2: And this also:

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";

to:

$headers = "From: " . $myemail . "\n"; 
$headers .= "Reply-To:" . $email_address;

UPDATE 3:

You validate the E-mail address $email_address but never use it.

一定要检查动作中的文件名!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM