我試圖讓這個表單提交工作,但我不熟悉PHP。我買了這個,但它似乎沒有工作。爲什麼我的表單提交不適用於PHP?
當我按下提交按鈕什麼都不會發生!我只是想知道代碼是否正確寫入。
預先感謝您
HTML代碼
<form method="post" id="contact-form" action="contact.php">
<div class="fields">
<div class="column">
<div class="field">
<input type="text" name="name" placeholder="Name*">
</div>
</div>
<div class="columnlast">
<div class="field">
<input type="text" name="email" placeholder="Email*">
</div>
</div>
</div>
<textarea cols="1" name="message" rows="4" placeholder="Message"></textarea>
<input type="submit" value="Submit">
</form>
PHP代碼
<?php
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = isset($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = isset($_GET['email']) ?$_GET['email'] : $_POST['email'];
$phone = isset($_GET['phone']) ?$_GET['phone'] : $_POST['phone'];
$message = isset($_GET['message']) ?$_GET['message'] : $_POST['message'];
if ($_POST) $post=1;
$errors = array();
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$phone) $errors[count($errors)] = 'Please enter your phone.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$message) $errors[count($errors)] = 'Please enter your message.';
//If the errors array is empty, send the mail
if (!$errors) {
// ====== mail here ====== //
$to = 'Ardi Mir <[email protected]>';
// Sender
$from = $name . ' <' . $email . '>';
//subject and the html message
$subject = 'Message from your website';
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
<tr><td>Name:</td><td>' . $name . '</td></tr>
<tr><td>Phone:</td><td>' . $phone . '</td></tr>
<tr><td>Email:</td><td>' . $email . '</td></tr>
<tr><td>Message:</td><td>' . nl2br($message) . '</td></tr>
</table>
</body>
</html>';
$result = sendmail($to, $subject, $message, $from);
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';
} else {
echo $result;
}
} else {}
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>
如果你想同時允許'GET'和'POST',您可以用'$ _REQUEST'。它合併'$ _GET'和'$ _POST'。 – Barmar 2014-10-04 01:37:17
@Barmar重要的是還要注意cookies也落在'$ _REQUEST'中,並且優先順序可以在PHP.ini中配置,這可以使其不可靠,這取決於您的特定需求。 – Brad 2014-10-04 01:39:15
什麼都沒有發生,或者你得到一個空白頁面?啓用錯誤報告:'error_reporting(E_ALL); ini_set(「display_errors」,1);' – Barmar 2014-10-04 01:39:55