PHP must be run on a web server that has a php engine. It cannot be run directly in the browser from a file on your desktop.
PHP / HTML5 Contact Form - What is Wrong [closed]
-
10-10-2022 - |
Question
I am just starting off with PHP and doing a few tutorials. I have just built the following PHP contact form, but the PHP syntax in the header bleeds into the webpage and I am completely unsure why? It might be worth mentioning I have also saved this file as contactForm.php and not contactForm.html (does that make a difference). What is wrong here?
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title> Exercise: Contact Form</title>
<link type="text/css" rel="stylesheet" href="css/myStyle.css" />
<?php
$name = $_POST ['name'];
$email = $_POST ['email'];
$message = $_POST ['message'];
$from = 'From: Me';
$to = 'email@email.co.uk';
$subject = 'Hello';
$human = $_POST ['human'];
$body = "From $name\n Email: $email\n Message: $message\n";
if ($_POST ['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
}
else{
echo '<p>Something went wrong, please try again.</p>';
}
}
else if ($_POST ['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly.</p>';
}
?>
</head>
<body>
<header>
<h1>My Test Page</h1>
</header>
<form id="testForm" method="post" action="contactForm.php">
<label>Name:</label>
<input class="input-fields" type="text" name="name" placeholder="Name" />
<label>E-mail:</label>
<input class="input-fields" type="email" name="email" placeholder="Email Address" />
<label>Message:</label>
<textarea class="input-fields" name="message" placeholder="Enter Message here" rows="5"></textarea>
<label>What is 2 + 2?</label>
<input class="input-fields" name="human" placeholder="Enter Answer">
<input type="submit" name="submit" id="submit" value="Click to Submit" />
</form>
</body>
Here is the image of how it displays...
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow