Parse error: syntax error, unexpected '<' in (filename)

Discussion in 'PHP' started by web_student, Apr 19, 2013.

  1. #1
    I am trying to do a simple comment function, where you write your name and comment in different fields, press submit and than the comment appears.

    In my PHP file, I have this :

    <?php

    $name = $_POST['name'];
    $comment = $_POST['comment'];

    <div>
    <div>Name is <?php echo $name ?></div>
    <div>Comment is <?php echo $comment ?></div>
    </div>


    When I upload it and press the submit button, it says:
    Parse error: syntax error, unexpected '<' in (filename) on line #

    I am assuming that the error has to do with either the division that is starting or the second <?php that starts.... What am I supposed to change?

    Thank you.
     
    web_student, Apr 19, 2013 IP
  2. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #2
    You need to close your PHP code with ?> before you start outputting HTML directly. Also, you should never, ever output user input to the page without first sanitising it. I could POST whatever I want and have it outputted on to the page with your above code. Something like this:

    <?php
    
    $name = htmlentities($_POST['name']);
    $comment = htmlentities($_POST['comment']);
    ?>
    
    <div>
    <div>Name is <?php echo $name ?></div>
    <div>Comment is <?php echo $comment ?></div>
    </div>
    PHP:
     
    Alex Roxon, Apr 19, 2013 IP
  3. Techking

    Techking Well-Known Member

    Messages:
    611
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    130
    #3
    Use this :

    
    <?php
    
    $name = $_POST['name'];
    $comment = $_POST['comment'];
    ?>
    <div>
    <div>Name is <?php echo $name ?></div>
    <div>Comment is <?php echo $comment ?></div>
    </div>
    
    Code (markup):
     
    Techking, Apr 21, 2013 IP
  4. IvinViljoen

    IvinViljoen Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    I assume you're not using a WordPress blog, right?
     
    IvinViljoen, Apr 22, 2013 IP
  5. gavo

    gavo Active Member

    Messages:
    123
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    70
    #5
    You need to sanitise user input like in Alex Roxon's code, never return raw $_POST/$_GET data.
     
    gavo, Apr 22, 2013 IP