Retrieve values from checkbox ajax php javascript - php

I have this code in html:
<form id='status_service' method='post' accept-charset='UTF-8' name='status_form'>
<fieldset>
<label class="cb-enable"><span>On</span></label>
<label class="cb-disable selected "><span>Off</span></label>
<input type="checkbox" id="id_sf_checkbox" class="checkbox" value="1" name="checkbox" onChange="document.getElementById('status_service').submit(); window.location.reload(true)"/>
<label class="cb-enable"><span>On</span></label>
<label class="cb-disable selected "><span>Off</span></label>
<input type="checkbox" id="id_sf_checkbox" class="checkbox" value="2" name="checkbox" onChange="document.getElementById('status_service').submit(); window.location.reload(true)"/>
<label class="cb-enable "><span>On</span></label>
<label class="cb-disable selected "><span>Off</span></label>
<input type="checkbox" id="id_sf_checkbox" class="checkbox" value="3" name="checkbox" onChange="document.getElementById('status_service').submit(); window.location.reload(true)"/>
<label class="cb-enable "><span>On</span></label>
<label class="cb-disable selected "><span>Off</span></label>
<input type="checkbox" id="id_sf_checkbox" class="checkbox" value="4" name="checkbox" onChange="document.getElementById('status_service').submit(); window.location.reload(true)"/>
</fieldset>
</form>
It creates a iphone styled button (ON/OFF), using CSS for activate/desactivate the service (the value corresponds the id_service the user wants to modify)
I would retrieve this using this code, but, what's wrong:
$(document).ready( function()
{
$(".cb-enable").click(function()
{
var parent = $(this).parents('.switch');
$('.cb-disable',parent).removeClass('selected');
$(this).addClass('selected');
$('.id_sf_checkbox',parent).attr('checked', true);
alert("Checkbox enabled");
alert($("input[name=checkbo]:checked").map(function () {return this.value;}).get().join(","));
$('.ajaxgif').removeClass('hide');
var checkbox = new Array();
$("input:checked").each(function()
{
data['checkbox[]'].push($(this).val());
});
alert(checkbox);
$.ajax({
type: "POST",
url: "../proceso_checkboxes.php",
data: {checkbox:checkbox},
success: function()
{
$('.ajaxgif').hide();
$('.msg').text('Mensaje enviado!').addClass('msg_ok').animate({ 'right' : '130px' }, 300);
},
error: function()
{
$('.ajaxgif').hide();
$('.msg').text('Hubo un error!').addClass('msg_error').animate({ 'right' : '130px' }, 300);
}
});
});
$(".cb-disable").click(function()
{
var parent = $(this).parents('.switch');
$('.cb-enable',parent).removeClass('selected');
$(this).addClass('selected');
$('.id_sf_checkbox',parent).attr('checked', false);
alert("Checkbox disabled");
showSelectedValues();
});
$("input[type=checkbox][id=id_sf_checkbox][checked]").each(function()
{
var parent = $(this).parents(".switch");
$(".cb-enable",parent).addClass("selected");
$(".cb-disable",parent).removeClass("selected");
});
});
My objective is to send the values of the checkbox to the php, but in the javascript alerts function shows blank, no values, and i do not know what's wrong, i've been looking a solution, and tested, but stills appears blank.
Any help? thanks in advance.
Attached below the CSS code about the checkboxes:
/* Checkboxes */
span.jqTransformCheckboxWrapper {display:block;float:left}
a.jqTransformCheckbox {background:transparent url(../images/checkbox.gif) no-repeat left -30px;vertical-align:middle;height:17px;width:17px;display:block;/*display:-moz-inline-block;*/}
/* Checked - Used for both Radio and Checkbox */
a.jqTransformChecked {background-position:left top}
/* Hidden - used to hide the original form elements */
.jqTransformHidden {display:none}
/* Formulario de login-home status service form */
#status_service .cb-enable, .cb-disable, .cb-enable span, .cb-disable span { background: url(../images/switch.gif) repeat-x; display: block; float: left; }
#status_service .cb-enable span, .cb-disable span { line-height: 30px; display: block; background-repeat: no-repeat; font-weight: bold; }
#status_service .cb-enable span { background-position: left -90px; padding: 0 10px; }
#status_service .cb-disable span { background-position: right -180px;padding: 0 10px; }
#status_service .cb-disable.selected { background-position: 0 -30px; }
#status_service .cb-disable.selected span { background-position: right -210px; color: #fff; }
#status_service .cb-enable.selected { background-position: 0 -60px; }
#status_service .cb-enable.selected span { background-position: left -150px; color: #fff; }
#status_service .switch label { cursor: pointer; }
#status_service .switch input { display: none; }
/* Mod para AJAX gif */
#status_service .ultimo{ margin-bottom: 0; position: relative; }
/* AJAX Gif y mensajes de exito o fracaso */
#status_service .hide{display: none; }
#status_service .ajaxgif{position: absolute; right: 150px; top: 5px; }
#status_service .msg{color: white; font-weight: bold; height: 32px; line-height: 32px; padding: 0 10px; position: absolute; right: -155px; text-transform: uppercase; min-width: 121px; }
#status_service .msg_ok{background: #589D05; }
#status_service .msg_error{background: red; }

You are missing an x in your selector.
alert($("input[name=checkbox]:checked")
----------^

Simply make checkbox as array, it will give you all checkboxes checked in an array.
var checkbox = new Array();
$("input:checked").each(function()
{
checkbox.push($(this).val());
});
alert(checkbox);

Related

Convert jquery dropdown into checkbox feature

I am using the following code to show search results. It has two options, one is a text field and another is a dropdown select option. I would like to convert the dropdown field into a checkbox option and allow users to select more than 1 option at a time. How can I do that?
<script>
$(document).ready(function(){
load_data(1);
function load_data(page,query,city)
{
$.ajax({
url:"fetch_data.php",
method:"POST",
data:{page:page, query:query, city:city},
success:function(data)
{
$('#dynamic_content').html(data);
}
});
}
$(document).on('click', '.page-link', function(){
var page = $(this).data('page_number');
var query = $('#search_box').val();
var city = $('#city_box').val();
load_data(page, query, city);
});
$('#search_box').keyup(function () {
var query = $('#search_box').val();
var city = $('#city_box').val();
load_data(1, query, city);
});
$('#city_box').change(function () {
var query = $('#search_box').val();
var city = $('#city_box').val();
load_data(1, query, city);
});
});
</script>
Current dropdown example:
<form action="" class="row login_form">
<select name="city_box" id="city_box">
<option value="newyork">New York</option>
<option value="london">London</option>
<option value="tokyo">Tokyo</option>
<option value="paris">Paris</option>
</select>
</form>
As per your query there are lot of stuff available. I have used Select2.js. This having so many features. I tried some way to achieve you scenario. Try below code snippet will work for you.
$(function() {
$("#multiple").select2({
closeOnSelect : false,
placeholder : "Placeholder",
allowHtml: true,
allowClear: true,
tags: true
});
});
function iformat(icon, badge,) {
var originalOption = icon.element;
var originalOptionBadge = $(originalOption).data('badge');
return $('<span><i class="fa ' + $(originalOption).data('icon') + '"></i> ' + icon.text + '<span class="badge">' + originalOptionBadge + '</span></span>');
}
.select2-container {
min-width: 400px;
}
.select2-results__option {
padding-right: 20px;
vertical-align: middle;
}
.select2-results__option:before {
content: "";
display: inline-block;
position: relative;
height: 20px;
width: 20px;
border: 2px solid #e9e9e9;
border-radius: 4px;
background-color: #fff;
margin-right: 20px;
vertical-align: middle;
}
.select2-results__option[aria-selected=true]:before {
font-family:fontAwesome;
content: "\f00c";
color: #fff;
background-color: #f77750;
border: 0;
display: inline-block;
padding-left: 3px;
}
.select2-container--default .select2-results__option[aria-selected=true] {
background-color: #fff;
}
.select2-container--default .select2-results__option--highlighted[aria-selected] {
background-color: #eaeaeb;
color: #272727;
}
.select2-container--default .select2-selection--multiple {
margin-bottom: 10px;
}
.select2-container--default.select2-container--open.select2-container--below .select2-selection--multiple {
border-radius: 4px;
}
.select2-container--default.select2-container--focus .select2-selection--multiple {
border-color: #f77750;
border-width: 2px;
}
.select2-container--default .select2-selection--multiple {
border-width: 2px;
}
.select2-container--open .select2-dropdown--below {
border-radius: 6px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
}
.select2-selection .select2-selection--multiple:after {
content: 'hhghgh';
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<h3>Multiple Select with Checkboxes</h3>
<select id="multiple" class="js-select2" multiple>
<option>Java</option>
<option>Javascript</option>
<option>PHP</option>
<option>Visual Basic</option>
</select>

Change radio button colour and select first element on load

I'm trying to populate radio buttons with data from MySQL.
I'd like the selected radio button to change colour, and the first element to be selected on page load.
How can I do this?
<div class="panel bg-product">
<?php
$stmt = mysqli_prepare($link, "SELECT id, name, price FROM products");
$stmt->execute();
$stmt->bind_result($prod_id, $prod_name, $prod_price);
while($stmt->fetch()) {
echo '<label><input type="radio" name="config-prod" value="'.$prod_id.'"> ' .$prod_name.'</label><br>';
}
$stmt->close();
?>
</div>
CSS:
.panel input[type="radio"]:checked + label{
font-weight: 700;
color: red;
transition: color 0.5s ease;
}
This CSS does not change the colour of the radio button when selected, nor is a radio button selected on load.
This is your new CSS:
.input-list {
list-style-type: none;
width: 100%;
padding: 0;
margin: 0;
}
.input-list li input:checked{
content: "";
-webkit-appearance: push-button;
width: 12px;
border-radius: 13px;
height: 12px;
background: red;
}
.input-list li input:checked + label {
color: red;
}
New JS code:
document.querySelector("#radio li input").checked = true;
And Your new PHP:
<ul id="radio" class="input-list">
<?php
$stmt = mysqli_prepare($link, "SELECT id, name, price FROM products");
$stmt->execute();
$stmt->bind_result($prod_id, $prod_name, $prod_price);
while($stmt->fetch()) {
echo '<li>
<input id="'.$prod_id.'" name="config-prod" value="'.$prod_id.'" type="radio">
<label class="sub-label" for="'.$prod_id.'">'.$prod_name.'</label>
</li>';
}
$stmt->close();
?>
</ul>
And that's how it will look:
document.querySelector("#radio li input").checked = true;
.input-list {
list-style-type: none;
width: 100%;
padding: 0;
margin: 0;
}
.input-list li input:checked {
content: "";
-webkit-appearance: push-button;
width: 12px;
border-radius: 13px;
height: 12px;
background: red;
}
.input-list li input:checked + label {
color: red;
}
<ul id="radio" class="input-list">
<li>
<input id="first" name="radio" value="first" type="radio">
<label class="sub-label" for="first">first</label>
</li>
<li>
<input id="second" name="radio" value="second" type="radio">
<label class="sub-label" for="second">second</label>
</li>
</ul>
This should do the trick for you. But you must note that it colors the label and not the radio, I think this is what you were trying to achieve.
HTML:
<div class="panel">
<input type="radio" name="radios" /><label>R1</label> //label for first Radio
<input type="radio" name="radios" /><label>R2</label> //Label for second radio
</div>
CSS:
.panel input[type="radio"]:checked + label{
background-color: yellow;
width: 10px;
height: 10px;
display: inline-block;
}
Tested on Codepen and works. Just use this as a template and populate your data accordingly. Just used some style to make it visible as I test.

Add more files separately using jquery [duplicate]

This question already has answers here:
Multiple file upload in php
(14 answers)
Closed 5 years ago.
My users are able to add more files separately. Now I am able to add multiple files at once, but I can't choose other files it getting replace. When user added one file there should be option for add more files. How can I achieve this? Here I have added only uploading scripts for this form.
PHP
if($_FILES['fil_attachment']['name'] != '') {
#copy(
$_FILES['fil_attachment']['tmp_name'],
'uploads/'.$_FILES['fil_attachment']['name']
);
$attachment[0] = "uploads"."/".$_FILES['fil_attachment']['name'];
$file_name=$_FILES['fil_attachment']['name'];
}
HTML
<form
class="form-request"
name="frmRequest"
method="post"
action
enctype="multipart/form-data"
>
<input
name="MyFile"
type="hidden"
id="MyFile"
tabindex="99"
size="1" />
<input
type="file"
name="fil_attachment"
multiple
id="fil_attachment"
onChange="MyFile.value=fil_attachment.value" />
<input type="submit">
</form>
Please refer to the PHP manual section on uploading multiple files here:
http://php.net/manual/en/features.file-upload.multiple.php
Also, have a look at this existing question:
Multiple file upload in php
JavaScript and jQuery help you to select multiple upload item. Here is the code snippet
$(function() {
var countFiles = 1,
$body = $('body'),
typeFileArea = ['txt', 'doc', 'docx', 'ods'],
coutnTypeFiles = typeFileArea.length;
//create new element
$body.on('click', '.files-wr label', function() {
var wrapFiles = $('.files-wr'),
newFileInput;
countFiles = wrapFiles.data('count-files') + 1;
wrapFiles.data('count-files', countFiles);
newFileInput = '<div class="one-file"><label for="file-' + countFiles + '">Attach file</label>' +
'<input type="file" name="file-' + countFiles + '" id="file-' + countFiles + '"><div class="file-item hide-btn">' +
'<span class="file-name"></span><span class="btn btn-del-file">x</span></div></div>';
wrapFiles.prepend(newFileInput);
});
//show text file and check type file
$body.on('change', 'input[type="file"]', function() {
var $this = $(this),
valText = $this.val(),
fileName = valText.split(/(\\|\/)/g).pop(),
fileItem = $this.siblings('.file-item'),
beginSlice = fileName.lastIndexOf('.') + 1,
typeFile = fileName.slice(beginSlice);
fileItem.find('.file-name').text(fileName);
if (valText != '') {
fileItem.removeClass('hide-btn');
for (var i = 0; i < coutnTypeFiles; i++) {
if (typeFile == typeFileArea[i]) {
$this.parent().addClass('has-mach');
}
}
} else {
fileItem.addClass('hide-btn');
}
if (!$this.parent().hasClass('has-mach')) {
$this.parent().addClass('error');
}
});
//remove file
$body.on('click', '.btn-del-file', function() {
var elem = $(this).closest('.one-file');
elem.fadeOut(400);
setTimeout(function() {
elem.remove();
}, 400);
});
});
input {
display: none;
}
.files-wr {
padding: 20px;
}
.files-wr label {
margin-bottom: 20px;
border-bottom: 1px dashed #177cca;
position: relative;
display: inline-block;
color: #177cca;
font-size: 18px;
font-weight: 400;
cursor: pointer;
transition: all .2s;
}
.files-wr label:after {
content: '+';
width: 32px;
height: 32px;
border-radius: 5px;
background-color: #177cca;
position: absolute;
top: -4px;
right: -47px;
font-size: 18px;
line-height: 32px;
color: #fff;
text-align: center;
transition: all .2s;
}
.files-wr label:hover,
.files-wr label:active {
color: #77c639;
border-color: #77c639;
}
.files-wr label:hover:after,
.files-wr label:active:after {
background-color: #77c639;
}
.files-wr .one-file~.one-file label {
display: none;
}
.files-wr .one-file.error {
border: none;
}
.files-wr .one-file.error .file-name {
color: #ca4a17;
}
.files-wr .file-item {
position: relative;
margin-top: 4px;
display: flex;
align-items: center;
}
.files-wr .file-item.hide-btn {
display: none;
}
.files-wr .file-name {
font-size: 16px;
font-style: italic;
line-height: 26px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
}
.files-wr .btn-del-file {
margin-left: 5px;
width: 16px;
min-width: 16px;
height: 16px;
line-height: 16px;
border-radius: 3px;
color: #fff;
text-align: center;
cursor: pointer;
transition: all .2s;
background-color: #177cca;
}
.files-wr .btn-del-file:hover,
.files-wr .btn-del-file:focus {
background-color: #ca4a17;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="files-wr" data-count-files="1">
<div class="one-file">
<label for="file-1">Attach file</label>
<input name="file-1" id="file-1" type="file">
<div class="file-item hide-btn">
<span class="file-name"></span>
<span class="btn btn-del-file">x</span>
</div>
</div>
</div>

How to create a checkbox for multiple choice on PHP

I have an input field I changed it to a checkbox for multiple choices.
The worry here I can't add the checkbox, I mean that my user should click on Ctrl and select on the same time several choices. You can see it in this Image:
This following my code of the form:
$app->get('/Chart/{currency}/{year}/{zone}/{lru}....... , function(Request $request,...... $repsite, $lru, $contract, $forecast) use ($app) {
if ($app['security']->isGranted('ROLE_USER')) {
///start form
$user = $app['security']->getToken()->getUser();
$form = $app['form.factory']->createBuilder('form')->setMethod('GET')
.
.
.
.
->add('lru', 'choice', array(
'choices' => array(
'\'ATSU\'' => 'ATSU',
'\'APCC\'' => 'APCC',
.....
),
'required' => FALSE,
'empty_value' => 'ALL',
'empty_data' => NULL,
'multiple' => TRUE
//'expanded' => TRUE
))
I want do a classic checkbox like this on the right:
I did a research on forums I found that I should use multiple and expanded and to set them to TRUE. When I add expanded=TRUE list became very "ugly", I give you a screensheet:
Can you please tell me how can I change my code to do a checkbox for multiple choicelike in the picture above.
I hope that I find solution. Thank you.
you can do this, this is implemented in this with code.
you will require jquery to do this.
change this code according to your use
https://codepen.io/elmahdim/pen/hlmri
/*
Dropdown with Multiple checkbox select with jQuery - May 27, 2013
(c) 2013 #ElmahdiMahmoud
license: https://www.opensource.org/licenses/mit-license.php
*/
$(".dropdown dt a").on('click', function() {
$(".dropdown dd ul").slideToggle('fast');
});
$(".dropdown dd ul li a").on('click', function() {
$(".dropdown dd ul").hide();
});
function getSelectedValue(id) {
return $("#" + id).find("dt a span.value").html();
}
$(document).bind('click', function(e) {
var $clicked = $(e.target);
if (!$clicked.parents().hasClass("dropdown")) $(".dropdown dd ul").hide();
});
$('.mutliSelect input[type="checkbox"]').on('click', function() {
var title = $(this).closest('.mutliSelect').find('input[type="checkbox"]').val(),
title = $(this).val() + ",";
if ($(this).is(':checked')) {
var html = '<span title="' + title + '">' + title + '</span>';
$('.multiSel').append(html);
$(".hida").hide();
} else {
$('span[title="' + title + '"]').remove();
var ret = $(".hida");
$('.dropdown dt a').append(ret);
}
});
body {
font: normal 14px/100% "Andale Mono", AndaleMono, monospace;
color: #fff;
padding: 50px;
width: 300px;
margin: 0 auto;
background-color: #374954;
}
.dropdown {
position: absolute;
top:50%;
transform: translateY(-50%);
}
a {
color: #fff;
}
.dropdown dd,
.dropdown dt {
margin: 0px;
padding: 0px;
}
.dropdown ul {
margin: -1px 0 0 0;
}
.dropdown dd {
position: relative;
}
.dropdown a,
.dropdown a:visited {
color: #fff;
text-decoration: none;
outline: none;
font-size: 12px;
}
.dropdown dt a {
background-color: #4F6877;
display: block;
padding: 8px 20px 5px 10px;
min-height: 25px;
line-height: 24px;
overflow: hidden;
border: 0;
width: 272px;
}
.dropdown dt a span,
.multiSel span {
cursor: pointer;
display: inline-block;
padding: 0 3px 2px 0;
}
.dropdown dd ul {
background-color: #4F6877;
border: 0;
color: #fff;
display: none;
left: 0px;
padding: 2px 15px 2px 5px;
position: absolute;
top: 2px;
width: 280px;
list-style: none;
height: 100px;
overflow: auto;
}
.dropdown span.value {
display: none;
}
.dropdown dd ul li a {
padding: 5px;
display: block;
}
.dropdown dd ul li a:hover {
background-color: #fff;
}
button {
background-color: #6BBE92;
width: 302px;
border: 0;
padding: 10px 0;
margin: 5px 0;
text-align: center;
color: #fff;
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<dl class="dropdown">
<dt>
<a href="#">
<span class="hida">Select</span>
<p class="multiSel"></p>
</a>
</dt>
<dd>
<div class="mutliSelect">
<ul>
<li>
<input type="checkbox" value="Apple" />Apple</li>
<li>
<input type="checkbox" value="Blackberry" />Blackberry</li>
<li>
<input type="checkbox" value="HTC" />HTC</li>
<li>
<input type="checkbox" value="Sony Ericson" />Sony Ericson</li>
<li>
<input type="checkbox" value="Motorola" />Motorola</li>
<li>
<input type="checkbox" value="Nokia" />Nokia</li>
</ul>
</div>
</dd>
<button>Filter</button>
</dl>
Hope this helps

Multiple Star Review on Same Page

I have a multi-page form and on one of the pages I have a star-review application, whereby users can vote between 1 and 5 stars. I have it working great, however on the same page I need this multiple times (i.e. different things to review).
I have tried a number of things; changing the class of 'stars' and 'star', but neither allows multiple ratings - they all trigger back to the first one (i.e. if you select 2 star on the second it also defaults the first selection to 2 star, or only selects the first selection to 2 star). Any ideas?
HTML code:
<div class="stars">
<input type="radio" name="star" class="star-1" id="star-1" />
<label class="star-1" for="star-1">1</label>
<input type="radio" name="star" class="star-2" id="star-2" />
<label class="star-2" for="star-2">2</label>
<input type="radio" name="star" class="star-3" id="star-3" />
<label class="star-3" for="star-3">3</label>
<input type="radio" name="star" class="star-4" id="star-4" />
<label class="star-4" for="star-4">4</label>
<input type="radio" name="star" class="star-5" id="star-5" />
<label class="star-5" for="star-5">5</label>
<span></span>
</div>
CSS:
form .stars {
background: url("stars.png") repeat-x 0 0;
width: 150px;
margin: 0 auto;
}
form .stars input[type="radio"] {
position: absolute;
opacity: 0;
filter: alpha(opacity=0);
}
form .stars input[type="radio"].star-5:checked ~ span {
width: 100%;
}
form .stars input[type="radio"].star-4:checked ~ span {
width: 80%;
}
form .stars input[type="radio"].star-3:checked ~ span {
width: 60%;
}
form .stars input[type="radio"].star-2:checked ~ span {
width: 40%;
}
form .stars input[type="radio"].star-1:checked ~ span {
width: 20%;
}
form .stars label {
display: block;
width: 30px;
height: 30px;
margin: 0!important;
padding: 0!important;
text-indent: -999em;
float: left;
position: relative;
z-index: 10;
background: transparent!important;
cursor: pointer;
}
form .stars label:hover ~ span {
background-position: 0 -30px;
}
form .stars label.star-5:hover ~ span {
width: 100% !important;
}
form .stars label.star-4:hover ~ span {
width: 80% !important;
}
form .stars label.star-3:hover ~ span {
width: 60% !important;
}
form .stars label.star-2:hover ~ span {
width: 40% !important;
}
form .stars label.star-1:hover ~ span {
width: 20% !important;
}
form .stars span {
display: block;
width: 0;
position: relative;
top: 0;
left: 0;
height: 30px;
background: url("stars.png") repeat-x 0 -60px;
-webkit-transition: -webkit-width 0.5s;
-moz-transition: -moz-width 0.5s;
-ms-transition: -ms-width 0.5s;
-o-transition: -o-width 0.5s;
transition: width 0.5s;
}
I got it working by changing the radio name and keeping IDs unique.
Radio buttons are grouped by name so that each represents a value for a particular form "variable". With each set of stars representing a different variable, a product's rating, each group should have a separate name reflecting which product they're for e.g. appending the product id, name="star-123456". You could also append the index of the product in the list of items for the form.
IDs should always be unique among elements. In this case the radio input ids should be unique for the labels' for attribute to point to the right one.
The snippet below contains unique names for the two radio groups and unique IDs for each radio input.
form .stars {
background: gray;
width: 150px;
margin: 0 auto;
}
form .stars input[type="radio"] {
position: absolute;
opacity: 0;
filter: alpha(opacity=0);
}
form .stars input[type="radio"].star-5:checked ~ span {
width: 100%;
}
form .stars input[type="radio"].star-4:checked ~ span {
width: 80%;
}
form .stars input[type="radio"].star-3:checked ~ span {
width: 60%;
}
form .stars input[type="radio"].star-2:checked ~ span {
width: 40%;
}
form .stars input[type="radio"].star-1:checked ~ span {
width: 20%;
}
form .stars label {
display: block;
width: 28px;
height: 28px;
border: 1px solid black;
margin: 0!important;
padding: 0!important;
text-indent: -999em;
float: left;
position: relative;
z-index: 10;
background: transparent!important;
cursor: pointer;
}
form .stars label:hover ~ span {
background-position: 0 -30px;
}
form .stars label.star-5:hover ~ span {
width: 100% !important;
}
form .stars label.star-4:hover ~ span {
width: 80% !important;
}
form .stars label.star-3:hover ~ span {
width: 60% !important;
}
form .stars label.star-2:hover ~ span {
width: 40% !important;
}
form .stars label.star-1:hover ~ span {
width: 20% !important;
}
form .stars span {
display: block;
width: 0;
position: relative;
top: 0;
left: 0;
height: 30px;
background: red;
filter: alpha(opacity=0);
-webkit-transition: -webkit-width 0.5s;
-moz-transition: -moz-width 0.5s;
-ms-transition: -ms-width 0.5s;
-o-transition: -o-width 0.5s;
transition: width 0.5s;
}
<form>
<div class="stars">
<input type="radio" name="star_a" class="star-1" id="star_a-1" />
<label class="star-1" for="star_a-1">1</label>
<input type="radio" name="star_a" class="star-2" id="star_a-2" />
<label class="star-2" for="star_a-2">2</label>
<input type="radio" name="star_a" class="star-3" id="star_a-3" />
<label class="star-3" for="star_a-3">3</label>
<input type="radio" name="star_a" class="star-4" id="star_a-4" />
<label class="star-4" for="star_a-4">4</label>
<input type="radio" name="star_a" class="star-5" id="star_a-5" />
<label class="star-5" for="star_a-5">5</label>
<span></span>
</div>
<div class="stars">
<input type="radio" name="star_b" class="star-1" id="star_b-1" />
<label class="star-1" for="star_b-1">1</label>
<input type="radio" name="star_b" class="star-2" id="star_b-2" />
<label class="star-2" for="star_b-2">2</label>
<input type="radio" name="star_b" class="star-3" id="star_b-3" />
<label class="star-3" for="star_b-3">3</label>
<input type="radio" name="star_b" class="star-4" id="star_b-4" />
<label class="star-4" for="star_b-4">4</label>
<input type="radio" name="star_b" class="star-5" id="star_b-5" />
<label class="star-5" for="star_b-5">5</label>
<span></span>
</div>
</form>

Categories