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

时间:2015-06-29 05:34:54

标签: javascript php jquery ajax twitter-bootstrap

我正在使用php的管理面板。

  • 我的想法是在管理面板中添加一个商店,它应该在数据库中添加商店。在提交后的管理面板中,它应该显示4个按钮,如图所示!

为此我已经写了一些HTML,但主要的问题是如果我点击show stores它应该添加一个php include并在下面显示像enter image description here但问题是没有点击show stores php iclude is默认启用。只有当我按下show stores按钮时,才能帮我包含php文件。

<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>

我也在这里包括showstores.php文件

<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>

回答1后的控制台日志 enter image description here

1 个答案:

答案 0 :(得分:3)

有很多选项,其中一个包括发送ajax请求,在这种情况下非常简单的例子你可以创建新的include.php文件

<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');
?>