1
我有問題讓我的contact.php表單工作。之前,我沒有得到消息,我的電子郵件在所有的,現在,電子郵件不會被髮送,這是所有它表明:如何讓我的html代碼和php聯繫表單文件起作用?
From: E-mail: Message:
這是我的HTML表單:
<form role="form" class="form-alt" action="contact.php" method="post">
<div class="form-group">
<label for="name" class="sr-only">name</label>
<input type="text" class="form-control" id="name" placeholder="Name (Required)">
</div>
<div class="form-group">
<label for="email" class="sr-only">Email</label>
<input type="email" class="form-control" id="email" placeholder="Email (Required)">
</div>
<div class="form-group">
<label for="subject" class="sr-only">Subject</label>
<input type="text" class="form-control" id="subject" placeholder="Subject">
</div>
<div class="form-group">
<label for="message" class="sr-only">message</label>
<textarea id="message" class="form-control" cols="30" rows="7" placeholder="Message"></textarea>
</div>
<button type="submit" class="btn btn-default">Enviar</button>
</form>
</div>
這是PHP的聯繫人文件:
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = '[email protected]';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to [email protected]');
window.location = 'contact_page.html';
</script>
<?php
}
?>
+1爲了清楚起見,POST變量使用「name」屬性而不是id。在HTML中使用id沒有任何問題,因爲它看起來更加普遍有用並且一致,但是人們必須提供名稱屬性來表示他們希望以這種方式提交的字段。 – BrianHall