$section = mysql_query("SELECT services.services, services.price FROM services WHERE pet_breed = 'Dog'");
while ($row = mysql_fetch_array($section))
{
echo "<br><input type='checkbox' id=check data-price='".$row['price']."' class='service' name='pet[]' value ='".$row['services']."'";
echo " />";
echo $row['services'];
}
you should use "checked" property in case you have data. for more please see this
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_checked
<input type="checkbox" name="vehicle" value="Car" checked> I have a car
Related
I can't get the input of checkboxes in PHP.
Here is my code:
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' id='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
#this is the part that probably isn't correct.
if(!empty($_POST['check_list'])){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
I would like to get the amount of checkboxes checked.
The checkboxes are created in a loop with the input of a database.
Even if this would work, how would I get the id of all of the checked checkboxes?
It seems you are not using the form to submit. Place your table inside the form
<form action="" method="post">
<?php
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' value='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</form>
You can get the post values
if($_POST){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
Simple form with checkboxes:-
<form name="" action="" method="post">
<input type="checkbox" name="gender[]" value="Male" />Male
<input type="checkbox" name="gender[]" value="Female" />Female
<input type="submit" name="submit" value="Submit" />
</form>
The PHP code to get the selected:-=
if(isset($_POST['gender'])){
$options = $_POST['gender'];
echo implode(',', $options);
}
If you want to pass the id you can do it like
<input type="checkbox" name="gender[2]" value="Male" />Male
<input type="checkbox" name="gender[3]" value="Female" />Female
You can loop through each option
foreach($options as $key => $value){
echo $key.'---'.$value;
}
//$key is the id sepcified, $values is the seected value
I am creating a multiple choice quiz where I will be displaying only 1 question per page, where the answer choices are shuffled to be displayed to the user.
<?php
$con = mysqli_connect("localhost","root","","labquiz");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$res = mysql_query("SELECT multiplechoiceid, question, choice1, choice2, choice3, answer, description FROM multiple_choice");
$ans_array = array('choice1', 'choice2', 'choice3', 'answer');
shuffle($ans_array);
while($row = mysql_fetch_array($res)){
echo $row["multiplechoiceid"]. ".". $row["question"];
echo "<br>";
echo '<input type="radio" name="quizid<?=$row->multiplechoiceid?>" value="$ans_array[0]" required> <?=$ans_array[0]?>';
echo "<br>";
echo '<input type="radio" name="quizid<?=$row->multiplechoiceid?>" value="<?=$ans_array[1]?>"> <?=$ans_array[1]?>';
echo "<br>";
echo '<input type="radio" name="quizid<?=$row->multiplechoiceid?>" value="<?=$ans_array[2]?>"> <?=$ans_array[2]?>';
echo "<br>";
echo '<input type="radio" name="quizid<?=$row->multiplechoiceid?>" value="<?=$ans_array[3]?>"> <?=$ans_array[3]?>';
echo "<br>";
}
?>
The php tags in the echo expressions do not display the answer choices but only the radio buttons are being displayed.
why not separate your php & html code & write like this -
while($row = mysql_fetch_array($res)){
echo $row["multiplechoiceid"]. ".". $row["question"]; ?>
<br>
<input type="radio" name="quizid<?=$row->multiplechoiceid?>" value="$ans_array[0]" required> <?=$ans_array[0]?>
<br>
<input type="radio" name="quizid<?=$row->multiplechoiceid?>" value="<?=$ans_array[1]?>"> <?=$ans_array[1]?>
<br>
<input type="radio" name="quizid<?=$row->multiplechoiceid?>" value="<?=$ans_array[2]?>"> <?=$ans_array[2]?>
<br>
<input type="radio" name="quizid<?=$row->multiplechoiceid?>" value="<?=$ans_array[3]?>"> <?=$ans_array[3]?>
<br>
<?php } ?>
If still face problem then check short code is enabled inside your php.ini file
I am making a student sign-up form using html and php.
First you are asked to insert your name, password and email and when you click submit, it takes you to another page (ChooseDepartment.php) in which you get a list of all departments from my database to choose your own.
Now, I am a total newbie, so here is the part of my php code that I am stuck with in ChooseDepartment.php:
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo </form>;
}
I am trying to make the value of the radio button carry the value of the
the department id so I can then update my database with the student's department which is currently NULL, but I can't figure out how to use both html and php at the very same line correctly! This gives me syntax error!
as you're in PHP, so you don't need to open and close PHP tag.
The reason you're getting Syntax error is just because you're not manipulating string properly.
error is with this line
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
^ here ^ here
So you need to remove the PHP tags and need to concatenate string properly like:
echo '<input type="radio" name="department" value="'.$row['DEPT_ID']. '">';
and with this one
echo </form>;
you're missing quotes around form tag. So it should be,
echo '</form>';
There are some other typos are as well, so your final code will be look like this.
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
//echo "<br />"; add this <br /> tag to next echo
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
//or you can do this way
//echo "<input type='radio' name='department' value='$row[DEPT_ID]'><br />";
//echo "<br />"; appended in upper statement.
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
//echo </form>; closed already(above statement).
}
and without comments, more cleaner :)
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
}
Take a look at string operators
http://php.net/manual/en/language.operators.string.php
You can combine two strings in php with a dot, so that part of your code would become this:
{
echo $row['NAME'];
echo '<input type="radio" name="department" value="'.$row['DEPT_ID'].'">';
echo "<br />";
}
No need to open php tag again
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0) {
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while ($row = mysql_fetch_array($ShowPossibleDep)) {
echo $row['NAME'];
echo '<input type="radio" name="department" value="' . $row['DEPT_ID'] .'">';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo "</form>";
}
I am fetching the data from the MySQL table in a while loop, and inserting the form data into another MySQL table in action page in foreach, but I do not get the correct value of the radio button, I am attaching my code, please help.
<?php
$i = 1;
$j = 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'];
?>
<input type="hidden" name="questionid[]" value="<?php echo $questionid; ?>" />
<input type="hidden" name="question[]" value="<?php echo $question; ?>" />
<?php 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>
";
$i++;
} else if ($row['answer_type'] == "Comments") {
echo "<textarea name='answer[]' cols='' rows=''></textarea>";
$j++;
}
echo "<br />";
?>
</div>
</div>
<?php } ?>
Action File Code
foreach($_POST['questionid'] as $key=>$questionid){
$questionid = $_POST['questionid'][$key];
$answer = $_POST['answer'][$key];
$result3 = mysqli_query($con, "select question,answer_type from questions where question_id=$questionid;");
while($row = mysqli_fetch_array($result3)) {
$question = $row['question'];
$answer_type = $row['answer_type'];
if($answer_type == "Comments") {
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_freeresponse) values(1,$_SESSION[surveyid],$questionid,'$question','$answer')";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
}
else if($answer_type == "Ratings") {
$query2 = "insert into review_details (review_id,survey_id,question_id,question,answer_rating) values(1,$_SESSION[surveyid],$questionid,'$question',$key)";
$result2 = mysqli_query($con,$query2);
if(!$result2) {
echo mysqli_error($result2);
}
}
}
$i++;
}
Output
I want to store the ratings displayed as a radio button, I guess its taking the counter incremented as variable $key, I don't know how to store the values of the radio button.
I guess you will get your radio button value as,
$ratingKey = "rating".$key;
$rating = $_POST[$ratingKey];
Than use $rating in your insert query instead of $key.
INSERT into review_details (review_id,survey_id,question_id,question,answer_rating)
values(1,$_SESSION[surveyid],$questionid,'$question',$rating)
I've got a form with 50 questions. After the user has filled in the form, I want to take them to another page to first cancel / confirm that they are finished with the form. My question is if they select cancel, how do I put back all the fields that has been answered in the form?
EDITED
I've edited my question to show my code because I'm strugling:
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND()";
$result1=mysql_query($sql1);
echo "<form method='post' action='....'>";
while($row1 = mysql_fetch_array($result1))
{
$test_name=$row1['test_name'];
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
$option3=$row1['option3'];
echo "$question";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; }
} //end else if q_type <> mr
echo "<BR>";
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Finish'>";
echo "</form>";
Make the Cancel button a Submit and let it post back to the original questionnaire. In there, you can do like this:
<input type="text" name="q1" value="<?=isset($_POST['q1']) ? $_POST['q1'] : '';?>" />
Or:
<input type="radio" name="q2" value="A" <?=isset($_POST['q2']) && $_POST['q2'] == 'A' ? 'checked="checked"' : '';?>" />
<input type="radio" name="q2" value="B" <?=isset($_POST['q2']) && $_POST['q2'] == 'B' ? 'checked="checked"' : '';?>" />
Same goes for option elements inside a select (selected="selected").
Pass the $_POST or $_GET array to the confirmation page, and if they hit cancel, pass the array back to the page, and fill in the elements.
The easiest solution would be adding:
<INPUT type=button value="Cancel" onClick="history.back();">
When you go back, the form would still be there.
You can just use $_REQUEST['Answer'] in value tag of form.