0

I get this error when trying to submit this form.

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in C:\Users\USER\XAMPP\htdocs\contactform\webform.php on line 3

This is line 3:

if(isset($userEmail = $_POST["email"]) && $userEmail = $_POST["email"] != "")

This is the entire part of code:

<?php
    $message_sent = false;
    if(isset($userEmail = $_POST["email"]) && $userEmail = $_POST["email"] != ""){
        if(filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)){
            $userName = $_POST["name"];
            $userEmail = $_POST["email"];
            $messageSubject = $_POST["subject"];
            $message = $_POST["message"];
            $to = "smartvishwa132@gmail.com";
            $body = "";
            $body .= "From: ".$userName. "\r\n";
            $body .= "Email: ".$userEmail. "\r\n";
            $body .= "Message: ".$message. "\r\n";
            mail($to,$messageSubject,$body);
            $message_sent = true;
        }
    }
?>

How do I fix this?

Vishwajith S
  • 3
  • 1
  • 2

1 Answers1

1
if(isset($userEmail = $_POST["email"]) && $userEmail = $_POST["email"] != ""){

Here, you are assigning it to a variable and then checking isset all in a single call. Split it up into separate calls:

  • check if it is set
  • check if it is not an empty string
  • validate it against the built-in email validator
  • assign it to a variable

Likely should be

if(isset($_POST["email"]) && $_POST["email"] != "") {
    if(filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {   
        ...
        $userEmail = $_POST["email"];
        ...
waterloomatt
  • 3,662
  • 1
  • 19
  • 25