0

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

  • Rename your index.html file to index.php – pokeybit Jan 27 '19 at 10:44
  • You can't put php in html file. Change the file extension from index.html to index.php – Iftieaq Jan 27 '19 at 10:44
  • maybe stupid question but.. when i want to see my html file, it opens nicely in my browser but when i open the php file in my browser it just shows the code. If i change my index from html to php, will it show the actual website in my browser or just the code? sorry for my stupidity – Adriana Chiricioiu Jan 27 '19 at 10:50

1 Answers1

0

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

mahfuz
  • 2,728
  • 20
  • 18
  • The thing is... The calculator for my star rating is not working and I think it may be because the connection with my database is not established. Any idea how should I do that? Or if the code is wrong.. – Adriana Chiricioiu Jan 27 '19 at 10:58
  • First be sure that, php code is working. write a simple php code then see what happens. If php works, then you can check other codes. – mahfuz Jan 27 '19 at 11:02
  • I put that line in my html but it's not working... i don't know what to do from here. I checked in my cmd php -v and it seems to be working there, i don't get an error – Adriana Chiricioiu Jan 27 '19 at 11:24
  • what is your php version? create a info.php file with this code ``, run in browser `http://localhost/info.php` and check the php version. I am using `php 7.2` in ubuntu and apache, and solution 4 works for me with two line of code in `.htaccess` – mahfuz Jan 27 '19 at 11:37