<button onlick="myFunction()">Click me</button>
<?php
function myFunction() {
$con = mysqli_connect("mysql.hostinger.no", "u452849516_altge", "password", "u452849516_altge");
$query = "SELECT * FROM altbruker ORDER BY RAND() LIMIT 1";
mysqli_query($con, $query);
$results = mysqli_query($query);
var_dump($results);
}
?>
Asked
Active
Viewed 57 times
-2

DarkBee
- 16,592
- 6
- 46
- 58

Abbishek Shiva
- 33
- 7
-
Could you please explain what you are trying to do better? And where are you having issues? – Sam Oct 22 '17 at 14:07
-
I have improved my post, Samuel. – Abbishek Shiva Oct 22 '17 at 14:09
-
Still do not see where the problem you are having comes from... Do you get any errors or anything along those lines? – Sam Oct 22 '17 at 14:10
-
You better change your password now as well – DarkBee Oct 22 '17 at 14:11
-
wops, changed the password now haha – Abbishek Shiva Oct 22 '17 at 14:12
-
And i don't get any errors. When i click on the button now, nothing happens. – Abbishek Shiva Oct 22 '17 at 14:12
2 Answers
2
you mixed up ! from html element onclick attribute you can't fire php function ! you should make request via ajax to server and in response send data from mysql for example : in index.html make ajax request to server.php in server.php connect to database and return data simple ajax example : https://stackoverflow.com/a/5298448/2210325

Farhad
- 1,873
- 17
- 28
0
js on click cant call a php code you can use AJAX to do that as the following
<button>Click me</button>
$("button").click(function(){
$.ajax({url: "the_php_page.php", success: function(result){
alert(result);
}});
});
in the the_php_page.php do the query
$con = mysqli_connect("mysql.hostinger.no", "u452849516_altge", "password", "u452849516_altge");
$query = "SELECT * FROM altbruker ORDER BY RAND() LIMIT 1";
mysqli_query($con, $query);
$results = mysqli_query($query);
var_dump($results);

Abdallah Elsabeeh
- 576
- 3
- 15
-
Now i get this error: "Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$' in /home/u452849516/public_html/index.php on line 24" – Abbishek Shiva Oct 22 '17 at 14:24
-