Multiple Star Review on Same Page - php

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>

Related

Radio Switch Button not working if its required and user click submit before select anything

I have php form like this
First, user need to select Type of individual.
if user do not select anything and click submit button, a required message will show. actually my form work if user doing straight forward.
The problem is when user do not select type individiual and click submit, message apair. but when user select individual, the radio button dont change to blue. the radio button should change to whatever user select.
but if user select company, the button can change to blue
scenario:
User do not select type individual, then click submit, required message apair
User then select company (radio button change to blue), but if user select individu (radio button dont change, but the hide/show function still work, it not change to blue only, blue color just for remark if user select that button)
$( document ).ready(function( $ ) {
$("#smart-form-register").validate({
rules : {
type_of_individual: {
required : true,
},
type_sfaff: {
required : true,
},
},
});
});
$( "#radio-one" ).change(function() {
var ind = $('#radio-one').val();
$("#type_staff").show();
$("#non_individual").hide();
$("#staff").hide();
});
$( "#radio-two" ).change(function() {
var non = $('#radio-two').val();
$("#individual").hide();
$("#non_individual").show();
$("#staff").hide();
$("#type_staff").hide();
$("#radio-three").prop('checked', false);
$("#radio-four").prop('checked', false);
});
//IF STAFF
$( "#radio-three" ).change(function() {
var staff1 = $('#radio-three').val();
$("#staff").show();
$("#individual").show();
});
//IF NON
$( "#radio-four" ).change(function() {
var nonstaff = $('#radio-four').val();
$("#staff").hide();
$("#individual").show();
});
.switch-field {
display: flex;
margin-bottom: 36px;
overflow: hidden;
}
.switch-field input {
position: absolute !important;
clip: rect(0, 0, 0, 0);
height: 1px;
width: 1px;
border: 0;
overflow: hidden;
}
.switch-field label {
/* background-color: #e4e4e4;
color: rgba(0, 0, 0, 0.6);
font-size: 14px;
line-height: 1;
text-align: center;
padding: 8px 16px;
margin-right: -1px;
border: 1px solid rgba(0, 0, 0, 0.2);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1);
transition: all 0.1s ease-in-out;*/
padding: 6px;
margin: 10px 17px 15px 0;
font-size: 16px;
background: #fff;
color: #0e4e91;
border: solid 2px #0e4e91;
border-radius: 5px;
min-width: 150px;
text-align: center;
transition: all 0.1s ease-in-out;
margin-right: -1px;
}
.switch-field label:hover {
cursor: pointer;
/*background-color: #204184;
box-shadow: none;
border: solid 2px #204184;
color: #fff;*/
}
.switch-field input:checked + label {
background-color: #204184;
box-shadow: none;
border: solid 2px #204184;
color: #fff;
}
.switch-field label:first-of-type {
border-radius: 4px 0 0 4px;
}
.switch-field label:last-of-type {
border-radius: 0 4px 4px 0;
}
.switch-field label.error {
position:absolute;
display: inline-block;
max-width: 100%;
font-weight: 700;
font-size: 16px;
background: #fff;
color: #e82c0c !important;
border: 0px !important;
border-radius: 5px;
min-width: 150px;
margin-top: 60px!important;
padding: 0 1px!important;
line-height: 15px!important;
}
.switch-field label[for=switch_right] {
background-color: red !important;
}
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
<div>
<form id="smart-form-register">
<label class="control-label" for="name">Individual Type</label><br>
<div class="switch-field">
<input type="radio" id="radio-one" name="type_of_individual" value="1" required="">
<label for="radio-one">Individual</label>
<input type="radio" id="radio-two" name="type_of_individual" value="0" required >
<label for="radio-two">Company</label></div>
</div>
<div id="type_staff" style="display: none">
<label class="control-label" for="name">Staff Type</label><br>
<div class="switch-field">
<input type="radio" id="radio-three" name="type_of_staff" value="1" required="">
<label for="radio-one">Staff</label>
<input type="radio" id="radio-four" name="type_of_staff" value="0" required >
<label for="radio-two">Non Staff</label></div>
</div>
<!--INDIVIDUAL-->
<div id="individual" style="display: none">
<label>Fullname <sup>*</sup></label>
<input type="text" class="form-control" id="fullname" name="fullname" maxlength="255" >
</div>
<!--NON INDIVIDUAL-->
<div id="non_individual" style="display: none">
<label>Company Name <sup>*</sup></label>
<input type="text" class="form-control" id="company_name" name="company_name" maxlength="255" >
</div>
<!--STAFF-->
<div id="staff" style="display: none">
<label>Staff ID <sup>*</sup></label>
<input type="text" class="form-control" id="staff_id" name="staff_id" maxlength="255" >
</div>
<button type="submit" name="singlebutton" class="btn btn-primary btn-sm " id="register" >Submit</button></form>

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.

HTML Hidden button is still clickable if the user clicks in the area where the button appears

I created a makeshift hide/show menu in HTML/CSS using a checkbox, it works great to hide and show text and links, but I have a section where I'd like to show a button when the "checkbox" is clicked. At first it looks great, the button is hidden and when the user clicks the checkbox, a button appears below it. My problem that I'm having is that if the user clicks below the checkbox where the button appears before the button actually appears, they can still trigger the button. Any idea?
<td>
<label class="collapsible">
<input type="checkbox" />
<span class="collapser">Edit</span>
<span class="arrow">></span>
<div class="collapsed">
<form method="post">
<input type="hidden" id="pwd" name="deleteid" value='. $value["id"] . '>
<button onclick="return confirm_delete();" type="submit" name="DelBidSubmit" class="btn btn-default">Delete</button>
<script type="text/javascript">
function confirm_delete() {
return confirm("Are you sure you wish to delete that?");
}
</script></form></div></label></td>
Heres my CSS code just in case
.collapsible {
display: block;
margin-bottom: 1rem;
}
.collapsible input {
position: absolute;
left: -9999px;
}
.collapsible input:focus ~ .collapser {
border-color: grey;
}
.collapsible .collapser {
cursor: pointer;
border: 1px transparent dotted;
}
.collapsible .arrow {
float: right;
margin-left: 0.5em;
display: inline-block;
transform: rotate(90deg);
transition: transform 0.25s ease-out;
}
.collapsible input:checked ~ .arrow,
.collapsible input:checked ~ .collapser .arrow {
transform: rotate(270deg);
}
.collapsible .collapsed {
font-size: 0;
margin: 0;
opacity: 0;
padding: 0;
/* fade out, then shrink */
transition: opacity 0.25s, margin 0.5s 0.25s, font-size 0.5s 0.25s, padding 0.5s 0.25s;
}
.collapsible input:checked ~ .collapsed {
font-size: 12px;
opacity: 1;
height: auto;
padding: 5px 0;
/* grow, then fade in */
transition: margin 0.25s, padding 0.25s, font-size 0.25s, opacity 0.5s 0.25s;
}
I think you must use {display: none}; and {display: block} to handle show/hide actions. Remove opacity:0 and opacity:1 from your styles and replace display:none and display:block styles.

how to change a css property of div when a text box is selected [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 4 years ago.
i have been trying for a while now to figure out how to change the opacity of a login panel that is being called in using a php function. and in css i have tryed this in css
.container
{
background: #FFF;
opacity: 0.6;
width: 320px;
height: 500px;
align-content: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-sizing: border-box;
padding: 30px 30px;
transition: 0.3s;
}
.container:hover
{
opacity: initial;
transition: 0.3s;
}
.container input[type="text"]:focus .container
{
opacity: initial;
transition: 0.3s;
}
to change the opacity of the .container element when the input is focused on
but it dosnt work is there any way to fix this
btw the input is in a form like this
<div class="container">
<form action="validatelogin.php" method="POST">
<p>Username:</p>
<input type="text" name="usernameinput" id="usernameinput" />
<p>Password:</p>
<input type="Password" name="passwordinput" id="passwordinput" />
<br />
<div style="margin-top:10px;">
<input type="submit" value="Login" />
<button type="button" onclick="location.href=\"register.html\";">Register</button>
</div>
</form>
</div>
https://jsfiddle.net/97py6vgj/23/ is my solution. I just used jQuery when I detected a mousedown on the container. To change the css, I used the .css() method. Then, I did the trick where you detect a click outside of the element, and made it back to regular. You can edit this accordingly.
I will add the snippet here.
var stay = 0;
$('input').mousedown(function() {
$(".container").css("opacity", "initial");
stay = 1;
});
$(window).mousedown(function () {
$('.container').css("opacity", "0.6")
stay = 0;
});
$('.container').mousedown(function (event) {
event.stopPropagation();
});
$('.container').mouseenter(function() {
$(this).css("opacity", "initial");
$(this).css("transition", "0.3s");
});
$('.container').mouseleave(function() {
if(stay == 0) {
$(this).css("opacity", "0.6");
$(this).css("transition", "0.3s");
} else {$(this).css("opacity", "initial");}
});
.container
{
background: #FFF;
opacity: 0.6;
width: 320px;
height: 500px;
align-content: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
box-sizing: border-box;
padding: 30px 30px;
transition: 0.3s;
}
.container input[type="text"]:focus .container
{
opacity: initial;
transition: 0.3s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<form action="validatelogin.php" method="POST">
<p>Username:</p>
<input type="text" name="usernameinput" id="usernameinput" />
<p>Password:</p>
<input type="Password" name="passwordinput" id="passwordinput" />
<br />
<div style="margin-top:10px;">
<input type="submit" value="Login" />
<button type="button" onclick="location.href=\'register.html\';">Register</button>
</div>
</form>
</div>
I'm not sure if this is what you wanted but here you are.

How can I insert data to a table from the selected checkbox?

I am trying to make a way that I can decide in what table the name and the hour goes, by selecting it from one or both checkboxes.
For example, if I have a new name and hour, I can choose whether it is going in the table of "standard" or "advanced" or both by selecting it through the checkboxes.
#myform .plus, .minus {
text-decoration: none;
color: #fff;
background-color: #fdd818;
text-align: center;
letter-spacing: .5px;
-webkit-transition: .2s ease-out;
transition: .2s ease-out;
cursor: pointer;
}
.collapsible-header{
color: #ffffff;
}
.row{
margin: 0;
width: 100%;
}
.uren{
display: -webkit-inline-box;
padding-left: 10px;
padding-right: 10px;
}
.btn {
-webkit-border-radius: 28;
-moz-border-radius: 28;
border-radius: 28px;
font-family: Arial;
color: #ffffff;
font-size: 20px;
background: #ffcc00;
padding: 5px 10px 5px 10px;
text-decoration: none;
}
.btn:hover {
background: #dbaf00;
text-decoration: none;
}
/* Pop up */
.letspop{
background-color: #fff;
width: 120px;
display: block;
margin: 5% auto;
padding: 20px;
position: relative;
text-align: center;
float: right;
margin-right: 20px;
margin-top: 0px;
}
.letspop:hover{
cursor: pointer;
}
.overlay{
display: none; /* Default Hidden */
position: fixed;
left: 0;
top: 0;
z-index: 1;
background-color: rgba(0,0,0,0.6);
height: 100%;
width: 100%;
}
.popup{
display: none; /* Default Hidden */
position: fixed;
left: 50%;
top: 5%;
z-index: 2;
background-color: #fff;
height: 450px;
width: 300px;
overflow: hidden;
padding: 40px;
transform: translate(-50%, 0);
}
#new_module{
width: 195px;
}
h1, h2{
color: steelblue ;
font-family: sans-serif;
text-align: center;
text-transform: uppercase;
line-height: 1;
}
h2{
color: dodgerblue ;
}
small{
color: #444 ;
font-size:0.4em;
display: block;
margin: 0.2rem auto 0;
}
.close{
position: absolute;
top: 8px;
right: 8px;
display: block;
color: #666666;
font-family: sans-serif;
}
.close:hover{
cursor: pointer;
color: #444444;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css" rel="stylesheet"/>
<form action="#" method="POST">
<div class="row">
<div class="input-field col s6">
<input id="new_module" type="text" class="validate">
<label class="active" for="new_module">Name</label>
</div>
</div>
<form action="#">
<p class="range-field">
<input type="range" id="test" min="0" max="20" />
</p>
</form>
<p>
<input type="checkbox" class="filled-in" id="standard"/>
<label for="standard">Standard</label>
</p>
<p>
<input type="checkbox" class="filled-in" id="advanced"/>
<label for="advanced">Advanced</label>
</p>
<hr>
<button class="btn waves-effect waves-light" type="submit" name="action">
<i class="material-icons right">send</i>
</button>
<div class="close">X</div>
</form>
You need to set name for html elements to submit to server.
try the following changes:
<form action="checkbox.php" method="post">
<p>
<input type="checkbox" class="filled-in" id="standard" name="chk[]" value="standard"/>
<label for="standard">Standard</label>
</p>
<p>
<input type="checkbox" class="filled-in" id="advanced" name="chk[]" value="advanced"/>
<label for="advanced">Advanced</label>
</p>
<input type="submit" />
</form>
and try the following php code to find selected checkbox
foreach($_POST['chk'] as $key => $chk){
if($chk == 'standard'){
// do some code
}
if($chk == 'advanced'){
// do some code
}
}
quick answer to begin to help you.
Do a "indatabase.php" where there is your SQL actions in. After, do something like :
<form action="/indatabase.php" method="POST">
See this :
https://stackoverflow.com/a/22836074/8695939
If you want a bit more security, use PHP PDO instead.
You can have sqli and PDO exemples here :
https://www.w3schools.com/php/php_mysql_insert.asp

Categories