Hello, I am sort of new to PHP programming. I am very familiar with C++, but i seem to be having some stupid little problems in PHP.

The code I am writing is to validate the information from a form and then email the information in the form to an email account I set up.

first problem: I have a portion of my page where the output is determined by whether or not the email is sent successfully, problem is, both the confirmation and the failure messages show up on the web browser. Somehow, both of my echo statements are making it to the web browser. What did I miss? (and this little bit of php is only a portion of the rest of the php, but this is the only part I have a problem with.)


(some HTML code here)

if ($submit) {            // If submit flag is 1, send mail
                prepvars();
                sendmail();
            
            if ($success) {    // If mail sent successfully, display confirmation
                        echo ("<h3 align=\"center\">Your Submit was successful. We will contact you back as soon as possible. 
                            Thank you.</h3>");            
                            
            
            } else {        // If not successful, tell user
                echo ("<h3 align=\"center\">Your Submit failed. Sorry for the inconvienience. Please Re-Submit, or try again
                        at a later time.</h3>");
                        
            }                // End Submission Failure Warning
        
    }

(more HTML here)

Second problem: Can someone explain the scope of variables a little better to me. My email is being sent to the email account fine, but any portions of the email that would contain variable are empty.
I have a form, then all variables are passed via POST, then in the php script they are processed and then mailed. A lot of the processing happens inside of functions, and then they are mailed by the mail() function. Why are all of my variables blank? Am I getting their scope messed up, such that I, Ideally, i want to get a local variable, but instead it grabs an empty global variable.

Final problem: is there any way to include large portions of HTML within PHP code, without having to escape every single set of quotes? Is there a program that will take code and escape all the quotes for you?

If anyone can help any of my problems, I would really appreciate it.

Dani AI

Generated

Short, practical follow-up for that builds on 's points and gives steps to fix each symptom.

First — both messages showing

  • The usual cause is that the success flag isn't what the code expects or the code path runs twice. Make the mail-sending function return a clear boolean and assign it to a variable. Dump that value immediately after the call to confirm what it really is (use var_dump($success) or error_log()), and stop execution with exit while testing so only one branch can run. Also check that the file isn't being included twice and that the form action isn't submitting twice.

Second — blank variables in the message

  • Avoid relying on globals. Build the e-mail body from data passed in or returned from helper functions. Pass $_POST explicitly (and confirm the form field names match). Example pattern:
function buildBody(array $data) {
  return "Name: " . trim($data['name']) . "\nMessage: " . trim($data['message']) . "\n";
}

function sendMail($to, $subject, $body, $from) {
  $headers = "From: $from\r\n";
  return mail($to, $subject, $body, $headers);
}

$body = buildBody($_POST);
$success = sendMail('me@example.com', 'Contact', $body, $_POST['email']);
  • Also enable full error reporting while debugging (error_reporting(E_ALL); ini_set('display_errors', 1);) and var_dump($_POST) to confirm values arrive.

Third — embedding lots of HTML in PHP

  • Prefer HEREDOC/nowdoc or template files instead of escaping quotes. HEREDOC example:
$body = <<<HTML
<html>
  <body>
    <h1>Thanks</h1>
    <p>{$name}</p>
  </body>
</html>
HTML;
  • Or keep the HTML in a separate file and capture it with output buffering (ob_start(); include 'template.phtml'; $body = ob_get_clean();) so PHP variables can be interpolated cleanly.

Small note on echo: as observed, treat echo as a language construct — parentheses are allowed with a single argument, but the simplest practice is echo "text";.

Recommended Answers

All 2 Replies

What's the value of $success?

The syntax of echo does not include ( or ).
echo "This is an echo statement.";

You can have large amounts of HTML (or even small amounts) by turning off PHP processing. Use the ?> tag to do that.

if ( $file_not_found )
  {
  ?>  //Tell PHP not to process this further
  <font size="+2" color="red'>
  Not found, the file is.
  </font>
  <?php //Ok, start processing again
  }

The code above has not been tested, it's merely an example.

Oh yeah, about variable scope:
PHP doesn't treat scope like other languages. If you have a global, you have to declare it global in the function it's global. (Kinda goes against everything you think makes sense, doesn't it?) You can pass variables in via function headings, and they're always in out mode.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.