Hydanium Hydanium - 9 months ago 58
Javascript Question

How to use a .click function in jQuery

The following code should add 100 to an existing number in a mysql table if the button gets clicked. If I click the button nothing happens, but if I reload the page the function adds 100 to the number. What is wrong with my code?

define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '123');
define('DBNAME', 'dbtest');

$conn = mysql_connect(DBHOST,DBUSER,DBPASS);
$dbcon = mysql_select_db(DBNAME);

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css">
<a id="button" class="waves-effect btn deep-orange darken-1">Button 1</a>

mysql_query("UPDATE users SET test = (test + 100) WHERE userId=1");

Answer Source

As mentioned by the previous poster PHP is server side and Javascript client side so what is actually happening is the following.

When the page is returned back to the user your piece of javascript just looks like the below..

Your MySQL statement here has executed already it can not interact with client side code in this way

  // nothing here.. But your MYSQL statement has executed anyway