在html中使用php文件

时间:2019-01-27 10:39:00

标签: php html ajax

我正在尝试建立一个网站,并且我的html带有菜单和向下滚动的内容。

我正在尝试添加“星级评论”部分,并且在php中有一个示例连接到我在本地主机中导入的数据库。问题是,我知道php在html中不起作用,但是有什么办法可以在html中调用php文件,而该php文件的内容将出现在我的网站中?

我试图在html中放入php内容,并且所有内容都显示在我的网站上,但无法正常工作。我创建了一个.htaccess文件,其中包含AddType application / x-httpd-php .htm .html行。我认为连接到我的数据库的config.php无法正常工作。我希望我能说清楚。我现在将一些代码放在这里,以使我的项目清晰可见

<!DOCTYPE html>
<! This is the star rating code in a php file which i am trying to implement in my html website>
<html>
<head>
<meta charset="utf-8" />
<title>Star Rating</title>
<link type="text/css" rel="stylesheet" href="style.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

<body>
<h1 style="text-align:center; color:#fff; margin:50px; margin-bottom:10px; font-size:36px">Star Rating System Using PHP,Ajax</h1>
<p style="text-align:center; color:#fff;font-size:20px; margin:0px">Including Update Rating</p>
<?php  
include('config.php');
$post_id = '1'; // yor page ID or Article ID
?>

<div class="container">
    <div class="rate">
    <div id="1" class="btn-1 rate-btn"></div>
        <div id="2" class="btn-2 rate-btn"></div>
        <div id="3" class="btn-3 rate-btn"></div>
        <div id="4" class="btn-4 rate-btn"></div>
        <div id="5" class="btn-5 rate-btn"></div>
    </div>
<br>
    <div class="box-result">
        <?php
        $query = mysql_query("SELECT * FROM star"); 
            while($data = mysql_fetch_assoc($query)){
                $rate_db[] = $data;
                $sum_rates[] = $data['rate'];
            }
            if(@count($rate_db)){
                $rate_times = count($rate_db);
                $sum_rates = array_sum($sum_rates);
                $rate_value = $sum_rates/$rate_times;
                $rate_bg = (($rate_value)/5)*100;
            }else{
                $rate_times = 0;
                $rate_value = 0;
                $rate_bg = 0;
            }
        ?>
    <div class="result-container">
        <div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
        <div class="rate-stars"></div>
    </div>
        <p style="margin:5px 0px; font-size:16px; text-align:center">Rated <strong><?php echo substr($rate_value,0,3); ?></strong> out of <?php echo $rate_times; ?> Review(s)</p>
    </div>
</div>

<script>
$(function(){ 
   $('.rate-btn').hover(function(){
   $('.rate-btn').removeClass('rate-btn-hover');
      var therate = $(this).attr('id');
      for (var i = therate; i >= 0; i--) {
   $('.btn-'+i).addClass('rate-btn-hover');
       };
     });

   $('.rate-btn').click(function(){    
      var therate = $(this).attr('id');
      var dataRate = 'act=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
   $('.rate-btn').removeClass('rate-btn-active');
      for (var i = therate; i >= 0; i--) {
   $('.btn-'+i).addClass('rate-btn-active');
      };
   $.ajax({
      type : "POST",
      url : "ajax.php",
      data: dataRate,
      success:function(){}
    });
  });
});
</script>
</body>
</html>




<!This is the config.php - the connection to my database that is not working>
<?php 
//change the values with your own hosting setting
 $mysql_host = "localhost";
 $mysql_database = "test";
 $mysql_user = "root";
 $mysql_password = "";

 $db = mysql_connect($mysql_host,$mysql_user,$mysql_password);
 mysql_connect($mysql_host,$mysql_user,$mysql_password);
 mysql_select_db($mysql_database);
 ?>




<!ajax.php>
<?php
require_once 'config.php';

if($_POST['act'] == 'rate'){
    $ip = $_SERVER["REMOTE_ADDR"];
    $therate = $_POST['rate'];
    $thepost = $_POST['post_id'];

    $query = mysql_query("SELECT * FROM star where ip= '$ip'  "); 
    while($data = mysql_fetch_assoc($query)){
        $rate_db[] = $data;
    }

    if(@count($rate_db) == 0 ){
        mysql_query("INSERT INTO star (id_post, ip, rate)VALUES('$thepost', '$ip', '$therate')");
    }else{
        mysql_query("UPDATE star SET rate= '$therate' WHERE ip = '$ip'");
    }
} 
?>

现在使用XAMPP的数据库

我试图将星级php文件的内容复制到我的html文件中,所有内容均显示正确,但是无法计算星级,我认为问题是它未连接到我的数据库,或者是我在html文件中使用php。

非常感谢

1 个答案:

答案 0 :(得分:0)

您可以尝试使用其他代码。尝试在.htaccess中添加这些代码。一一尝试。首先尝试解决方案1;如果不起作用,请尝试解决方案2;等等。

  1. AddHandler x-httpd-php .html .htm

  2. AddHandler php-script .php .html .htm

  3. AddHandler php5-script .php .html .htm

  4. RemoveHandler .html .htm AddType application/x-httpd-php .php .htm .html