By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,679 Members | 1,500 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,679 IT Pros & Developers. It's quick & easy.

Unexpected $end and the If...Else statement

P: 3
I have written a php script (test3.php), which I attached as a text file. Its includes are also attached as text files. I'm trying to run the script here: http://www.wondergy.com/phptestbed/test3.php

The problem I'm having is the frustrating Unexpected $end. Yes, I know that it usually means something isn't being closed correctly. The problem is, I can't figure out what. To make the problem stranger, I can get it to parse when I break it up into three files and include the two files in a large If...Else statement. The first time the script runs, it goes to the Else part of the statement and includes form3.php. Perfect, the form displays and the world is happy. No unexpected $end error. BUT - when I take that very same code (from form3.php) and include it inside the else brackets {}, I get the unexpected $end error. How can this be? How can I fix this?
Attached Files
File Type: txt form3.txt (776 Bytes, 419 views)
File Type: txt handle3.txt (1.2 KB, 438 views)
File Type: txt test3.txt (1.4 KB, 280 views)
Nov 13 '09 #1

✓ answered by Markus

On line 33 of handle3.php, make sure EMAIL_RESPONSE has no whitespace before it, that is, it should be the very first thing on the line. Consider the following:

Expand|Select|Wrap|Line Numbers
  1. // good
  2.     echo <<<HTML
  3.     <input>...</input>
  4. HTML;
  5. // bad
  6.     echo <<<HTML
  7.     <input>...</input>
  8.     HTML;
  9.  
Mark.

Share this Question
Share on Google+
4 Replies


Markus
Expert 5K+
P: 6,050
The error text is? Short of running the code ourselves (extra effort on our part) we do not know what part of the code we should be looking at. Also, instead of providing the files as attachments, if the files do not have 1000+ lines, just post them here (wrapped with [code] tags) for us to see.

Mark.
Nov 13 '09 #2

P: 3
The exact error message is as follows:
Parse error: syntax error, unexpected $end in ../handle3.php on line 49

test3.php:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Email Form</title>
  6. </head>
  7. <body>
  8.  
  9. <?php
  10. /* This is a test of a form generating an email */
  11. $myaddy = "charles@wondergy.com";
  12. $file = $_SERVER['PHP_SELF'];
  13. //First, an if statement to see if the form has been submitted
  14. if (isset($_POST['submitted']))
  15. {
  16.     //check for errors
  17.     //initializing the errors array
  18.     include("handle3.php");
  19. }
  20. else
  21. {
  22.     //Show the form
  23.     include("form3.php");
  24.     /* Sends to itself again; 
  25.     Uses "submitted" as a check value */
  26.     /*echo <<<END_OF_FORM
  27.     <form action="$file" method="POST"> 
  28.     <input type="hidden" name="submitted" value="TRUE" /> 
  29.     <table>
  30.         <tr>
  31.             <td>Name:</td>
  32.             <td><input type="text" size="55"  name="name" /></td>
  33.         </tr>
  34.         <tr>
  35.             <td>Email:</td>
  36.             <td><input type="text" size="55"  name="email" /></td>        
  37.         </tr>
  38.         <tr>
  39.             <td>Subject:</td>
  40.             <td><input type="text" size="55"  name="subject" /></td>
  41.         </tr>
  42.     </table>
  43.  
  44.     <textarea name="body" rows="4" cols="62"></textarea><br />
  45.     <input type="submit" name="Submit Email" value="Submit Email" /><input type="reset" name="Reset" value="Reset This, Please." />
  46.  
  47.     </form>
  48.     END_OF_FORM;*/
  49. }
  50.  
  51.  
  52. ?>
  53. </body>
  54. </html>
  55.  
form3.php:
Expand|Select|Wrap|Line Numbers
  1. <?php    
  2.  
  3. /* Sends to itself again; 
  4. Uses "submitted" as a check value */
  5. echo <<<END_OF_FORM
  6. <form action="$file" method="POST"> 
  7. <input type="hidden" name="submitted" value="TRUE" /> 
  8. <table>
  9.     <tr>
  10.         <td>Name:</td>
  11.         <td><input type="text" size="55"  name="name" /></td>
  12.     </tr>
  13.     <tr>
  14.         <td>Email:</td>
  15.         <td><input type="text" size="55"  name="email" /></td>        
  16.     </tr>
  17.     <tr>
  18.         <td>Subject:</td>
  19.         <td><input type="text" size="55"  name="subject" /></td>
  20.     </tr>
  21. </table>
  22.  
  23. <textarea name="body" rows="4" cols="62"></textarea><br />
  24. <input type="submit" name="Submit Email" value="Submit Email" /><input type="reset" name="Reset" value="Reset This, Please." />
  25.  
  26. </form>
  27. END_OF_FORM;
  28. ?>
  29.  
handle3.php:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $errors = array();
  3. //checking for each entry
  4. if (empty($_POST['name'])) 
  5. {
  6.     $errors[] = "No Name Entered";
  7. }
  8. if (empty($_POST['email'])) 
  9. {
  10.     $errors[] = "No Email Entered";
  11. }
  12. if (empty($_POST['subject'])) 
  13. {
  14.     $errors[] = "No Subject Entered";
  15. }
  16. $body = stripslashes($_POST['body']); //No other error checking for the body
  17. $message = $body;
  18. //Now checking for errors
  19. if (empty($errors))
  20. {
  21.     //Send email; Post response
  22.     $name = $_POST['name'];
  23.  
  24.     $body = "From: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nSubject: " . $_POST['subject'] . "\nMessage: " . $body;
  25.  
  26.     mail($myaddy, "Form Email - " . $_POST['subject'], $body, "From: " . $_POST['email']); //sends the mail
  27.  
  28.     echo <<<EMAIL_RESPONSE
  29.     <h1>Email Sent</h1>
  30.     <p>$name, your email has been sent.</p>
  31.     <p>Your message was: $message</p>
  32.     <p><a href="$file">Send another email</a></p>
  33.     EMAIL_RESPONSE;
  34. }
  35. else
  36. {
  37.     //Post error page w/ link to try again
  38.     echo "<h1>Error!</h1>\n<p>You did not complete the form. Please address the following problems and try again.</p>\n";
  39.  
  40.     echo "<ul>\n";
  41.     foreach ($errors as $key => $val)
  42.     {
  43.         //Print out a line of errors
  44.         echo "<li>$val</li>\n";
  45.     }
  46.     echo "</ul>\n<p><a href=\"$file\">Try Again</a></p>";
  47. }
  48.  
  49. ?>
  50.  
Nov 13 '09 #3

Markus
Expert 5K+
P: 6,050
On line 33 of handle3.php, make sure EMAIL_RESPONSE has no whitespace before it, that is, it should be the very first thing on the line. Consider the following:

Expand|Select|Wrap|Line Numbers
  1. // good
  2.     echo <<<HTML
  3.     <input>...</input>
  4. HTML;
  5. // bad
  6.     echo <<<HTML
  7.     <input>...</input>
  8.     HTML;
  9.  
Mark.
Nov 13 '09 #4

P: 3
Perfect! Thank you! (............adding extra characters to have more than 20....)
Nov 13 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.