weixin_33725722 2019-01-27 10:38 采纳率: 0%
浏览 34

在html中使用php文件[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page" dir="ltr">PHP code is not being executed, instead code shows on the page</a>
                            <span class="question-originals-answer-count">
                                (27 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2019-01-27 10:57:28Z" class="relativetime">last year</span>.</div>
        </div>
    </aside>

I am trying to build a website and i have my html with a menu and a scroll down content.

I am trying to add a "star review" part and I have an example in php connected to a database that i imported in my localhost. The thing is, i know php does not work in html but is there any way I can call in my html the php file and the content of the php file will appear in my website?

I tried to put in my html the php content, and everything is displayed in my website but it is not working. I created a .htaccess file with the line AddType application/x-httpd-php .htm .html in it. I think the config.php which is connectig to my database is not working. I hope I made myself clear. I will put some code here now, to make a clear image of my project

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

Now for the database I am using XAMPP

I tried to copy the content of the star rating php file in my html file, everything is showing properly but it does not calculate the star rating and I think the problem is either it is not connected to my database or the problem is that I am using php in my html file.

Thanks a lot

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33699914 2019-01-27 10:52
    关注

    You can try with different code. Try adding those code in .htaccess. Try one by one. First try solution 1; if it do not work, try solution 2; and so on.

    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

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况