2

I created an HTML form that, on submit, opens submit_form.php. Inside, I process the data entered into the form and send a confirmation email. I wrote a function for the mail-sending-process.
Inside the .php file, I have PHP, as well as HTML code. The important parts of the file look like this:

<?php
    $success = "undefined";

    /* ... */

    function send_mail($to, $from, $fromName, $subject, $message) {
        /* headers, etc. */
    
        if (mail($to, $subject, "", $header)) {
            $success = "true";
        } else {
            $success = "false";
        }
    }
?>

<html>
    <body>
        success? <?php echo $success; ?>
    </body
</html>

Now, when loading the HTML on the success site (HTML code below the .php script, in the same file), I put this code to verify whether the email was sent successfully:

success? <?php echo $success; ?>

...which presents the following output: success? undefined

6
  • Is the mailto in the same file as the success? <?php echo $success; ?>? Commented Aug 3, 2017 at 8:51
  • please share more code, as much as possible. might be a little bit trivial myself here, but make sure the code you wrote is at the top of the page, before the html Commented Aug 3, 2017 at 8:51
  • Do you get into your send_mail function? try to var_dump something in there, and see if you even access the condition state Commented Aug 3, 2017 at 9:01
  • Well, since I receive the email, it probably executes the code, yes. Commented Aug 3, 2017 at 9:03
  • Does your function return success variable? afterall if it doesn't return anything it is pointless to have it there. Commented Aug 3, 2017 at 9:06

1 Answer 1

2

The problem is that the $success variable is not in global scope. Use global to make it global.

function send_mail($to, $from, $fromName, $subject, $message) {
        global $success;

        /* headers, etc. */

        if (mail($to, $subject, "", $header)) {
            $success = "true";
        } else {
            $success = "false";
        }
    }

Variable scope means where the variable is visible. In php,variables local to a function don't live in the global scope by default.

Sign up to request clarification or add additional context in comments.

5 Comments

Even though I created the variable outside the mail function? It's currently at the very top of the PHP part.
Yes there is two versions of $success, one in the outer global scope, and one in the functions scope. Thus you need global keyword.
Where do you call send_mail ? It needs to be executed before the echo $success
Woah! It actually works now. The global keyword was the key(-word, huehue)! Thanks!
The global keyword is not considered best practice because ot potentially floats or interferes with scope variables. Instead, use a return statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.