[英]Contact form submission
我是 php 的新手 - 我正在尝试使用提供的模板代码提交一个简单的联系表单。 我收到此错误报告:
致命错误:未捕获的错误:函数名称必须是 /home/u512372001/domains/thestripedphoenix.com/public_html/assets/php/mail.php:15 中的字符串:#0 {main} throw in /home/u512372001/ domain/thestripedphoenix.com/public_html/assets/php/mail.php 第 15 行
php 出了什么问题?
这是 HTML 表单代码:
<form method="POST" action="assets/php/mail.php">
<div class="row gtr-uniform">
<div class="col-6 col-12-xsmall">
<input type="text" name="name" id="name" value="" placeholder="Name" />
</div>
<div class="col-6 col-12-xsmall">
<input type="email" name="email" id="email" value="" placeholder="Email" />
</div>
<div class="col-12">
<select name="category" id="category">
<option value="">I want a . . .</option>
<option value="1">Logo</option>
<option value="1">Website</option>
<option value="1">Polished Document</option>
<option value="1">Promotional Material</option>
</select>
</div>
<div class="col-4 col-12-small">
<input type="radio" id="priority-low" name="priority" checked>
<label for="priority-low">Low</label>
</div>
<div class="col-4 col-12-small">
<input type="radio" id="priority-normal" name="priority">
<label for="priority-normal">Normal</label>
</div>
<div class="col-4 col-12-small">
<input type="radio" id="priority-high" name="priority">
<label for="priority-high">High</label>
</div>
<div class="col-12">
<textarea name="message" id="message" placeholder="Enter your message" rows="6"></textarea>
</div>
<p>
This site is protected by reCAPTCHA and the Google
<a href="https://policies.google.com/privacy">Privacy Policy</a> and
<a href="https://policies.google.com/terms">Terms of Service</a> apply. </p>
<div class="col-12">
<ul class="actions">
<li><input name="submit" name="submit" value="Send Message" class="primary" /></li>
<li><input type="reset" value="Reset" /></li>
</ul>
</div>
</div>
</form>
表单提交的php代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST['submit'])) {
$to = "mymail@gmail.com";
$subject = "Subject Line";
# Sender Data
$name = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["name"])));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$category = $_POST(["category"]);
$priority = $_POST(["priority"]);
$message = trim($_POST["message"]);
if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($category)) {
# Set a 400 (bad request) response code and exit.
http_response_code(400);
echo '<p class="alert alert-warning">Please complete the form and try again.</p>';
exit;
}
# Mail Content
$content = "Name: $name\r\n";
$content .= "Email: $email\n\n";
$content .= "I want a: $category\n\n";
$content .= "Priority: $priority\n\n";
$content .= "Message: $message\n\n";
# Send the email.
mail($to, $subject, $content);
if ($success) {
# Set a 200 (okay) response code.
http_response_code(200);
echo '<p class="alert alert-success">Thank You! Your message has been sent.</p>';
} else {
# Set a 500 (internal server error) response code.
http_response_code(500);
echo '<p class="alert alert-warning">Oops! Something went wrong, we couldnt send your message.</p>';
}
} else {
# Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo '<p class="alert alert-warning">There was a problem with your submission, please try again.</p>';
}
?>
从php表单来看,如果用户没有完整填写表单,那么就会出现验证错误信息。 提交所有必需的信息后,该消息应发送到我的电子邮件。
如果您需要进一步说明,请告诉我。 谢谢你的帮助!
这里:
$content .= "I want a: $category\n\n" // => missing ;
$content .= "Priority: $priority\n\n" // => missing ;
和这里:
echo '<p class="alert alert-warning">Oops! Something went wrong, we couldnt send your message.</p>';
}
} // => closing bracket not matching anything
为了能够看到致命错误,如语法错误,请确保在php.ini
启用display_errors
。 如果您不知道在哪里可以找到这个文件,您可以通过调用phpinfo()
并在其输出中查找“加载的配置文件”来找到您的php.ini
的位置。
对于所有非致命错误,您可以直接使用error_reporting()
和display_errors()
在脚本中启用显示错误:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
在运行代码之前,使用像PHPStorm这样的 IDE 还可以帮助您格式化和显示错误。
错误在 mail.php 文件中的第 15 和 16 行:
您的代码:
$category = $_POST(["category"]);
$priority = $_POST(["priority"]);
更新科斯:
$category = $_POST["category"];
$priority = $_POST["priority"];
只需删除括号“(”,“)”
我在 form.php 文件中给你一个建议:使用按钮而不是这样的输入:
<button type="submit" name="submit">Submit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.