-1

I know this question has repeated in stack overflow but i'm asking this question as i didn't got a correct answer from trying those.i'm using codeigniter and i have a form where user can rate a product. What i need to know is,when a star button is clicked and submit i need to insert into db whether which star button is clicked.If 5th button is clicked it'll display "Excellent" text. If 4th button is clicked it'll display "good" text. So i need the users' rating to be inserted into the db.So please guide me how can i do that?

 <form name="myForm7" method="post" action="<?php echo 'http://localhost/ci/businessRateCtrl/insertIntoBusinessReview/'. $Vehicleid;  ?>" >



 <h1><div class="rating" style="width:200px;float:left;padding-left:1px">
                    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
                                <span class="rate-star" data-rate="Excellent">&bigstar;</span> 
                                <span class="rate-star" data-rate="Good" >&bigstar;</span> 
                                <span class="rate-star" data-rate="Okay" >&bigstar;</span>
                                <span class="rate-star" data-rate="Unsatisfied" >&bigstar;</span>
                                <span class="rate-star" data-rate="Terrible" >&bigstar;</span>
                                </div>
                            </h1>


                              <div style="float:right;padding-right:450px">
                            <h3><label id="rateText" name="lblrating"></label></h3>

 <script type="text/javascript">
             var rateText;
                    window.onload = function() {
                    var starList = document.getElementsByClassName('rate-star');  
                    var numOfStars = starList.length;

                        for(var i = 0; i < numOfStars; i++) {
                            starList[i].addEventListener('click', starClickHandler, false);
                        }
                    }

                        function starClickHandler(event) {
                            var star = event.currentTarget;

                            setActive(star, false);
                            setActive(star, true);
                            document.getElementById('rateText').textContent = star.getAttribute('data-rate');

                               rateText=document.getElementById('rateText').textContent 
                        document.getElementById('rating').value = rateText;

                        }

                        function setActive(element, isActive) {
                            if(isActive) {
                            element.classList.add('active');
                            element.nextElementSibling && setActive(element.nextElementSibling, isActive);
                            }
                            else {
                            element.classList.remove('active');
                            element.previousElementSibling && setActive(element.previousElementSibling, isActive);
                            }
                            }

                             $(document).ready(function(){
                            $(".rating .rate-star").click(function(){
                            $(".active").removeClass("active");
                            $( ".rate-star:lt("+($(this).index()+1)+")" ).addClass("active");
                            $("#rateText").html($(this).data("rate"));
                            $("#submitreview").removeAttr("disabled");
                            });

                            });



                        function cancelConfirm(){
                            return confirm("Are you sure you want to cancel and leave this page?");
                        }

            </script>

            <style type="text/css">

                 .active{

                            color:yellow;
                                }

                       #rateText{

                    text-align:right;

                        }
                        .rating {
                            unicode-bidi: bidi-override;
                            direction: rtl ;
                                }


                                .rating > .rate-star.active,
                                .rating > .rate-star:hover,
                                .rating > .rate-star:hover ~ .rate-star {
                                     color: #FFFF00;
                                    cursor: default;
                                    }





            </style>

From below code it assign the text which is belong to clicked button , into rateText label.now i need that text to be pass to my controller through this forms' action attribute( as a parameter). Can anyone suggest me a way to solve this problem.

document.getElementById('rateText').textContent = star.getAttribute('data-rate');

and i thought of a way i don't know whether it's correct .and below i've mentioned the added part in strong text. If this is correct I want to know how to get this javascript variable into the forms' action attribute as a parameter to send it to the controller.

               **var rateText;**
                window.onload = function() {
                var starList = document.getElementsByClassName('rate-star');  
                var numOfStars = starList.length;

                    for(var i = 0; i < numOfStars; i++) {
                        starList[i].addEventListener('click', starClickHandler, false);
                    }
                }

                    function starClickHandler(event) {
                        var star = event.currentTarget;

                        setActive(star, false);
                        setActive(star, true);
                        document.getElementById('rateText').textContent = star.getAttribute('data-rate');
                          **rateText=document.getElementById('rateText').textContent** 

                    }

P.S I added the code as you have mentioned here .But it is refresh the page and the same page is displaying. and in the url it shows something like this

http://localhost/ci/businessRateCtrl/loadReviewPage/base_url();?%3EbusinessRateCtrl/insertIntoBusinessReview/base_url();

in forms' action attribute i have used only action="businessRateCtrl/insertIntoBusinessReview/'.$Vehicleid;?>" above code. but i don't know how it is appending http://localhost/ci/businessRateCtrl/loadReviewPage/ into the url. Please anyone help me ? I was searching a solution for this hours now.

mad_ss
  • 61
  • 2
  • 10
  • 1
    From a data structure standpoint you are much better off using a numbered rating system, it's one to five stars so making ratings an integer between 1-5 is efficient. Display whatever text you want based on that; calculating the average rating and things like that will be unnecessarily complex in the direction you are currently going though. Also calculating how many stars to show if you are in fact showing the filled in stars to represent what's selected. Numbers are easier. – skrilled Apr 06 '16 at 21:17

2 Answers2

1

It is unclear to me what you are doing right now. you can either set a hidden input which you then submit, which seems what you are trying to do?

Or you can do an ajax request which wouldn't require to do a submit, meaning no refresh which usually gives a much better user experience

Read more about ajax requests here: http://api.jquery.com/jquery.ajax/

P.S. there are examples at the bottom of the page.

For submitting it through a form, ad a hidden input to your form:

<input type="hidden" id="rating" value="" />

and then when you want to assign its value in your function:

function starClickHandler(event) {
                    var star = event.currentTarget;

                    setActive(star, false);
                    setActive(star, true);
                    document.getElementById('rateText').textContent = star.getAttribute('data-rate');
                    rateText=document.getElementById('rateText').textContent 
                    document.getElementById('rating').value = rateText;

                }

I think that should work, as others have said it's probably better to use numeric values instead of strings to save in your database

Jester
  • 1,408
  • 1
  • 9
  • 21
  • When user click star buttons, think user clicked 5th star button then in front of that, a label will display saying "Excellent".Now what i want to do is to when form is submitted ,i need to take whatever "Excellent" or a data regarding that rating to take into the controller. then i can insert into the db.If that successful , then i can view the rating in another page even.that is what i need to do . hope you can clear it out now? for me actually i didn't understand what you said "you can either set a hidden input " like this here – mad_ss Apr 06 '16 at 21:30
  • I understand what you want, so what have you tried? like i said you can add an input with `type="hidden"` and set the value just like you did text element that shows it to the user. – Jester Apr 06 '16 at 21:32
  • I kindly requesting from you to explain it with a code. i don't have any idea to how to do it.Please Kindly consider about it – mad_ss Apr 06 '16 at 22:20
  • http://stackoverflow.com/questions/7764154/pass-a-javascript-variable-value-into-input-type-hidden-value i'll add a short example to answer, but it's same as there. (going to sleep after) – Jester Apr 06 '16 at 22:22
1

can I suggest use jquery and ajax call instead of javascript? with ajax call you can get the value of your stars and pass to a php page for save it into a database witouth reloading the page here is a quick example:

html your is ok

jquery

    $('.rate-star').click(function(){
        var ratestar=$(this).data('rate');
            $.ajax({
                type: "POST",
                url: "../ajax/ajax-rate.php",//here your php page
                cache: false,
                dataType: "json",
                data: {
                    rate: ratestar              
                },
                success: function (data) {
                    //if the ajax is done correctly enter in success   
                  if(data.error==false){
                    $('#rateText').html(ratestar);
                  }else{
                     //handle your error
                   }
                },
                error: function (e, t, n){
                //if there are some problems with the ajax request enter here
                }
            });
        });

php ajax-rate.php

<?php
if(isset($_POST['rate'])){
$rate=$_POST['rate'];
//now save it into the db as you usually do
$message['message']='some message if you need';
$message['error']=false //this is not the error ajax function but it can helps you in the susseccs ajax function to handle errors
echo json_encode($message);
}
?>

here some docs http://api.jquery.com/jquery.ajax/

Ruggero
  • 216
  • 1
  • 5
  • 15
  • Now i'm using codeigniter. So this ajax-rate.php is a controller here?, can you please correct me? and for url you have used this.that two dots (..) means i need to add their something? and also in my forms' action attribute i need to call to ajax-rate.php and then after if condition if(isset($_POST['rate'])){ $rate=$_POST['rate']; i need to call to the controller which is responsible for inserting the data into db. These question may be somewhat silly but can you kindly consider about it and explain it. as I'm a beginner to programming. – mad_ss Apr 07 '16 at 07:08
  • 1
    yeah in my code i don't consider code igniter because i don't use it but you can adapt it. I use the two dots only because the html page isn't in the same root of the ajax-rate.php so you have to remove all my path and add your padd using the function url of codigniter.There is no form on click of an element with the class rate-star the ajax call start.Yes after $rate.. use your code for insert the variavble rate into the database.I don't now exactly if you have to consider ajax-rate as a controller because is a simple page of php that run aside and than respond – Ruggero Apr 07 '16 at 07:43
  • 1
    search some example like this one http://www.formget.com/codeigniter-jquery-ajax-post/ hope it helps – Ruggero Apr 07 '16 at 07:56
  • to url do i need to give the path to the ajax-rate.php? – mad_ss Apr 07 '16 at 07:58
  • 1
    yeah use base_url() function of codeingiter ond use the right path – Ruggero Apr 07 '16 at 08:11
  • Thank you very much for helping me by replying to my questions. I'm sooo grateful to you :-) – mad_ss Apr 07 '16 at 08:32
  • Still this didn't give solved my problem . Seems like the code you have put here didn't work .bcoz i removed form tag and then after putting above code to my code , I tried to submit the form even it doesn't submit .Then again when i put form tag as earlier it submits the form but it telling ($_POST['lblrating']) lblrating is undefined . how can i solve this please help me – mad_ss Apr 07 '16 at 14:11
  • In your html code i didn't see any form...anyway you don't need to submit any form.The ajax call start when you click on star class. – Ruggero Apr 07 '16 at 15:26
  • Ihaven't added that form tag here before, now i edited the question – mad_ss Apr 07 '16 at 17:10
  • here is a fiddle clearly the ajax call doesn't work here because is not supported(fiddle can't find the ajax-rate page) but I paste the php code of ajax-rate in the php sections to let you understand. Iadd also other comment start from the javascript section https://jsfiddle.net/032794sn/ – Ruggero Apr 07 '16 at 17:46
  • fixed the ratestar variable acquisition – Ruggero Apr 07 '16 at 17:47