I'm trying to find out what's going on with the following code but i can't figure out. I have made an html page, which communicates with a php and a js file. The purpose of my example is to get the data from the server side and display them at the client side. I've made also 2 buttons. One button in order to display me all the entries, and the second button in order to display me a specific entry when a user gives a specific ID.
The problem is that syntactic is OK. It passed all the necessary checks. But it doesn't display me something and unfortunately the console does not have any problem messages. I just want to inform you that i have put these 3 files in this path C:\wamp64\www\PHP and i'm running the selectfrm.html from here: http://127.0.0.1/PHP/selectfrm.html
So, can you help me with this?
selectfrm.html:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
<script type="text/javascript" src="select.js">
</script>
</head>
<body>
<button id="button">Select All </button>
<br>
<input type="text" id="id" />
<button id="button2">Select by ID</button>
<div id="content"></div>
</body>
</html>
select.js
$(document).ready(function(){
$("#button").click(function(){
function show_all(){
$.ajax({
type: "POST",
url: "select.php",
data:{action:"showroom"},
success: function (data) {
$("#id").hide();
$("#button2").hide();
$("#content").html(data);
}
});
}
show_all();
});
});
$(document).ready(function(){
$("#button2").click(function() {
function show_selected() {
var id=$("#id").val();
$.ajax({
type: "POST",
url: "select.php",
data:{action:id},
success: function (data) {
$("#content").html(data);
$("#button").hide();
}
});
}
show_selected();
});
});
select.php:
<?php
$link=mysqli_connect("localhost", "root", "", "connection");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL:" .mysqli_connect_error();
$action=$_POST["action"];
if($action=="showroom") {
$query = "SELECT * FROM names";
$show = mysqli_query($link, $query) or die ("Error");
echo "<table border='2px'><tr><td>name_id</td><td>age</td></tr>";
while ($row = mysqli_fetch_array($show)) {
echo "<tr><td>" .$row['name_id'] ."</td><td>" .$row['name'] ."</td><td>" .$row['age'] ."</td></tr>";
}
echo "</table>";
}
else{
$query = "SELECT * FROM names WHERE name_id = '$action'";
$show = mysqli_query($link, $query) or die ("Error");
echo "<table border='2px'><tr><td>name_id</td><td>age</td></tr>";
while ($row = mysqli_fetch_array($show)) {
echo "<tr><td>" .$row['name_id'] ."</td><td>" .$row['name'] ."</td><td>" .$row['age'] ."</td></tr>";
}
echo "</table>";
}
?>
function show_all(){