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 submitted, I need to insert into the database whichever 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 database.
<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">★</span>
<span class="rate-star" data-rate="Good">★</span>
<span class="rate-star" data-rate="Okay">★</span>
<span class="rate-star" data-rate="Unsatisfied">★</span>
<span class="rate-star" data-rate="Terrible">★</span>
</div>
</h1>
<div style="float:right;padding-right:450px">
<h3><label id="rateText" name="lblrating"></label></h3>
<input type="hidden" id="rating" name="RatingTxt" value="" />
</div>
<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 the following code it assigns the text from the clicked button, into rateText label. Now I need that text to be passed to my controller through this form's action attribute (as a parameter).
document.getElementById('rateText').textContent = star.getAttribute('data-rate');
Is the following correct and if so, how can the rateText variable be added to the form's action attribute and sent to the controller?
<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
}
This code refreshes the page and displays the same content. It adds the following to the URL:
http://localhost/ci/businessRateCtrl/loadReviewPage/base_url();?%3EbusinessRateCtrl/insertIntoBusinessReview/base_url();
In the form's action attribute I used
action="<?php echo 'base_url();?>businessRateCtrl/insertIntoBusinessReview/'.$Vehicleid;?>"
but I don't know how is it appending http://localhost/ci/businessRateCtrl/loadReviewPage/ into the URL.