Unexpected $end and the If...Else statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maveri4201
    New Member
    • Nov 2009
    • 3

    Unexpected $end and the If...Else statement

    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
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

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

    Comment

    • maveri4201
      New Member
      • Nov 2009
      • 3

      #3
      More Information

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

      test3.php:
      Code:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Email Form</title>
      </head>
      <body>
      
      <?php
      /* This is a test of a form generating an email */
      $myaddy = "charles@wondergy.com";
      $file = $_SERVER['PHP_SELF'];
      //First, an if statement to see if the form has been submitted
      if (isset($_POST['submitted']))
      {
      	//check for errors
      	//initializing the errors array
      	include("handle3.php");
      }
      else
      {
      	//Show the form
      	include("form3.php");
      	/* Sends to itself again; 
      	Uses "submitted" as a check value */
      	/*echo <<<END_OF_FORM
      	<form action="$file" method="POST"> 
      	<input type="hidden" name="submitted" value="TRUE" /> 
      	<table>
      		<tr>
      			<td>Name:</td>
      			<td><input type="text" size="55"  name="name" /></td>
      		</tr>
      		<tr>
      			<td>Email:</td>
      			<td><input type="text" size="55"  name="email" /></td>        
      		</tr>
      		<tr>
      			<td>Subject:</td>
      			<td><input type="text" size="55"  name="subject" /></td>
      		</tr>
      	</table>
      	
      	<textarea name="body" rows="4" cols="62"></textarea><br />
      	<input type="submit" name="Submit Email" value="Submit Email" /><input type="reset" name="Reset" value="Reset This, Please." />
      			
      	</form>
      	END_OF_FORM;*/
      }
      
      
      ?>
      </body>
      </html>
      form3.php:
      Code:
      <?php	
      
      /* Sends to itself again; 
      Uses "submitted" as a check value */
      echo <<<END_OF_FORM
      <form action="$file" method="POST"> 
      <input type="hidden" name="submitted" value="TRUE" /> 
      <table>
          <tr>
              <td>Name:</td>
              <td><input type="text" size="55"  name="name" /></td>
          </tr>
          <tr>
              <td>Email:</td>
              <td><input type="text" size="55"  name="email" /></td>        
          </tr>
          <tr>
              <td>Subject:</td>
              <td><input type="text" size="55"  name="subject" /></td>
          </tr>
      </table>
      
      <textarea name="body" rows="4" cols="62"></textarea><br />
      <input type="submit" name="Submit Email" value="Submit Email" /><input type="reset" name="Reset" value="Reset This, Please." />
              
      </form>
      END_OF_FORM;
      ?>
      handle3.php:
      Code:
      <?php
      $errors = array();
      //checking for each entry
      if (empty($_POST['name'])) 
      {
      	$errors[] = "No Name Entered";
      }
      if (empty($_POST['email'])) 
      {
      	$errors[] = "No Email Entered";
      }
      if (empty($_POST['subject'])) 
      {
      	$errors[] = "No Subject Entered";
      }
      $body = stripslashes($_POST['body']); //No other error checking for the body
      $message = $body;
      //Now checking for errors
      if (empty($errors))
      {
      	//Send email; Post response
      	$name = $_POST['name'];
      	
      	$body = "From: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nSubject: " . $_POST['subject'] . "\nMessage: " . $body;
      	
      	mail($myaddy, "Form Email - " . $_POST['subject'], $body, "From: " . $_POST['email']); //sends the mail
      	
      	echo <<<EMAIL_RESPONSE
      	<h1>Email Sent</h1>
      	<p>$name, your email has been sent.</p>
      	<p>Your message was: $message</p>
      	<p><a href="$file">Send another email</a></p>
      	EMAIL_RESPONSE;
      }
      else
      {
      	//Post error page w/ link to try again
      	echo "<h1>Error!</h1>\n<p>You did not complete the form. Please address the following problems and try again.</p>\n";
      	
      	echo "<ul>\n";
      	foreach ($errors as $key => $val)
      	{
      		//Print out a line of errors
      		echo "<li>$val</li>\n";
      	}
      	echo "</ul>\n<p><a href=\"$file\">Try Again</a></p>";
      }
      
      ?>
      Last edited by maveri4201; Nov 13 '09, 07:01 PM. Reason: security

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        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:

        Code:
        // good
            echo <<<HTML
            <input>...</input>
        HTML;
        // bad
            echo <<<HTML
            <input>...</input>
            HTML;
        Mark.

        Comment

        • maveri4201
          New Member
          • Nov 2009
          • 3

          #5
          Perfect! Thank you! (............ad ding extra characters to have more than 20....)

          Comment

          Working...