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?
I have a group of checkboxes in a from and I need to get values with php. Note that div class="group" can also be duplicated onruntime (with jquery) so I may end with multiple div class="group" of checkboxes.
Is there a way to set checkboxes name so form data gets grouped together for each "group", or whatever is easier to handles afterwards? There is also a problem of checkboxes that were not checked, so they wont be present in a post data.
I tried using names like this but I dont like the results.
<form>
<div class="group">
<input type="checkbox" name="ev[][time]">
<input type="checkbox" name="ev[][space]">
<input type="checkbox" name="ev[][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[][time]">
<input type="checkbox" name="ev[][space]">
<input type="checkbox" name="ev[][money]">
</div>
</form>
When defining checkbox groups its much better to be verbose with input names and avoid using the [] notation.
A un-ticked checkbox is not sent in post data, and your selections will become misaligned.
<form>
<div class="group">
<input type="checkbox" name="ev[0][time]">
<input type="checkbox" name="ev[0][space]">
<input type="checkbox" name="ev[0][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[1][time]">
<input type="checkbox" name="ev[1][space]">
<input type="checkbox" name="ev[1][money]">
</div>
<div class="group">
<input type="checkbox" name="ev[2][time]">
<input type="checkbox" name="ev[2][space]">
<input type="checkbox" name="ev[2][money]">
</div>
</form>
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 am new to stackoverflow and little bit confused about what to title this question, but let me explain what my problem is.
I am developing survey system where I have option for admin to enter the questions and answers from the back end, so in the front end I am displaying the question, answers using the while loop in the form.
My problem is how to write the code to enter the multiple values (survey filled by the customer) in the mysql. I am attaching my code here.
$(function() {
$('form').bind('submit', function(){
$.ajax({
type: 'post',
url: "/prs/pageCode.php",
data: $("form").serialize(),
success: function() {
alert("Data Saved, Press Next!");
}
});
return false;
});
});
form is below
<form name="form" id="form" class="form-horizontal" action="#"
method="POST">
<fieldset>
<div class="control-group">
<label class="control-label" for="focusedInput">Name</label>
<div class="controls">
<input name="pname" class="input-xlarge focused" id="focusedInput"
type="text" value="" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<input name="pemail" class="input-xlarge focused" id="focusedInput"
type="text" value="" required>
</div>
</div>
<div class="control-group">
<label>We would like to post your comments to internet rating sites,
while we may use your name or an alias name. Please select your
desire:</label>
</div>
<div class="control-group">
<label class="control-label" for="focusedInput"></label>
<div class="controls">
<p>
<label> <input type="radio" name="choice" value="yes" id="choice_0">
Yes, use my name as entered above
</label> <label> <input type="radio" name="choice" value="no"
id="choice_1" onChange="enabletextbox();"> Use the alias name, as
I enter below
</label> <input id="alias" name="alias" type="text"
class="input-xlarge focused">
</p>
</div>
</div>
</fieldset>
</div>
<div class="tab-pane" id="tab2">
<fieldset>
<?php
$i=1;
while($row=mysqli_fetch_array($questions)){
?>
<div class="control-group">
<label class="control-label" for="focusedInput">(<?php echo $i;?>) <?php
$questionid = $row['question_id'];
$question = $row['question'];
echo $row['question']; ?></label>
<div class="controls">
<?php
if($row['answer_type']=="Ratings") {
echo "
<p>
Low<input type='radio' name='rating$i' value='1' id='rating_0'>
<input type='radio' name='rating$i' value='2' id='rating_1'>
<input type='radio' name='rating$i' value='3' id='rating_2'>
<input type='radio' name='rating$i' value='4' id='rating_3'>
<input type='radio' name='rating$i' value='5' id='rating_4'>High
</p>
";
}
else if ($row['answer_type']=="Comments") {
echo "<textarea name='answer' cols='' rows=''></textarea>";
}
$i++;
echo "<br />";
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating,answer_freeresponse) values (1,$surveyid,$questionid,'$question','$rating$i','$_POST[answer]')";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
?>
</div>
</div>
<?php }?>
</fieldset>
</div>
<div class="tab-pane" id="tab3">
<fieldset>
<div class="control-group">
<label class="control-label" for="focusedInput">Testimonial about
your care by <?php echo $_SESSION['doctorname'];?>
</label>
<div class="controls">
<textarea name="review" cols="5" rows="5" required></textarea>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-primary"
value="Save & Press Finish"></input>
</div>
</fieldset>
pageCode.php
<?php
session_start();
require_once('config.php');
$con=mysqli_connect(HOST,USER,PASSWORD,DATABASE);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$alias = $_POST['choice'];
if($alias=="yes")
$aliasname = $_POST['pname'];
else
$aliasname = $_POST['alias'];
$sql="INSERT INTO reviews (doctor_id,doctor_name,survey_id,name,email,display_name,overall_review)
VALUES
('$_SESSION[doctorid]','$_SESSION[doctorname]',$_SESSION[surveyid],'$_POST[pname]','$_POST[pemail]','$aliasname','$_POST[review]')";
$result = mysqli_query($con,$sql);
//$result2 = mysqli_query($con,$_SESSION['insertallquery']);
if(!$result) {
echo mysqli_error($result);
}
?>
I actually want to insert the answers into database filled by customers.
You need to store answers in the review table or a separate table its your wish.
but in general, what I understood you have a separate table for questions with the question ids.
So, you need to add column in the review table or a separate table with the question ids as the field names.
and store the respective answers in those column for that user.
For eg. considering adding column in existing reviews table :
INSERT INTO reviews (doctor_id,doctor_name,survey_id,name,email,display_name,overall_review,q1,q2,q3...)
so on according to number of questions you have.
Also, when you add a new question to the questions table you will have to make sure to add a column in this table using ALTER TABLE with the.
For getting the answers as the question are in a while loop, create a array.
and insert those values into mysql using a while loop iterating the array.
The query inside the while loop will be like this :
INSERT INTO reviews (q.$i) VALUES ($answer[$i]) WHERE docter_id='$_SESSION[doctorid]'
You could get the values from IDs/class names or tag names itself then you could store it in an array then you can pass it to your jquery code( $.ajax ) or ($.post) then accept the array variable declared in your javascript
but our tags must contain temp index values example.
Another way is to pass the answers as JSONs then you could use json_encode in php to access the answers
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.