PHP脚本因Isset()而死。其他选择?

时间:2017-02-09 17:19:18

标签: php html forms contact

我有一个我正在尝试运行的联系表单,但无论出于何种原因,每次我尝试提交脚本时脚本都会死亡。不知道为什么。下面的代码有什么问题吗?我之前没有使用过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 = 'thatbraxjohnsonguy@gmail.com'; 
    $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致命错误:不能在表达式的结果上使用isset()(您可以在/ home /中使用“null!== expression”)第17行的yourerlv / public_html / hanksmith.com / php / contact.php

2 个答案:

答案 0 :(得分:2)

你做错了。您需要移动括号,以便仅评估一个变量:

if (isset($_POST['submit']) && $captcha == 4) { 
                          ^

答案 1 :(得分:2)

更改代码:

if (isset($_POST['submit'] && $captcha == 4)) {  // Original
if (isset($_POST['submit']) && $captcha == 4 ) { // Changed