使用php在html表中显示mysql行内容 [英] Showing mysql rows content in html table using php

查看:126
本文介绍了使用php在html表中显示mysql行内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,我有一个问题,我到处寻找解决方案,但仍然无法解决这个问题。



我想显示结果在使用JS的表(html)中的SELECT语句(在PHP中)。以下是这3个文件的代码:

main.HTML文件

 < HTML> 
< head>
< meta charset =UTF-8/>
< script type =text / javascriptsrc =jquery.js>< / script>
< script src =select.jstype =text / javascript>< / script>
< / head>
< body>
< button id =button>显示器< /按钮>
< br>
< input type =textid =id/>
< div id =content>< / div>
< / body>
< / html>

select.PHP文件

 <?php 
$ link = mysqli_connect(xxxxxxx,user_tienda,%%%%%%%%%,pool_tiendas) ;

if(mysqli_connect_errno())
echoFallo 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)或die(error);
echo< table border ='2px'>< tr>< td> cod< / td>< td> nmbre< / td>< td> drccn< / td< ;
while($ row = mysqli_fetch_array($ show)){

echo< tr>< td> 。$行[ '鳕鱼'] < / TD>< TD> 中。$行[ 'nmbre']。 < / TD>< TD> 中$行[ 'drccn']。 < / TD>< / TR>中;
}
echo< / table>;
}
?>

select.JS文件

  $(document).ready(function(){
$(#button)。click(function(){

函数show_all(){
$ .ajax({
类型:POST,
url:select.php,
data:{action:showroom} ,
success:function(data){
$(#id)。hide();
$(#content)。html(data);
}
});
}

show_all();
});
});

问题是当我点击按钮来显示内容时什么也没有发生。



选择statemnt是正确的,在Mysql字体中我可以看到SELECT语句的结果。


这是我得到的,如果我直接运行select.php:
codnmbredrccn; while($ row = mysqli_fetch_array($ show)){echo。$ row ['cod' ]。。$ row ['nmbre']。。$ row ['drccn']。;} echo;}?>



现在在mozilla控制台中,我可以看到2个错误:没有se encuentra elemento select.php:18和没有se encuentra elemento main.html:18(没有se encuentra elemento - >无法找到元素


该按钮应该在点击时隐藏,但没有任何反应,似乎它永远不会执行js文件。

解决方案

我删除了jquery.js,并在头部插入了另一个脚本,它对我有用
我将它连接到本地主机检查它

> main.html


 < html> 
<头>
< met charset =UTF-8/>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js><<< ; /脚本>
< script src =select.jstype =text / javascript>< / script>
< / head>
< body>
< button id =button>显示器< /按钮>
< br>
< input type =textid =id/>
< div id =content>< / div>
< / body>
< / html>

select.js

  $(document).ready(function(){
$(#button)。click(function(){

函数show_all(){
$ .ajax({
类型:POST,
url:select.php,
data:{action:showroom},
success:function(data){
$(#id)。hide();
$(#content)。html(data);
}
});
}

show_all();
});
});

select.php

 <?php 
$ link = mysqli_connect(localhost,root,,manishYii);

if(mysqli_connect_errno())
echoFallo en la conexion con mysql.mysqli_connect_error();

$ action = $ _ POST [action];
if($ action ==showroom){
$ query =从成员中选择名字,姓氏,EmailID;
$ show = mysqli_query($ link,$ query)或die(error);
echo< table border ='2px'>< tr>< td> cod< / td>< td> nmbre< / td>< td> drccn< / td< ;
while($ row = mysqli_fetch_array($ show)){

echo< tr>< td> 。$行[ '姓'] < / TD>< TD> 中。$行[ '名字'] < / TD>< TD> 中。$行[ 'EMAILID']。 < / TD>< / TR>中;
}
echo< / table>;
}
?>

输出


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

<html>
<head>
    <meta charset="UTF-8"/>
    <script type = "text/javascript" src="jquery.js" ></script> 
    <script src="select.js" type="text/javascript"></script>
</head>
<body>
<button id="button"> Mostrar </button>
<br>
<input type="text" id="id" />
<div id="content"></div>
</body>
</html>

select.PHP file

<?php
$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 "<table border='2px'><tr><td>cod</td><td>nmbre</td><td>drccn</td</tr>";
    while ($row = mysqli_fetch_array($show)) {

        echo "<tr><td>" .$row['cod']."</td><td>".$row['nmbre']."</td><td>".$row['drccn']."</td></tr>";
    }
    echo "</table>";
}
?>

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

<html>
    <head>
        <meta charset="UTF-8"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="select.js" type="text/javascript"></script>
    </head>
    <body>
    <button id="button"> Mostrar </button>
    <br>
    <input type="text" id="id" />
    <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();
                        $("#content").html(data);
                    }
                });
            }

            show_all();
        });
    });

select.php

<?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 "<table border='2px'><tr><td>cod</td><td>nmbre</td><td>drccn</td</tr>";
    while ($row = mysqli_fetch_array($show)) {

        echo "<tr><td>" .$row['FirstName']."</td><td>".$row['LastName']."</td><td>".$row['EmailID']."</td></tr>";
    }
    echo "</table>";
}
?>

Output

这篇关于使用php在html表中显示mysql行内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆