I have a table that has different names of competences and another one in which we have the competence_ID and the user_ID that has that competence. I have a form in which I submit the competences that apply to a certain user. Now, I want to retrieve these competences and check boxes in case they are valid.
I use this code, but it does not seem to work.
<?php
include "db.php";
$employees=mysql_query("SELECT * FROM asiakas");
echo "<form method='post' action='' id='employeesselection'><select name='select_employee' id='select_employee'>";
while($row=mysql_fetch_array($employees)){
$selected = ($row['Id'] == $_POST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
}
echo "</select><input type='hidden' name='action' value='selectedemployee'>
<input type='submit' value ='submit' name='submit'><br/>";
if(isset($_POST['select_employee'])){
$specific=mysql_query("SELECT Id, Sukunimi,Etunimi from asiakas WHERE Id=".$_POST['select_employee']);
$sp=mysql_fetch_array($specific);
}
if(isset($sp['Etunimi']) && isset($sp['Sukunimi'])){
$comp=mysql_query("SELECT * FROM Competences");
echo "<br/>select competences for: ". $sp['Etunimi'];
$id= $sp['Id'];
$result=mysql_query("SELECT distinct c_ID from User_Competence WHERE e_ID=".$id);
while($test=mysql_fetch_array($result))
{
echo $test['c_ID'];
}
echo "<table><th>valid?</th><th>Competence description</th>";
$counter=0;
$traverse=0;
while($compi=mysql_fetch_array($comp)){
$checked='';
if($test[$traverse]==$counter){
$checked="checked";
$traverse++;
}
$counter ++;
echo "<tr><td><input type='checkbox'" .$checked." name='c[]' value='".$compi['Competence_ID']."'></td><td>".$compi['Competence_Description']."</td></tr>";
}
echo "</table>";
echo "<input type='hidden' name='action' value='selectchecked'>";
echo "<input type='submit' value='submit checks'>";
}
if(isset($_POST) && !empty($_POST)){
print_r($_POST);
}
if(isset($_POST['action']) && $_POST['action']='selectchecked'){
if (isset($_POST['c'])){
$s = $_POST['c'];
foreach ($s as $k => $v) {
if (is_array($v)) {
// array_push($s, implode('', array($v [$what])));
}
};
echo $abc= implode(',', $s);
for ( $a=0;$a<count($s);$a++){
$ar=explode(',',$abc);
echo $var= $ar[$a];
$q=mysql_query("INSERT INTO user_competence(c_ID, e_ID) VALUES ('".$var."','".$id."')");
}
}
}
echo "</form>";
?>
If you put checked as an attribute on the <input type="checkbox"> it will be checked, even if you leave the actual value of the attribute empty.
So change the following parts from...
$checked="checked";
...
echo "<tr><td><input type='checkbox' checked='".$checked."' name='c[]' ...
Into...
$checked="checked='checked'";
...
echo "<tr><td><input type='checkbox' ".$checked." name='c[]' ...
Which will mean if the $test[$traverse$] is true, checked='checked' will be placed into the HTML, otherwise it will be left out.
Related
please help, i'm developing an online quiz application. All the questions and answers will be selected from the database. Where i'm having probkem with is to get the values from the radio button whether checked or not. bellow is the code that generate the questions and answers from database.
if (!isset($_POST['submit'])) {
echo "<form method=post action='#'>";
echo "<table border=0>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["ans1"];
$opt2 = $row["ans2"];
$opt3 = $row["ans3"];
$opt4 = $row["ans4"];
$opt5 = $row["ans5"];
$answer = $row["ans"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td><td>$opt4 <input type=radio name=q$id value=\"$opt4\"></td><td>$opt5 <input type=radio name=q$id value=\"$opt5\">q$id</td></tr>";
}
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
the name of the radio button is
<input type='radio' name='q$id' value='$opt4' />
How do i get the value of the checked radio button?
or is my PHP code wrong?
what i needed is to output what is selected if a radio button is checked.
if(isset($_POST['submit']))
{
$value = $_POST[''];//the value of the radio button, i don't know what to put here
$n = count($value);
for($i=0; $i < $n; $i++)
{
echo $value[$i];
}
}
Try:
if(isset($_POST['submit']))
{
$value = $_POST;//the value of the radio button, i don't know what to put here
$n = count($value);
for($i=0; $i < $n; $i++)
{
echo $value[$i];
}
}
$_POST[''] was wrong. You needed to use just $_POST.
You could also just use foreach here:
if(isset($_POST['submit']))
{
$values = $_POST;//the value of the radio button, i don't know what to put here
foreach($values as $value)
{
echo $value;
}
}
$_POST is just a array containing whatever your form submited. If you had a name textfield you would use:
echo $_POST['name'];
to echo it.
Try
var_dump("<PRE>", $_POST);
And you will see exactly how your form is being organised. Link to documentation
thanks for your response. i was able to come out with this code and it works but i don't know if the structure or the way i solved it is proper or good enough.
<?php
if (!isset($_POST['submit'])) {
echo "<form method=post action='#'>";
echo "<table border=0>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["ans1"];
$opt2 = $row["ans2"];
$opt3 = $row["ans3"];
$opt4 = $row["ans4"];
$opt5 = $row["ans5"];
$answer = $row["ans"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td><input type=radio name=$id value=\"$id $opt1\">$opt1 </td><td><input type=radio name=$id value=\"$id $opt2\">$opt2 </td><td><input type=radio name=$id value=\"$id $opt3\">$opt3 </td><td><input type=radio name=$id value=\"$id $opt4\">$opt4 </td><td><input type=radio name=$id value=\"$id $opt5\">$opt5 <br>";
}
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
?>
what i did was to include the id of the question in the value of the radio button
<input type=radio name=$id value=\"$id $opt1\">
i collect the values of the radio button using $_POST as suggested, i then explode the value into two arrays so the first array will contain the question id and the second will contain the actual value of the radio button (answer).
<?php
elseif (isset($_POST['submit']))
{
var_dump("<PRE>", $_POST);
$sd = $_POST;
foreach($sd as $sd)
{
$tok = explode(" ", $sd, 2);
$w = mysql_query("select * from `questions` WHERE `id` = '$tok[0]'")or die(mysql_error());
if(mysql_num_rows($w) == 1)
{
$sf = mysql_fetch_object($w);
echo $tok[1].".......".$sf->ans."<br>";
}
}
}
?>
it works perfectly but will it give efficient result?
I have a PHP script that allows users to enter grades by selecting a radio button that corresponds to the students grade. It allows them to view the selected grades before they can be finally submitted. I also want the page to have the ability to go back to the selection page and remember the radio buttons that were selected so that the user doesn't have to set all of them again when going back. Here is what I have coded so far, it takes the user back to selection page but doesn't restore the radio button selection.
<?php
session_start();
$script_name = $_SERVER["PHP_SELF"];
if(!isset($_SESSION["course"]) || !isset($_SESSION["course"])) {
$_SESSION["course"] = $_POST["coursename"];
$_SESSION["section"] = $_POST["section"];
}
if(($_SESSION["authenticated"] == true || isset($_POST["back"])) && !isset($_POST["continue"])) {
$course = $_SESSION["course"];
$section = $_SESSION["section"];
$file_name = $course.$section.".txt";
$_SESSION["filename"] = $file_name;
// Open file containing student names.
$fp = fopen($_SESSION["filename"], "r") or die("Could not open file");
$students = array();
$i = 0;
echo "<h2>Grades Submission Form</h2>";
echo "<h2>Course: $course, Section: $section</h2>";
echo "<form action=\"$script_name\" method='post'>";
echo "<table border='1'>";
while (!feof($fp)) {
$line = trim(fgets($fp));
$students[$i++] = $line;
echo "<tr><td>$line</td>";
echo "<td><input type='radio' name=\"$line\" value='A'/>A</td>";
echo "<td><input type='radio' name=\"$line\" value='B'/>B</td>";
echo "<td><input type='radio' name=\"$line\" value='C'/>C</td>";
echo "<td><input type='radio' name=\"$line\" value='D'/>D</td>";
echo "<td><input type='radio' name=\"$line\" value='F'/>F</td>";
echo "</tr>";
}
echo "</table><br>";
echo "<input type='submit' name='continue'/>";
echo "</form>";
} elseif($_SESSION["authenticated"] == true && isset($_POST["continue"]) && !isset($_POST["back"])) {
unset($_POST["continue"]);
$keys = array_keys($_POST);
$values = array_values($_POST);
echo "<h2>Grades to Submit</h2>";
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Grade</th></tr>";
for($i = 0; $i < count($keys); $i++) {
echo "<tr><td>{$keys[$i]}</td><td>{$values[$i]}</td></tr>";
}
echo "</table><br>";
echo "<form action='confirmation.php' method='post'>";
echo "<input type='submit' value='Submit Grades'/>";
echo "</form>";
echo "<form action=\"$script_name\" method='post'>";
echo "<input type='submit' value='Back'/>";
echo "</form>";
} else {
header("Location: main.php");
}
?>
You could serialize() an array containing the radio button states and store it in your session. When you go back, all you have to do is unserialize it and set the data again.
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.
I have been using variable in the name part of the input tag. Now while access answers as selected by users using $_post,It gives error as undefined index.Tell me how to get answers of all questions as selected .
echo "<form method=\"post\" action=\"\">";
$query=mysql_query("select q_detail,q_id from question where category=\"$value2\"",$connection);
if(!$query)
{
echo mysql_error().'query failed';
}
$ans=1;
while($value1=mysql_fetch_array($query))
{
echo "Q-$i"." ";
echo $value1['q_detail']."<br />";
$i++;
$qno=$value1['q_id'];
$query1=mysql_query("select * from answer where ans_id=$qno");
if(!$query1)
{
echo mysql_error().'query failed';
}
while($value2=mysql_fetch_array($query1))
{
$opt=$value2['option1'];
$opt1=$value2['option2'];
$opt2=$value2['option3'];
$opt3=$value2['correct'];
echo "<input type=\"radio\" value=\"$opt\" name=\"$ans\">";
echo "<span class=\"margin\">$opt</span>";
echo "<input type=\"radio\" value=\"$opt1\" name=\"$ans\">";
echo $opt1." ";
echo "<input type=\"radio\" value=\"$opt2\" name=\"$ans\">";
echo $opt2." ";
echo "<input type=\"radio\" value=\"$opt3\" name=\"$ans\">";
echo $opt3." <br /><br />";
$ans++;
}
}
echo"<input type=\"submit\" name=\"submit\" value=\"submit\">";
echo "</form>";
You can make the name of input as array, e.g.:
<input type="radio" value="Blah" name="answers[]" />
and in php code you can access this using the following code:
foreach($_POST['answers[]'] as $answer)
{
echo $answer;
}
Enclose the non-numeric value with single quotes.
$query=mysql_query("select q_detail,q_id
from question where category='$value2'");
Try this:
$ans=1;
if(isset($_POST))
{
echo $_POST['radio_' . $ans];
}
echo "<form method=\"post\" action=\"\">";
$query=mysql_query("select q_detail,q_id from question where category=\"$value2\"",$connection);
if(!$query)
{
echo mysql_error().'query failed';
}
while($value1=mysql_fetch_array($query))
{
echo "Q-$i"." ";
echo $value1['q_detail']."<br />";
$i++;
$qno=$value1['q_id'];
$query1=mysql_query("select * from answer where ans_id=$qno");
if(!$query1)
{
echo mysql_error().'query failed';
}
while($value2=mysql_fetch_array($query1))
{
$opt=$value2['option1'];
$opt1=$value2['option2'];
$opt2=$value2['option3'];
$opt3=$value2['correct'];
echo "<input type=\"radio\" value=\"$opt\" name=\"radio_$ans\">";
echo "<span class=\"margin\">$opt</span>";
echo "<input type=\"radio\" value=\"$opt1\" name=\"radio_$ans\">";
echo $opt1." ";
echo "<input type=\"radio\" value=\"$opt2\" name=\"radio_$ans\">";
echo $opt2." ";
echo "<input type=\"radio\" value=\"$opt3\" name=\"radio_$ans\">";
echo $opt3." <br /><br />";
$ans++;
}
}
echo"<input type=\"submit\" name=\"submit\" value=\"submit\">";
echo "</form>";
I've added the if(isset($_POST)) and before the name I've added some text, because a name as a integer, can be used as a integer index in PHP. Array's can be called like
$ar = array("Name" => "VALUE");
echo $ar[0]; // Outputs VALUE
echo $ar["Name"]; // Outputs VALUE
Will both output "VALUE". So what you're trying to do. Is getting the value of index 1 instead of key 1.
You have to use $_POST['actual radiobutton name'], i. e. whatever the content of $ans is at the time of the echoing.
You can also debug your form after submitting by using this code:
echo "<pre>";
print_r($_POST);
echo "</pre>";
This will show you the contents of the $_POST data so you can see if values are being passed correctly.
Harmiih really helped me with my script (http://stackoverflow.com/questions/7510546/html-form-name-php-variable). Basically I have a table with questions. The form retrieves the questions from the table and then I need the selected answers to go to answer.php. Everything is working with the radio butttons but with the check boxes it's only sending the last selected checkbox. Can someone please help me?
form
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' AND q_type = 'mr' ORDER BY RAND() LIMIT 5";
$result1=mysql_query($sql1);
echo "<form method='post' action='answer.php'>";
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'];
echo "<P><strong>$q_nr $question</strong><BR>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='$option1'>$option1<BR>";
} else {
echo '';
}
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='$option2'>$option2<BR>";
} else {
echo '';
}
} else {
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr]' value='$option1'>$option1<BR>";
} else {
echo '';
}
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr]' value='$option2'>$option2<BR>";
} else {
echo '';
}
}
echo "<BR>";
echo "<BR>";
echo "</p>";
}
echo "<input type='submit' value='Send Form'>";
echo "</form>";
answer.php
<?php
//Key is $q_nr and $answer is selected $option
foreach($_POST['question'] as $key => $answer) {
echo $key;
echo $answer;
}
?>
You can use this as name:
name="question[$q_nr][]"
This will make sure you return an array containing all values of the selected checkboxes.
You need to have different name values in checkboxes.
For example name='question1[$q_nr]' and name='question2[$q_nr]'. The same name works only with grouping elements witch radio buttons are.
Or passing them as arrays:
name='question[$q_nr][1]' and name='question[$q_nr][2]'