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
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.