In a quiz page of a website I'm building, I'm using ajax pagination to load in questions with multiple choices (only one of which is correct) from a MYSQL database.
The problem I'm facing is, whenever I go to another page and switch back, all radio buttons are unchecked.
This is a sample code of a quiz question:
<div class='quiz-item'>
<p class='question'>question</p>
<label for='questionID-A' class='radio'>
<input type='radio' name='questionID' id='questionID-A' class='radio-input'>
optionA
</label>
<label for='questionID-B' class='radio'>
<input type='radio' name='questionID' id='questionID-B' class='radio-input'>
optionB
</label>
<label for='questionID-C' class='radio'>
<input type='radio' name='questionID' id='questionID-C' class='radio-input'>
optionC
</label>
<label for='questionID-D' class='radio'>
<input type='radio' name='questionID' id='questionID-D' class='radio-input'>
optionD
</label>
</div>
Is there any way to keep them checked?
Related
I tried to search yet i unable to find out what i am looking for.Please help me.I am working on a rating form which using radio button star for rating,I am using while loop of mysqli_fetch_assoc to pull the date from database.As shown below
<?php
$sql = "SELECT * FROM orderdetail,orderuser,food WHERE orderuser.CustomerID=orderdetail.CustomerID AND orderuser.OrderID=orderdetail.OrderID AND orderdetail.FoodID=food.FoodID AND orderuser.confirmstatus=1";
$result = $conn->query($sql) or die($conn->error);
while($colum = mysqli_fetch_assoc($result))
{
?>
<img src="../php/<?php echo $colum['Foodphoto'];?>" style="height:150px;width:150px">
<br>
<?php echo $colum['FoodName'];?>
<br>
<div class="stars">
<form action="">
<input class="star star-5" id="rate-5[]" type="radio" name="rate[]" />
<label class="star star-5" for="rate-5[]"></label>
<input class="star star-4" id="rate-4[]" type="radio" name="rate[]" />
<label class="star star-4" for="rate-4[]"></label>
<input class="star star-3" id="rate-3[]" type="radio" name="rate[]" />
<label class="star star-3" for="rate-3[]"></label>
<input class="star star-2" id="rate-2[]" type="radio" name="rate[]" />
<label class="star star-2" for="rate-2[]"></label>
<input class="star star-1" id="rate-1[]" type="radio" name="rate[]" />
<label class="star star-1" for="rate-1[]"></label>
</form>
</div>
<hr>
<?php
}
?>
but the rating for the second result is not working,i find out that is due to the problem of name but i tried to edit it by adding the array still not working. And how could i store them into mysql using php?those tutorial i found only using ajax or jquery i need the ratings submit once the user submitted the form.
The problem I think is that you cannot relate the form being submitted with the record so any update will not work as expected. One of the simplest ways to accomplis this would be a hidden field with the ID of the record - the ID can then be used in the update statement - ie: ( pseudo sql update )
update orderdetail set rating=$rate where ID=$id
So, add a hidden field per form like this perhaps
while($colum = mysqli_fetch_assoc($result)){
?>
<img src='../php/<?php echo $colum['Foodphoto'];?>' style='height:150px;width:150px'>
<br>
<?php echo $colum['FoodName'];?>
<br>
<div class='stars'>
<form action=''>
<input class='star star-5' type='radio' id='rate-5' name='rate' />
<label class='star star-5' for='rate-5'></label>
<input class='star star-4' type='radio' id='rate-4' name='rate' />
<label class='star star-4' for='rate-4'></label>
<input class='star star-3' type='radio' id='rate-3' name='rate' />
<label class='star star-3' for='rate-3'></label>
<input class='star star-2' type='radio' id='rate-2' name='rate' />
<label class='star star-2' for='rate-2'></label>
<input class='star star-1' type='radio' id='rate-1' name='rate' />
<label class='star star-1' for='rate-1'></label>
<!--
HIDDEN FIELD WITH ID
-->
<input type='hidden' name='id' value='<?php echo $colum['id'];?>' />
</form>
</div>
<hr>
I have same div multiple times.If I click on 'Mr' radio button in first div field should change last div radio button value as 'male' Here my code.It is changing all div values as 'male'.How do I change related form Gender radio button value when clicking the title radio button.Please anybody help me.
<?php
for($i=1;$i<=$totalchilds;$i++){
?>
<div class="primary Applicant-details">
<h3>Tourist Details </h3>
<div>
<label>Title : </label>
<input type="radio" class="gender" name="c_genderinfo_<?=$i?>" value="Mr" checked>
Mr
<input type="radio" class="gender1" name="c_genderinfo_<?=$i?>" value="Mrs">
Mrs </div>
<div>
<label for="fname">Name</label>
<input type="text" name="c_name_<?= $i ?>" placeholder="child name">
</div>
<div>
<label for="lname">Age</label>
<input type="text" name="c_age_<?=$i?>" placeholder="Enter child age">
</div>
<div>
<label>Gender : </label>
<input type="radio" class="gender" name="c_gender_<?=$i?>" value="M" checked>
Male
<input type="radio" class="gender1" name="c_gender_<?=$i?>" value="F">
Female </div>
</div>
<?php }?>
To change the gender radio button in the same div use this code:
$('.gender').click(function(){
$(this).closest('.Applicant-details').find('.gender').prop('checked',true);
});
$('.gender1').click(function(){
$(this).closest('.Applicant-details').find('.gender1').prop('checked',true);
});
The structure of the code isn't very optimal. Rename the title from gender to title. Naming the radio buttons gender and gender1 isn't any better. Besides, from a usability perspective, having both selections that have the same value doesn't make a lot of sense.
You can use jQuery. There are different methods to achieve this. This is one of them.
Pass your id and value from html like this:
<div>
<label>Title : </label>
<input onclick="changeGender(<?=$i?>, 'M')" type="radio" class="gender" name="c_genderinfo_<?=$i?>" value="Mr" checked>
Mr
<input onclick="changeGender(<?=$i?>, 'F')" type="radio" class="gender1" name="c_genderinfo_<?=$i?>" value="Mrs">
Mrs
</div>
And in script:
function changeGender(id, value)
{
$("input[name=c_gender_"+id+"][value=" + value + "]").prop('checked', true);
}
I have a table in html which lists review entries then scores them using a drop down box. I am wondering how to count the results:
<td class="views-field views-field-field-price-editable" >
<div class="views-row-edit-static"></div><div class="views-row-edit-edit"><div class="field-type-list-text field-name-field-price field-widget-options-buttons form-wrapper" id="edit-node-59200313-field-price"><div class="form-item form-type-radios form-item-node-59200313-field-price-und">
<label for="edit-node-59200313-field-price-und">Price <span class="form-required" title="This field is required.">*</span></label>
<div id="edit-node-59200313-field-price-und" class="form-radios"><div class="form-item form-type-radio form-item-node-59200313-field-price-und">
<input type="radio" id="edit-node-59200313-field-price-und-no-mention" name="node[59200313][field_price][und]" value="no mention" checked="checked" class="form-radio" /> <label class="option" for="edit-node-59200313-field-price-und-no-mention">No Mention </label>
</div>
<div class="form-item form-type-radio form-item-node-59200313-field-price-und">
<input type="radio" id="edit-node-59200313-field-price-und-positive" name="node[59200313][field_price][und]" value="positive" class="form-radio" /> <label class="option" for="edit-node-59200313-field-price-und-positive">Positive </label>
</div>
<div class="form-item form-type-radio form-item-node-59200313-field-price-und">
<input type="radio" id="edit-node-59200313-field-price-und-negative" name="node[59200313][field_price][und]" value="negative" class="form-radio" /> <label class="option" for="edit-node-59200313-field-price-und-negative">Negative </label>
</div>
</div>
</div>
</div></div> </td>
There are 3 options: 1) "No Mention" 2) Positive 3) Negative
In the code, the field 'Service' has been scored 'positive'.
How do I count these values up, so I can see how many 'Positives' there are in my 'Service' column? The website is run on php so I'd assume I can use this but as the fields are not numeric I am not so sure. Any help appreciated.
You have to provide us with more (HTML) code (the part with No Mention and Negative) for us to help you out but you (probably) only need to create a new variable and set it to zero and every time a field has the value Positive you just add one to the variable - but without the HTML and PHP code we cannot really help you more!
how can i get multiple value data from multi checkbox options? Following are my checkbox html code that can select multiple values
<li class='voteable-attribute off clearfix'>
<label class='primary formField'>Parking:
</label>
<div class='inputFields ieSucks'>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:valet'>
<input type='checkbox' id='av_box:BusinessParking:valet' name='BusinessParking_valet' value='1'>
Valet
</label>
</div>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:garage'>
<input type='checkbox' id='av_box:BusinessParking:garage' name='BusinessParking_garage' value='1'>
Garage
</label>
</div>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:street'>
<input type='checkbox' id='av_box:BusinessParking:street' name='BusinessParking_street' value='1'>
Street
</label>
</div>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:lot'>
<input type='checkbox' id='av_box:BusinessParking:lot' name='BusinessParking_lot' value='1'>
Private Lot
</label>
</div>
<div class='thereisonlybool'>
<label for='av_box:BusinessParking:validated'>
<input type='checkbox' id='av_box:BusinessParking:validated' name='BusinessParking_validated' value='1'>
Validated
</label>
</div>
</div>
</li>
From zend controller i can access single value as follow:
$data1 = $this->_getParam('input_name')
But in this case there is multiple value, how can i get this multiple values in zend controller and store it on array and save on db.Thanks
You have to create a element with this name BusinessParking[].
meand create this checkbox like array.
now when you get value controller then you get what you want.
Create the html page as:
<input type="hidden" name="id[]" value="<?php echo $value[i] ?>"/>
Note the name="id[]", that's the trick. Now in Zend you can get the values as:
$array = $this->getRequest()->getParam("id");
I have a database with pictures that i can delete from or use them in different parts of the the website by choosing one of the radio buttons in the popup.Problem is everytime I click I click on a picture the name of the first picture in the database comes up not the one from the picture i clicked . What's wrong? I used PHP 5.3 and HeidiSQL
<?php
// ...
$result=mysql_query("SELECT * FROM imagini WHERE menu_id=6");
while($data=mysql_fetch_row($result)){ ?>
<div class="tag">
<div id='container_poze_originale'>
Alege alt rol
<div id="my_popup" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
close
<form action="popup.php" method="post" >
<input type="text" name="alt-rol" value="<?php echo $data[1];?>" /> //always returns the name of the first picture in the database not the one i select
<input type="submit" value="Adauga imagine" class="buton_imagine" />
<div class="radio">
<input type="radio" name="tip_imagine" value="0"/><label for="tip_imagine" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1"/><label for="tip_imagine" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2"/><label for="tip_imagine" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3"/><label for="tip_imagine" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4"/><label for="tip_imagine" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5"/><label for="tip_imagine" class="radio2">Background</label>
</div>
</form>
</div>
<div class="imagine_originala">
<img src= "../upload/original/<?php echo $data[1];?>" /></a>
</div>
<div class="Btag" style="display:none;">
<div id="buton_slide4" >
Sterge</td>
</div>
</div>
</div>
</div>
<?php
}
?>
I think I understand your question. You are clicking on
Alege alt rol
and you are surprised that the same <div> always appears?
Well, it is simply because an id should be unique. And if you cycle through your images you create a new <div> each time with the same id: my_popup. When clicking on the link (no matter which one), JavaScript will then just take the first <div> it finds with the id my_popup. So you should just assign an unique id to each div so your JavaScript knows which one to open. You could do this by appending your image id to the div id (assuming you have a unique image id in $data[1]).
So change it to (shortened):
<? while($data=mysql_fetch_row($result)) { ?>
<div class="tag">
<div id="container_poze_originale">
Alege alt rol
<div id="my_popup<?=$data[1]?>" style="display:none;border:1px dotted gray;padding:.3em;background-color:white;position:absolute;width:auto;left:100px;top:100px">
...
</div>
</div>
</div>
<? } ?>
Oh and it might be good to put all those huge inline CSS attributes into an external stylesheet, so it will be easier to maintain/change your webpage later on. And it makes things a bit more readable... :-)
I think your form is ok when you click on the radio but not when you click on the label.
The for attribute of the label need an ID as reference like this :
<input type="radio" name="tip_imagine" value="0" id="tip0" /><label for="tip0" class="radio2">Logo</label>
<input type="radio" name="tip_imagine" value="1" id="tip1"/><label for="tip1" class="radio2">Slider</label>
<input type="radio" name="tip_imagine" value="2" id="tip2"/><label for="tip2" class="radio2">Hot destinations</label>
<input type="radio" name="tip_imagine" value="3" id="tip3"/><label for="tip3" class="radio2">Pachete</label>
<input type="radio" name="tip_imagine" value="4" id="tip4"/><label for="tip4" class="radio2">Reclama</label>
<input type="radio" name="tip_imagine" value="5" id="tip5"/><label for="tip5" class="radio2">Background</label>
With your actual code, the FOR is always the same so you get always the first.