PHP 创建表然后允许用户单击按钮从表中获取信息

标签 php mysql database html-table

我创建了一个 HTML 页面,它使用 PHP 从数据库中获取数据并将其存储到创建的表中。我怎样才能得到它,以便当用户单击一个按钮时,它将使用 php 根据表中的行执行特定任务?

我想从数据库中获取信息并在 HTML 表格中创建新行。在这些行中将是来自数据库的数据,每行上还有一个按钮。当用户单击该按钮时,我希望它使用由变量 $nam 表示的指定“名称”调用一个函数。

<?php
    ob_start();
    session_start();
    require_once 'dbconnect.php';
    if( !isset($_SESSION['user']) ) {
        header("Location: index.php");
        exit;
    }
    $deny = array("222.222.222", "333.333.333");
    if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
       header("location: http://www.google.com/");
       exit();
    }
    $res=mysqli_query($con,"SELECT * FROM users WHERE userId=".$_SESSION['user']);
    $userRow=mysqli_fetch_array($res);
?>
<!DOCTYPE html>
<html>
    <?php header("Access-Control-Allow-Origin: http://www.py69.esy.es"); ?>
    <head>
        <title>ServiceCoin</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"  />
        <link rel="stylesheet" href="scripts/home/index.css" />
    </head>
    <body>
        <center>
        <h3>Welcome, <?php echo $userRow['userName']; ?>. You Currently Have <span id="services"><?php echo $userRow['userServices']; ?></span> Services</h3>
        <p id="error"></p>
        <button onclick="send_coins()" class="button">Send Coins</button>
        <button onclick="create_service()" class="button">Create A Service</button>
        <button onclick="send_coins()" class="button">My Services</button>
        <h3>View Services</h3>
        <span><?php 
        $users = 1;
        $num = 1;
        echo "<center><table width=1000><th>Buy</th><th>Service Name</th><th>Service Cost</th><th>Service Description</th><th>Service Provider Name</th>";
        while($users < 100 && $num < 100){
            $res=mysqli_query($con,"SELECT * FROM users WHERE userId=".$users);
            $userRow=mysqli_fetch_array($res);
            $id = 0;
            while($id != 10){
                $id = $id + 1;
                if($userRow['userService'.$id] === 'null'){
                }else if(!empty($userRow['userService'.$id])){
                    echo "<tr><td name=$num ><input type=submit value=buy ></td><td class=services width=250 align=center>".$userRow['userService'.$id]."</td><td class=services width=250 align=center>".$userRow['userServiceCost'.$id]."</td><td class=services class=services width=500 align=center>".$userRow['userServiceDes'.$id]."<td class=services width=500 align=center>".$userRow['userServiceName'.$id]."</tr>";
                    //echo $id."Error: ".$con->error;
                    $num = $num + 1;
                    $id = $id + 1;
                }
            }
            $users = $users + 1;
        }
        echo "All Services Loaded";
        echo "</table></center>";
        ?></span>
            <span class="text-danger"><?php echo $msg; ?></span>
        </center>
    </body>
    <script lang="text/javascript" src="scripts/home/index.js"></script>    
</html>
<?php ob_end_flush(); ?>

最佳答案

首先,如果不使用名为 Ajax 的东西,就无法在用户操作上调用 PHP 函数。 .这是因为一旦将 HTML 代码提供给用户的 Web 浏览器,它就是客户端,单击按钮将不允许您从服务器请求信息(除非它是新页面),除非您通过 Javascript 调用包含功能代码的 php 文件(使用 Ajax)。

例如,创建一个“functions.js”文件并插入以下代码:

function FuncName(NameOfRow){
                $.post('phpfunctionfile.php', {NameOfRow: NameOfRow}, function(data) { 
                     if(data==1){
                         //Some sort of code, maybe display a modal with some sort of control panel to do something.
                     }
                }
}

然后在 php 文件中编写您的函数代码并回显您想要的任何内容作为变量“数据”(echo $data;)。当然,您可以重定向到此文件中的新页面等(我以前从未尝试过,但它应该可以工作)。

现在在原始代码中假设您正在根据 userServiceName'.$id 调用每个函数,如果它是您表中的唯一键,您希望使用此值作为参数调用您的 Javascript 函数。我没有测试过以下代码,我不确定它是否真的是您想要的,但您应该从以下代码中获得灵感,然后能够根据您正在创建的内容对其进行调整。

echo "<tr><td name=$num onClick="FuncName('".$userRow['userServiceName'.$id]."' ><input type=submit value=buy ></td><td class=services width=250 align=center>".$userRow['userService'.$id]."</td><td class=services width=250 align=center>".$userRow['userServiceCost'.$id]."</td><td class=services class=services width=500 align=center>".$userRow['userServiceDes'.$id]."<td class=services width=500 align=center>".$userRow['userServiceName'.$id]."</tr>";

如果这还不够清楚,请说明。我很乐意为您提供我的一些代码,这些代码将更好地展示我之前是如何做到的。

关于PHP 创建表然后允许用户单击按钮从表中获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40637437/

相关文章:

php - 为什么要清理 POST 和 GET

mysql 加入条件行为

mysql - 数字列上基于函数的索引

php - 连接3张表获取数据

sql - 为什么要在数据库中创建 View ?

mysql - 完全外连接中的语法错误?

ruby-on-rails - 在 ruby​​ on rails 中将数组存储在数据库中

php - 如何将同一个订单号的产品价格相加?在 codeigniter 中加入查询

带有命名空间和属性的 XML for PHP

php - php 代码不适用于创建验证码