douyuefei3546 2015-06-29 05:34
浏览 46

如何通过单击按钮激活PHP包括?

I am working on an admin panel in php.

  • My idea is to add a store in the admin panel and it should add the store in database.In the admin panel after submitting it should show the 4 buttons as shown in figure!

for this i have written some html ,but main problem is if i click on show stores it should add a php include and show below like thisenter image description hereBut the problem is without clicking on show stores the php iclude is default enable.Please help me to include the php file only if i press the show stores button.

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Database connection is created with the db AskCoupon.com -->
<?php
//Step1
 if ( ! empty( $_POST ) ) {

  // Connect to MySQL
  $mysqli = new mysqli( 'localhost', 'root', 'sai123', 'askcoupon.com' );

  // Check our connection
  if ( $mysqli->connect_error ) {
    die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error );
  }
  /*New Code added here*/
  $storeName = $_POST['store_name'];

  $check=mysqli_query($mysqli,"select * from featured_stores where store_name='$storeName'");
    $checkrows=mysqli_num_rows($check);
   if($checkrows>0){echo "customer exists";}  
else{  
    //insert results from the form input
    $query = "INSERT INTO featured_stores ( store_id, store_name,no_of_coupons,caption ) VALUES ( '{$mysqli->real_escape_string($_POST['store_id'])}', '{$mysqli->real_escape_string($_POST['store_name'])}', '{$mysqli->real_escape_string($_POST['no_of_coupons'])}', '{$mysqli->real_escape_string($_POST['caption'])}' )";
    // $insert = $mysqli->query($sql);
    $result = mysqli_query($mysqli, $query) or die('Error querying database.');

    mysqli_close($mysqli);
    echo "Store Added";
    }

  };

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Admin Panel</title>
</head>
<body>
<div class="container">
  <div class="row">
    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
    <a class="btn btn-primary" href="../admin_panel" role="button">Insert Another Store</a>
    </div>
    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
     <a class="btn btn-danger" href="../admin_panel" role="button">Insert Another Coupon</a>
    </div>
    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
    <a class="btn btn-success" href="../Admin_panel/show_stores.php" role="button" onclick="loadThis">Show Stores</a>
    </div>
    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">
    <button  class="btn btn-success" href="#" role="button" onclick="loadThis">Show Coupons</button>
    </div>
  </div>
</div>

<script>
function loadThis(){
<?php include'show_stores.php';?>
}
</script>

</body>
</html>

Also I am including showstores.php file here

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<?php
// 1) connect to the database by mysql_connect()

mysql_connect("localhost", "root", "sai123") or die(mysql_error()); 
// 2) than select the database like  mysql_select_db()

mysql_select_db("askcoupon.com") or die(mysql_error()); 
// 3) you need to use mysql_query()

$data = mysql_query("SELECT * FROM featured_stores where 1")  or die(mysql_error());
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Panel</title>

</head>
<body>
<div class="container">
<div class="row well">
<?php
while($row= mysql_fetch_array( $data )){ ?>
    <div class="col-xs- col-sm- col-md-2 col-lg-">
        <?php echo $row['store_id']; ?> 
    </div>
    <div class="col-xs- col-sm- col-md-4 col-lg-">
        <?php echo $row['store_name'] ?>                                        
    </div>
    <div class="col-xs- col-sm- col-md-3 col-lg-">
        <?php echo $row['no_of_coupons'] ?>
    </div>
    <div class="col-xs- col-sm- col-md-3 col-lg-">
        <?php echo $row['caption'] ?>
    </div>
    <?php } ?>
</div>
</div> 
</body>
</html>

Console Log after answer 1 enter image description here

  • 写回答

1条回答 默认 最新

  • douqihou7537 2015-06-29 06:25
    关注

    there are many options, one of which include sending ajax request, in this situation very simple example you can create new include.php file

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>    
    
    
    function loadThis() { 
        $.ajax({              
            type: "POST",
            url: "include.php",
            dataType: "html",
            success: function(data) {}
        });
    }
    
    
    /* include.php file */
    <?php
    include('showstores.php');
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?