我对HTML和CSS有相当基础的知识,从头开始构建我的网站。 我在iFrame中使用JotForm作为我的联系表单,但是想将其更改为PHP表单,所以我不依赖于这些人,我可以更好地控制表单的外观。
我最初的表格是发送一封包含空白字段的电子邮件 - 我试图解决此问题,现在已经完全搞砸了。
非常感谢任何帮助。
以下是我在表格中列出的表单的HTML;
<table width="400" border="0" align="left" cellpadding="5" id="form">
<tr>
<th width="134" scope="row" align="right">NAME</th>
<th width="240" scope="row">
<form name="name" method="post" action="contact.php">
<span id="sprytextfield1">
<input type="text" name="name" id="name" value="<?php print "$nameField"; ?>">
<span class="textfieldRequiredMsg">A value is required.</span><spanclass="textfieldInvalidFormatMsg">Invalid format.</span></span>
</form></th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"> </th>
</tr>
<tr>
<th scope="row" align="right">EMAIL</th>
<th scope="row">
<form name="email" method="post" action="contact.php">
<span id="sprytextfield2">
<input type="text" name="email" id="email" value="<?php print "$emailField"; ?>">
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>
</form></th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"> </th>
</tr>
<tr>
<th height="128" scope="row" align="right">MESSAGE</th>
<th scope="row">
<form name="message" method="post" action="contact.php">
<span id="sprytextarea1">
<textarea name="message" id="message" cols="45" rows="5"><?php print "$messageField"; ?></textarea>
<span class="textareaRequiredMsg"><br>
A value is required.</span></span>
</form></th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"> </th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"><form name="submit" method="post" action="contact.php">
<input type="hidden" name="parse_var" id="parse_var" value="contactform">
<input type="submit" name="submit" id="submit" value="SEND MESSAGE">
</form></th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"><?php print "$sent"; ?></th>
</tr>
</table>
和我的PHP:
<?php
if ($_POST['parse_var'] == "form") {
$emailTitle = 'New Contact Form Message';
$yourEmail = 'charlotte@charlottemay.co.uk';
$emailField = $_POST['email'];
$nameField = $_POST['name'];
$messageField = $_POST['message'];
$body = <<<EOD
<br><hr><br>
Email: $emailField <br />
Name: $nameField <br />
Message: $messageField <br />
EOD;
$headers = "From: $emailField\r\n";
$headers .= "content-type: text/html\r\n";
$success = mail("$yourEmail", "$emailTitle", "$body", "$headers");
$sent = "thankyou, your message has been successfully sent x";
}
?>
答案 0 :(得分:0)
好的,这里有功能,但不是好的形式,每个字段都应该被验证/检查(必填字段,字段格式等等)。
但是,这会奏效。我已经清理了你的HTML代码,请看你犯错的地方......
如上所述,您的HTML结构是主要问题 - 您有多个FORM标签 - 对于表单的每个字段 - 这不是正确的方法,请参阅HTML表单的HTML基本示例...
代码:
<?php
if($_POST['parse_var']=='contactform') {
$emailTitle = 'New Contact Form Message';
$yourEmail = 'charlotte@charlottemay.co.uk';
$emailField = $_POST['email'];
$nameField = $_POST['name'];
$messageField = $_POST['message'];
$body = "
<br><hr><br>
Email: $emailField <br />
Name: $nameField <br />
Message: $messageField <br />
";
$headers = "From: $emailField\r\n";
$headers .= "content-type: text/html\r\n";
$success = mail("$yourEmail", "$emailTitle", "$body", "$headers");
$sent = "thankyou, your message has been successfully sent x";
}
?>
<table width="400" border="0" align="left" cellpadding="5" id="form">
<tr>
<th width="134" scope="row" align="right">NAME</th>
<th width="240" scope="row">
<form name="name" method="post" action="contact.php">
<span id="sprytextfield1">
<input type="text" name="name" id="name" value="">
<span class="textfieldRequiredMsg">A value is required.</span></span>
</th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"> </th>
</tr>
<tr>
<th scope="row" align="right">EMAIL</th>
<th scope="row">
<span id="sprytextfield2">
<input type="text" name="email" id="email" value="">
<span class="textfieldRequiredMsg">A value is required.</span></span>
</th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"> </th>
</tr>
<tr>
<th height="128" scope="row" align="right">MESSAGE</th>
<th scope="row">
<span id="sprytextarea1">
<textarea name="message" id="message" cols="45" rows="5"></textarea>
<span class="textareaRequiredMsg"><br>
A value is required.</span></span>
</th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"> </th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row">
<input type="hidden" name="parse_var" id="parse_var" value="contactform">
<input type="submit" name="submit" id="submit" value="SEND MESSAGE">
</form></th>
</tr>
<tr>
<th scope="row"> </th>
<th scope="row"><?php print "$sent"; ?></th>
</tr>
</table>
P.S。需要您自担风险使用它。 :)为避免发送垃圾邮件,必须验证空表单发送 - 字段 - 有大量关于此的教程,请检查其中一些。
答案 1 :(得分:0)
首先使用<div>
和<p>
替换表来重构您的html。表格是表格式的,但不应将表格用于一般布局。
还尝试避免PHP脚本中的html。请改用单独的视图。 PHP框架可能会帮助您。看看Codeginiter。
将<form>
标记视为div容器。用它包裹整个表格。所以从<form>
开始,然后将所有与表单相关的元素放在最后的</form>
内,并以<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
<!-- Your header items like meta and link for css stylesheets -->
</head>
<body>
<div id="container">
<form id="form-id" action="contact.php" method="POST">
<input id="parse_var" type="hidden" name="parse_var" value="contactform">
<div>
<label for="name">Name</label>
<input id="name" type="text" name="name" value="<?php echo $nameField; ?>">
<?php if (empty($nameField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?>
<?php if (empty($nameField)) echo '<span class="textfield invalidformatmsg">Invalid format.</span>'; ?>
</div>
<div>
<label for="email">E-Mail</label>
<input id="email" type="text" name="email" value="<?php echo $emailField; ?>">
<?php if (empty($emailField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?>
<?php if (empty($emailField)) echo '<span class="textfield invalidformatmsg">Invalid format.</span>'; ?>
</div>
<div>
<label for="message">Message</label>
<textarea id="message" name="message"><?php echo $messageField; ?></textarea>
<?php if (empty($messageField)) echo '<span class="textfield requiredmsg">A value is required.</span>'; ?>
</div>
<div class="form-controls">
<input type="submit" name="submit" id="submit" value="Send message">
</div>
</form>
</div>
</body>
</html>
结束。这使得html文档中的表单部分脱颖而出,因此您可以更轻松地使用(可读性)。
我更改了您的代码示例:
表单HTML:
<?php
// Create a view load function
function loadView($view)
{
// Change this to where you save the view files
$viewPath = '/'.$view.'.php';
// Check if the requested view exists
if ( ! file_exists($viewPath)) return false;
// This will put the view source in a variable
// without flushing to the screen (like echo / print)
ob_start();
include $viewPath;
$buffer = ob_get_contents();
@ob_end_clean();
return $buffer;
}
// Check if the form was submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['parse_var']))
{
// Differentiate between forms
if ($_POST['parse_var'] == 'contactform')
{
// Check if all required data was submitted
if (isset($_POST['name'], $_POST['email'], $_POST['message'])
&& filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
// Mail Message Settings
$emailTitle = 'New Contact Form Message';
$yourEmail = 'charlotte@charlottemay.co.uk';
// Get the form data (remember to secure user input against e.g. CSRF)
$emailField = $_POST['email'];
$nameField = htmlentities($_POST['name']);
$messageField = strip_tags($_POST['message']);
// Load the email body view. The variables above
// will be accessible in the view.
$body = loadView('email_body');
// Set the mail headers
$headers = "From: $emailField\r\n";
$headers .= "Content-type: text/html\r\n";
// Send the message - I highly recommend you to use something like SwiftMailer!
$mail = mail($yourEmail, $emailTitle, $body, $headers);
// Check if the mail was sent
if ($mail)
{
echo 'Sweet! Your message has been successfully sent.';
}
else
{
echo 'Whoops! The message was not sent.';
}
}
else
{
// Here you could return an error message
}
}
}
关于您的PHP代码,请查看:
"
请注意,我将双'
引号更改为单{{1}}引号。为什么?看到这个问题:https://stackoverflow.com/a/3446286/2493918。
我强烈建议您使用SwiftMailer之类的东西来发送带有PHP的电子邮件!使用本机mail()函数可能并不总是按预期工作,并且大多数已发送的邮件将被抛出收件人SPAM文件夹(如gmail等)。
阅读代码注释,了解有关我所做更改的更多信息。
了解PHP所提供内容的一个很好的资源是PHP文档,可以在这里找到:例如ob_start()函数http://www.php.net/manual/en/function.ob-start.php。
快乐的编码!