I'm new here and I have a question, I looked for the solution everywhere and I still cant manage to solve this.
I want to show the results of the SELECT statement (in php) in a table (html) using JS. Here is the code of these 3 files:
main.HTML file
Mostrar
select.PHP file
$link=mysqli_connect("xxxxxxx", "user_tienda","%%%%%%%%","pool_tiendas");
if (mysqli_connect_errno() )
echo "Fallo en la conexion con mysql" .mysqli_connect_error();
$action=$_POST["action"];
if ($action=="showroom") {
$query = "SELECT cod, nmbre, drccn from tienda";
$show = mysqli_query($link, $query) or die ("error");
echo "
cod | nmbre | drccn |
while ($row = mysqli_fetch_array($show)) {
echo "
" .$row['cod']."".$row['nmbre']."".$row['drccn']."";}
echo "
";}
?>
select.JS file
$(document).ready(function(){
$("#button").click(function () {
function show_all() {
$.ajax({
type: "POST",
url: "select.php",
data:{action:"showroom"},
success: function (data) {
$("#id").hide();
$("#content").html(data);
}
});
}
show_all();
});
});
The problem is when I click the button to show the content nothing happens.
The Select statemnt is correct, in Mysql font I can see the results of the SELECT statement.
This is what I'm getting if I directly run select.php:
codnmbredrccn"; while ($row = mysqli_fetch_array($show)) { echo "" .$row['cod']."".$row['nmbre']."".$row['drccn'].""; } echo ""; } ?>
Now in mozilla console I can see 2 errors: no se encuentra elemento select.php:18 and no se encuentra elemento main.html:18 (no se encuentra elemento -> can't find element
The button is supposed to hide when clicked, but nothing happens. Seems like it never executes the js file.
解决方案
I removed jquery.js and inserted another script at head. It worked for me.
I connected it to localhost. check it.
main.html
Mostrar
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();
$("#content").html(data);
}
});
}
show_all();
});
});
select.php
$link=mysqli_connect("localhost", "root","","manishYii");
if (mysqli_connect_errno() )
echo "Fallo en la conexion con mysql" .mysqli_connect_error();
$action=$_POST["action"];
if ($action=="showroom") {
$query = "SELECT FirstName, LastName, EmailID from members";
$show = mysqli_query($link, $query) or die ("error");
echo "
cod | nmbre | drccn |
while ($row = mysqli_fetch_array($show)) {
echo "
" .$row['FirstName']."".$row['LastName']."".$row['EmailID']."";}
echo "
";}
?>
Output