Unexpected [

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kydude1957
Forum Commoner
Posts: 31
Joined: Sun Sep 14, 2014 1:37 am

Unexpected [

Post by kydude1957 »

Im getting an unexpected [ error on line 4 of the following code. What am I missing?

Code: Select all

<div class='mainbody'>
<?php
if($_POST=['submit']){
    if(empty($_POST)['name']) || (empty($_POST)['email'])  || (empty($_POST)['message'])) {
        $error = 'true';
         else {
            
            $to = 'test@gmail.com'; 
                
            $name =trim($_POST['name']);
            $email =trim($_POST['email']);
            $message =trim($_POST['comments']);
            
            $subject = "Contact Form";
            
            $messages = "Name: $name \r\n Email: $email \r\n Comments: $comments";
            $headers = "From:" , $name;
            $mailsent = mail($to, $subject $messages $headers);
            
            if($mailsent){
                $sent = true;
            }
            
        }
            
        }
    }

?>

<!DOCTYPE html PUBLIC "-//WC3/DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtmll-transitional.dtd">
<html xmlns="http://www.w3c.org/1999/xhtml">
<head>
<title>Some Title</title>
<meta http-equiv="content-type" content="text/html" charset="UTF-8" />

<link rel="stylesheet" href="layout/css" />

</head>

<body>
     <div id="wrapper">
          <h1>A Contact Form</h1>
          <div id="contactform">
          <form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <label for="name">Name:</label>
                <input type="text" name="name" />
                <label for="email">Email:</label>
                <input type="text" name="email" />
                <label for="comments">Comments:</label>
                <textarea name="comments" cols="15" rows="10"></textarea>
                <input type="submit" name="submit" class="submit" value="Submit" />
          </form>
          <div style="clear: both;"></div>
          </div>
     </div>

</body>
</html>
</div>
phpdeveloper1
Forum Newbie
Posts: 19
Joined: Tue Aug 12, 2014 6:13 am
Location: Chennai, India

Re: Unexpected [

Post by phpdeveloper1 »

if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
Chris, Php Developer and Programmer,
https://www.phpfreelanceprogrammer.com/
kydude1957
Forum Commoner
Posts: 31
Joined: Sun Sep 14, 2014 1:37 am

Re: Unexpected [

Post by kydude1957 »

Thanks I see it was the spaces. Now it says the else right below is unexpected.
kydude1957
Forum Commoner
Posts: 31
Joined: Sun Sep 14, 2014 1:37 am

Re: Unexpected [

Post by kydude1957 »

ok got it sorted out with no errors. now, the php tags and the html doesnt look right inside the 'mainbody' div tags. in other words, if you use something like notepad++ the php tags are usually red not black. So the question is will this even work as is?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unexpected [

Post by Celauran »

kydude1957 wrote:So the question is will this even work as is?
That's easy enough to test.
Post Reply