PHP Script由于Isset()而死亡。其他选项?
问题描述:
我有一个接触的形式,我试图运行,但是由于各种原因,该脚本将每次我试图提交它死的。不知道为什么。下面的代码有什么毛病吗?我之前没有使用isset(),但这里有人建议我这样做,现在它打破了页面。PHP Script由于Isset()而死亡。其他选项?
FORM
<form class="contactform" method="post" action="php/contact.php">
<h3>Name</h3>
<input class="form inputboxes" type="text" name="name">
<h3>Email</h3>
<input class="form inputboxes" type="email" name="email">
<h3>Phone</h3>
<input class="form inputboxes" type="number" name="phone">
<h3>Message</h3>
<textarea class="form inputboxes" name="message"></textarea>
<h3 class="captchastyle">Are you real? What is 2 + 2?</h3><input class="captcha captchastyle" type="text" name="captcha" maxlength="1">
<input class="form submit" name="submit" type="submit" value="Submit">
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$captcha = $_POST['captcha'];
$from = 'From: HankSmith.com Contact Form';
$to = '[email protected]';
$subject = 'HANK SMITH CONTACT FORM';
$body = "
From: $name\n
E-Mail: $email\n
Phone: $phone\n
Message: $message\n";
if (isset($_POST['submit'] && $captcha == 4)) {
if (mail ($to, $subject, $body)) {
echo '<p style="margin-top: 150; text-align:center; font-size: 18px;">Your message has been sent! Click <a href="../index.php">here</a> to return to the website.</p>';
} else {
echo '<p>Problem sending mail!';
}
} else {
echo '<p>Something went wrong, try again later!</p>';
}
?>
的error_log
[09-Feb-2017 12:15:46 America/New_York] PHP Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /home/yourerlv/public_html/hanksmith.com/php/contact.php on line 17
答
你就错了。您需要通过,所以它只能评估一个变量移动括号:
if (isset($_POST['submit']) && $captcha == 4) {
^
答
更改代码:各地`isset
if (isset($_POST['submit'] && $captcha == 4)) { // Original
if (isset($_POST['submit']) && $captcha == 4) { // Changed
你的括号()'测试是错误的。它应该是'if(isset($ _ POST ['submit'])&& $ captcha == 4)' – WillardSolutions