Hi Guys,

I am a novice at coding, firstly I knowthe functions in my code are deprecated in future versions of PHP, I'm running 5.5. I have a few days to get my current code working with this desired functionality which is a throw away prototype, so for now I'm stuck with them, and we will be using them for this project.

I have created a rating system which was working as intended, i wanted to add a comment box so users could also comment on my videos. I have added code to the bottom of my index file which makes another AJAX request for the comment but i am not sure of the structure, I am not very familiar with AJAX etc.
I have obviously added variables and a column in my database to receive the comments. However, my ratings submit but my comments inserts nothing into my database. Could this be due to have 2 AJAX requests on one page?

Can anyone advise how I can amend my code to enable both the rating and the comment to insert into my database?

Here is the code for my 2 files.

Index.php

        <?php  
            session_start();
            include_once 'dbconnect.php';

            if( !isset( $_SESSION['user'] ) ) header("Location: index.php");

            $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
            $userRow=mysql_fetch_array( $res );

                $post_id = 1; 
            ?>

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-20" />
        <title>Welcome - <?php echo $userRow['username']; ?></title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        </head>
        <body>
        <div id="header">
         <div id="left">
            <label>NHSCT eNISAT Tutorials</label>
            </div>
            <div id="right">
             <div id="content">
                 Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="eNISATVids.php?home">Return to Tutorials page</a>&nbsp;&nbsp;<a href="logout.php?logout">Sign Out</a>
                </div>
            </div>
            <br>
            <br>
            <br>
            <br>
            <br>
        <p align="center"><img src="title.jpeg" width="400" height="100" alt="title.jpeg">
        <br>
          <link rel="stylesheet" href="style.css" type="text/css" />
                <link type="text/css" rel="stylesheet" href="css/example.css">


            <div class="tuto-cnt">

                <div class="rate-ex1-cnt">
                    <div id="1" class="rate-btn-1 rate-btn"></div>
                    <div id="2" class="rate-btn-2 rate-btn"></div>
                    <div id="3" class="rate-btn-3 rate-btn"></div>
                    <div id="4" class="rate-btn-4 rate-btn"></div>
                    <div id="5" class="rate-btn-5 rate-btn"></div>
                </div>

                <hr>

                <div class="box-result-cnt">
                    <?php
                        $query = mysql_query("SELECT * FROM enisat1_rate"); 
                        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;
                        }
                    ?>
                    <hr>
                    <h3>This tutorial has been rated <strong><?php echo $rate_times; ?></strong> times.</h3>
                    <hr>
                    <h3>The average rating is <strong><?php echo $rate_value; ?></strong> stars.</h3>
                    <hr>
                    <div class="rate-result-cnt">
                        <div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
                        <div class="rate-stars"></div>
                    </div>
                    <hr>
                    <center>
            <div id="addCommentContainer">
            <p>Leave a comment on what you thought of the tutorial</p>
            <br>
            <form id="addCommentForm" method="post" action="eNISATVids.php" >
                <div>
                    <textarea name="comments" id="comments" cols="50" rows="5"></textarea>
                <br>
                    <input type="submit" id="submitcomment" value="Submit" />
            </form>
                </div><!-- /rate-result-cnt -->

            </div><!-- /tuto-cnt -->

            <script>

             $( document ).ready(function() {

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

                    $('.rate-btn').click(function(){    
                        var therate = $(this).attr('id');
                        var dataRate = 'submit=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
                        $('.rate-btn').removeClass('rate-btn-active');
                        for (var i = therate; i >= 0; i--) {
                            $('.rate-btn-'+i).addClass('rate-btn-active');
                        };

                        $.ajax({
                            type : "POST",
                            url : "http://localhost/Tna/E1ajax.php",
                            data: dataRate,
                            success:function(){}
                        });

                    });
                });

                // javascript for handling your comment:
        $("#submitcomment").click(function() {
             var comments = $("#comments").val();
             $.ajax({
                url: "http://localhost/Tna/E1ajax.php",
                type: "POST",
                data: { "comments": comments },
                success: function(back)
                    {
                    alert(back);
                    }
                });
        });
        });  

            </script>

        </body>
</html> 

Ajax.php

    <?php

    session_start();
    include_once 'dbconnect.php';

    if(!isset($_SESSION['user']))
    {
     header("Location: index.php");
    }
    $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
    $userRow=mysql_fetch_array($res);

        if($_POST['submit'] == 'rate'){
            //search if the userID has already gave a note
            $userID=$_SESSION['user'];
            $therate = $_POST['rate'];
            $thepost = $_POST['post_id'];
            $comments = $_POST['comments'];

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

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

Thank You

The php runs on the server and the html/javascript/etc run on the client, so instead of mixing your html and php, build up your html strings in php as string variables. When ready, then output that. It is much more efficient that way. Also, you should build up php strings for your mysql queries, etc. That way you can debug/output the strings to see if they are what you expect. Finally, GET RID OF YOUR MySQL and move to mysqli apis instead! Not only are the old mysql apis deprecated, they are also very buggy!

Hi @Rubberman, do you mean split my index file into two files, HTML etc and PHP? Or to restructure my index file? If its the latter can you be more specific with how I would go about that? Perhaps with an example? I have spent weeks trying to get to this point which has been a struggle for me, it seems to be only the comment section I added which is the problem, everything to do with ratings is still working, so would rather not re-write the whole thing unecessarily if possible. Sorry I am not really familar with Javascript and this is my first time using it, can you notice anything wrong with the Javascript and/or the HTML for the comments box section?

With regards to changing to SQLI I definitely do not have time to change my whole project to these, I only have a few days left, well a matter of hours actually as I work full time and can only work on this a night as part of a course. I will use the new functions in future if I ever start another project

I meant to restructure your Index.php file. Make it pure php. Build up the HTML strings in a php variable, and then output that. Also in the Ajax.php file, build php string variables with the select, insert, update strings that mysqli (again DO NOT use the old mysql api's!) will use. Then when you are debugging your scripts, you can easily add in an output command that will display the string for you so you can verify it is what you are looking to do. I used that technique extensively at Nokia when I was building complex web applications for our QA department, including a cell phone emulator that could run in a browser.

FWIW, at least move to 5.5.4 or later for php! That way, you can run php as a web server for simpler testing purposes. You won't need an entire LAMP stack running! And can run it on any appropriate client.

I think you are underestimating my level of skills and knowledge here, I had never seen a piece of code 6 weeks ago, and have had to try and teach myself it for my project, im balding with the stress of this!! :) Thank you anyway rubberman I will try what you suggested by I dont think I will be able to implement it, and it doesnt help with my Javascript anyway which is the actual issue here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.