0

When a form titled "functions_question2" is submitted, the following PHP code should dictate what happens

<?php
if (isset($_POST['functions_question2'])) {
    if ($id) {
        $res3 = $db->query("SELECT * FROM answers WHERE user_id=$id");
        $data3 = $res3->fetch_array();
        if (isset($_SESSION['functions_question2']) && $_POST['functions_question2'] == $_SESSION['functions_question2']) {  
          if ($data3['FunctionsPercent'] > 50 && $data3['FunctionsPercent'] < 100) {
            $db->query("UPDATE answers SET FunctionsPercent = 50 WHERE user_id=$id");
            echo'<script type="text/javascript" src="insert.js"></script>'; 
            //***THIS IS THE PART THAT'S GIVING ME TROUBLE.
          }
        }
    }
}
?>

Within the external page insert.js, I have the following code:

<script type='text/javascript'>
$(document).ready(function(){
 $(".incorrectanswermark").fadeIn('slow');
});
</script>

All of the rest of the PHP executes correctly (the MySQL database updates), and there are no console errors. Also, I double-checked to make sure the div that's supposed to show up when the form submits does indeed have the class incorrectanswermark.

Any help would be much appreciated. Thanks!

Snoops
  • 215
  • 1
  • 12
  • Your code is likely vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Jun 28 '17 at 19:52
  • Just testing the echo of js and it appears to work fine. Is you condition being meant for that if? – nerdlyist Jun 28 '17 at 19:53
  • Are you sure that _id > 0_ ? – Sampgun Jun 28 '17 at 19:54
  • have you try echo the code without if condition? **if** is working without if condition – Kamarul Anuar Jun 28 '17 at 19:54
  • 4
    You shouldn't have script tags in a js file – Dan Jun 28 '17 at 19:54
  • dan08's right, it may be the issue.. – Sampgun Jun 28 '17 at 19:55
  • Thanks for your help everyone. I tried removing the script tags and now the console says that $ is undefined. I included a screenshot [link] http://imgur.com/a/QsS8s Do I need to put the external Jquery resources in insert.js? That would add script tags, though... – Snoops Jun 28 '17 at 20:01

1 Answers1

1

Pursuant to the comment from @dan08 above, the script tags in your Javascript file are likely causing the failure.

Change:

<script type='text/javascript'>
$(document).ready(function(){
 $(".incorrectanswermark").fadeIn('slow');
});
</script>

To:

$(document).ready(function(){
 $(".incorrectanswermark").fadeIn('slow');
});
Ben Shoval
  • 1,732
  • 1
  • 15
  • 20