设置并获取按钮ID

时间:2014-09-15 19:10:48

标签: javascript php jquery html mysql

我想在将其实现到我正在构建的网站之前获得一项功能。

现在的想法是当按下index.php页面中的按钮时,test.php页面将根据已存储的按钮ID(TableID)进行过滤。存储的按钮id应该通过php会话传递到test.php页面,其中test.php中的mysql查询获取传递的按钮id(TableID)。然后相应地过滤test.php页面。

示例:

1)用户点击测试按钮

2)存储的按钮ID传递给$ _Session

3)然后将用户重定向到test.php

4)在test.php页面内,页面将根据存储的按钮ID进行过滤

因此,如果按下测试测试2按钮,下面的test.php页面图像将显示以下内容:

  • 测试2
  • 测试2持有人 - 如果没有显示,那么他们就是错误。

数据库

enter image descrion here

**

它index.php

**

enter image description here

代码到目前为止

<?php 

    session_start();

?>

<!DOCTYPE html>
<html class="full" lang="en">
<!-- Make sure the <html> tag is set to the .full CSS class. Change the background image in the full.css file. -->

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Index Page </title>

     <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="css/main.css" rel="stylesheet">
    <link href="css/images.css" rel="stylesheet">
    <link href="css/text.css" rel="stylesheet">
    <link href="css/buttons.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>

    <?php 

        include("php/connection.php");

    ?>

    <div class="container">

        <br/>

        <div class="row">
            <div class="col-lg-12">
                <h2 align="center">Page Title</h2>
            </div>
        </div>

        <BR/>
        <!-- /.row -->

        <div class="row">

                    <?php

                        $data = mysql_query("   SELECT *
                                                FROM tableTest

                                              ") or die(mysql_error());

                                    while($info = mysql_fetch_array( $data )) 

                                    {  

                                        echo "<div class=\"col-lg-3 col-md-3 col-sm-3\">\n";
                                            echo "   <div class=\"thumbnail well\">\n";
                                            echo "        <div class=\"thumbnail-pad\">\n";

                                            echo "           <br/>\n";

                                            echo "           <h4 align=\"center\">\n" . $info['testName'] . "</h4>\n";

                                            echo "           <br/>\n";

                                            echo "           <div align=\"center\"> <a href=\"test.php\" class=\"btn btn-success btn-md\" type=\"submit\">View League</a> </div>\n";

                                            echo "         </div>\n";
                                            echo "    </div>\n";
                                        echo " </div>\n";

                                    }
                                ?>
      </div>

    </div>
    <!-- /.container-->

    <!-- jQuery Version 1.11.0 -->
    <script src="js/jquery-1.11.0.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>

</body>

</html>

**

  • test.php的

**

  • 测试页面图片

enter image description here

代码

<?php
    // start session
    session_start(); 

?>


<!DOCTYPE html>
<html class="full" lang="en">
<!-- Make sure the <html> tag is set to the .full CSS class. Change the background image in the full.css file. -->

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Test Page</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="css/main.css" rel="stylesheet">
    <link href="css/images.css" rel="stylesheet">
    <link href="css/text.css" rel="stylesheet">
    <link href="css/buttons.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>

<body>

    <?php 

      include_once("php/connection.php");

    ?>

    <div class="container">    
      <div class="row">
        <div class="col-lg-12">

          <div class="col-lg-3">
            <?php include_once("sideNav.php");?>
          </div>

          <div class="col-lg-12" style="width: 0 auto;">


          <div class="section1 well">
            <div class="row">

            <?php

            $data = mysql_query(" SELECT testName , testBio                                                 
            FROM tableTest
            WHERE tableID = 1
            GROUP BY tableID ") 
            or die(mysql_error());

            $info = mysql_fetch_array( $data );

            echo "<div class=\"col-lg-12 col-md-12 col-sm-12\">\n";
            echo "   <h2 align=\"center\">" .$info['testName'] . "</h2>\n";
            echo "</div>";

            echo "<div class=\"col-lg-12 col-md-12 col-sm-12\">\n";
            echo "<h5 align=\"center\"> Drescription </h5>\n";
            echo "<p align=\"center\">" . $info['testBio'] . "</p>\n";
            echo "</div>\n";     


            ?>

            <div class="col-lg-12" align="center">
            <a href="index.php" align="center" class="btn btn-success btn-md">Go Back </a>
            </div>

          </div>
        </div>  

        </div>
      </div>
    </div>

    <br/>
    <br/>
    <br/>

     <?php include_once("footer.php");?>


    <script src="js/jquery-1.11.0.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>

</body>

</html>

1 个答案:

答案 0 :(得分:1)

将每个按钮的“按钮ID”存储在隐藏输入中,形式与按钮相同。

echo "<form action=\"test.php\" method=\"POST\"><div class=\"col-lg-3 col-md-3 col-sm-3\">\n";
echo "<div class=\"thumbnail well\">\n";
echo "<div class=\"thumbnail-pad\">\n";
echo "<br/>\n";
echo "<h4 align=\"center\">\n" . $info['testName'] . "</h4>\n";
echo "<br/>\n";
echo "<input type='hidden' value='".$info['tableID']."'>";
echo "<div align=\"center\"><input type=\"submit\" class=\"btn btn-success btn-md\" type=\"submit\" value=\"View League\"></div>\n";
echo "</div>\n";
echo "</div>\n";
echo "</div></form>\n";

您需要使用FORM标记,以便服务器知道您传递给它的数据。在这种情况下,它会将值= $ info ['tableID']的隐藏ID传递给它,具体取决于您按下的按钮。

    $data = mysql_query(" SELECT testName , testBio FROM tableTest 
WHERE tableID = ".$_POST['tableID']." GROUP BY tableID ") or die(mysql_error());

希望这应该有所帮助。请记住,当从网页上获取输入时,您需要使用表单。