0

I am trying to create a button that onclick would pass a mysqlquery for inserting some data into a testtable. My script looks like this:

<?php 
            function AddCount(){
                $con = mysqli_connect(connectvalues); 
                $query = "INSERT INTO testtable VALUES (1);";
                mysqli_query($con, $query);  
            };
        ?>
        <button style"button" onlick="AddCount()">Click me</button>

However it doesn't work. Any tips? PS. I'd like to use that simple script as an example for a bigger script that I'm going to use in my online shop project. So the function in the next button I'll make will be more complicated but right now I only need a pattern to follow. Thank you in advance!

  • 2
    Javascript runs on the client-side (the browser of the user) while the PHP runs on your server. This is not gong to work like this. – Ivar Apr 24 '16 at 16:05
  • 1
    You need to research AJAX – RiggsFolly Apr 24 '16 at 16:08
  • Do you have any suggestions of how can I do it then? I'm fine with using JavaScript for it, however I don't have much knowledge about it. Should I then write javascript that forces a php to post a qeury? I'm lost – Janek Sławiński Apr 24 '16 at 16:09

1 Answers1

0
function AddCount(){
    var ajax = new XMLHttpRequest();
    ajax.open('POST','ajax.php',true);
    ajax.send();
}

in ajax.php

<?php 

          $con = mysqli_connect(connectvalues); 
          $query = "INSERT INTO testtable VALUES (1);";
           mysqli_query($con, $query);  

?>