1

I'm currently working on a comment section for products review. It is a form that sends back data to PHP will display the comments and all of the comments will be sent to MYSQL database but I got this error:

Parse error: syntax error, unexpected 'var' (T_VAR) in C:\Users\USER\Desktop\XAMPP\htdocs\ecom\resources\functions.php on line 390

Any help is appreciated, thank you.

These are my codes:

Functions.php

function post(){

  LINE 390:: var comment = document.getElementById("comment").value;
  var name = document.getElementById("username").value;
  if(comment && name)
  {
    $.ajax
    ({
      type: 'post',
      url: 'post_comment.php',
      data: 
      {
         user_comm:comment,
         user_name:name
      },
      success: function (response) 
      {
        document.getElementById("all_comments").innerHTML=response+document.getElementById("all_comments").innerHTML;
        document.getElementById("comment").value="";
        document.getElementById("username").value="";

      }
    });
  }

  return false;
}

Item.php

<div id="all_comments">
      <?php
      post();

      ?>

  <?php

    $comm = mysql_query("select name,comment,post_time from comments order by post_time desc");
    while($row=mysql_fetch_array($comm))
    {
      $name=$row['name'];
      $comment=$row['comment'];
      $time=$row['post_time'];
    ?>

    <div class="comment_div"> 
      <p class="name">Posted By:<?php echo $name;?></p>
      <p class="comment"><?php echo $comment;?></p> 
      <p class="time"><?php echo $time;?></p>
    </div>

    <?php
    }
    ?>
  </div>
Phlox
  • 11
  • 2
  • What is line 390? – Haley Mueller Mar 28 '19 at 18:31
  • Is that all of your code? Are there any includes that you might have omitted? – gmiley Mar 28 '19 at 18:31
  • 2
    Isn't that Javascript in your PHP file? – Nigel Ren Mar 28 '19 at 18:31
  • You can't call a javascript function in php code. – Daan Mar 28 '19 at 18:31
  • If you're going to write javascript in your php file, make sure it's outside of the PHP block. Of course, you won't be able to use it as a PHP function, meaning `` will fail. – aynber Mar 28 '19 at 18:33
  • mysql_* functions are deprecated as of PHP 5.5.0, and removed as of PHP 7.0.0. Switch your code to use [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) or [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. Be sure to use prepared statements and parameter binding, so **you'll never have to worry about quoting issues again.** – aynber Mar 28 '19 at 18:34
  • functions.php is not PHP code but rather is JavaScript. If you try to parse it as if it were PHP, then you will get weird results. Try renaming it functions.js and then importing that file. I got the same error message too when I ran this javascript code as if it were PHP:` – slevy1 Mar 28 '19 at 18:55

0 Answers0