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.
Related
My idea is very simple, I will have a search box and a submit button.
When user key in the keyword and click on the submit button, results will be shown below with an additional button. Now my problem is I have no idea on how to make the button to be located at bottom right of the table populated.
Please consider the below code for my situation:
<input type="text" name="criteriaInput" style="width: 300px;"> <input type="submit" name="submit" value="GO" />
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['inquiryMethod'])){
error_reporting(0);
$sql = 'SELECT
*
FROM
table
WHERE
fullname REGEXP \''.$_POST['criteriaInput'].'\'' ;
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("mysql",$server);
$query = mysql_query($sql);
echo "<table class=\"striped\">";
echo "<tr class=\"header\">";
echo "<td>Full Name</td>";
echo "<td>ID</td>";
echo "<td>ID Type</td>";
echo "<td>Issuance Country</td>";
echo "<td>Class</td>";
echo "</tr>";
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[fullname]."</td>";
echo "<td>".$row[id]."</td>";
echo "<td>".$row[id_type]."</td>";
echo "<td>".$row[issuance_country]."</td>";
echo "<td>{$row['class']}</td>";
echo "</tr>";
}
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</table>";
}else{
echo "Please select one of the criteria!";
}
}
?>
The submit button with value "Create" did successfully created on existence of data, however it's aligned on top left of the table.
Kindly advice Thank you.
You need to put your button into a table row and cell.
echo "<tr>";
echo "<td colspan=\"5\">"
echo "<form method=\"post\" action=\"CIF_InquiryAction.php\">";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
echo "</form>";
echo "</td>"
echo "</tr>";
Also, your form should probably move to be outside your table.
Editing to show input outside of table:
echo "</table>";
echo "<input type=\"submit\" name=\"create\" value=\"Create\" />";
I'm trying to create a simple PHP page that will Display a number starting with 0. It will then ask for a number input, then add the inputted number to the beginning number. and keep adding the number that is input in the text box. Here's what I have so far... I can get the first variable initialized and get the input from a form box. I just can't seem to add them together and keep a running total. Thanks in advance.
<?php
$_POST['number1'];
echo "Current number is ".$_POST['number1'];
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number2']."<br>";
$sumtotal = $_POST['number1'] + $_POST['$number2'];
echo "Your new total is ".$sumtotal;
$_POST['number1'] == $_POST['number2'];
?>
Using input type HIDDEN:
<?php
if(isset($_POST['submit'])){
$total=$_POST['number1']+$_POST['number2'];
echo "Your entered number is ".$_POST['number2']."<br>";
}
else {
$total=0;
}
echo "Current total number is ".$total;
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='hidden' name='number1' value='$total'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$total;
?>
Using SESSION:
<?php
session_start();
if(empty($_SESSION["total"])){
$_SESSION["total"]=0;
}
if(isset($_POST['submit'])){
$total=$_SESSION["total"];
$total=$total+$_POST['number2'];
$_SESSION["total"]=$total;
echo "Your entered number is ".$_POST['number2']."<br>";
}
echo "Current total number is ".$_SESSION['total'];
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$_SESSION['total'];
?>
Both works and have the same output, but with different approach from one another.
You could add another hidden input field that keeps the last number that was posted by you.
<input style="display: none" name="tmp_number" value="<?php echo $tmp_number; ?>">
Try (edited for all details and errors)
<?php
$input = $sum = 0;
if (isset($_POST)) {
$input = $_POST['number1'];
$prev_sum = $_POST['prev'];
$sum = $input + $prev_sum;
}
echo "Current number is ".$input;
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number1'>";
echo "<input type='hidden' name='prev' value='$sum' >";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number1']."<br>";
echo "Your new total is ".$sum;
?>
Please refer to the image below:
http://i.stack.imgur.com/6hBPC.png
For instance, if a user clicks the button on the row which says "You have a quiz for math", the "Quiz ID" value of THAT row would then be passed to another PHP file.
Here's my current code:
<?php
$con=mysqli_connect("127.0.0.1", "root", "", "quizmaker");
if (mysqli_connect_errno($con))
{
echo "MySqli Error: " . mysqli_connect_error();
}
$now=date("m/d/Y");
$sql=mysqli_query($con,"SELECT * FROM quiz_query WHERE quiz_date='$now'");
$count=mysqli_num_rows($sql);
if($count>=1)
{
echo "<table border='1' width='50%'>";
echo "<form action='answer_quiz.php' method='post'>";
echo "<tr>
<td>You have a pending quiz!</td><td> </td><td> </td>
</tr>";
$number=1;
while($result=mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>You have a quiz for " . $result['subject'] . "</td>";
echo "<td>Quiz ID: " .$result['quiz_ID']. "</td>";
echo "<td><input type='submit' name='button' id='button' value='Take Quiz'>";
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>";
echo "</td>";
echo "</tr>";
$number++;
}
echo "</form>";
echo "</table>";
}
else
{
"You have no quiz! :D";
}
mysqli_close($con);
?>
Move this line:
echo "<form action='answer_quiz.php' method='post'>";
Inside of the while loop.
Also, change
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>"
with
echo "<input type='hidden' name='quizId' value='$result[quiz_ID]'>"
Now, in answer_quiz.php you'll receive $_POST['quizId'] with the value you need.
Change your while to :
while( $row = $result->fetch_array(MYSQLI_ASSOC)){
echo $row['subject'];
}
You are forgetting quotes around your variable:
Instead of
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>";
It should be
echo "<input type='hidden' name='quiz[$number]' value='$result[\"quiz_ID\"]'>";
For some reason when I call a function to create a form, the drop down menus aren't sticky and the browser forces users to click into the first text field and tab through the rest. It won't let them mouse through the fields. This is only happening in FF, not IE or Chrome. The forms I'm including are just basic html and only php pages I include are doing this.
Here is one function:
function addNoteUI($keyword) {
echo "<div id='search_result_right'>";
echo "<center><div id='enter_note_header'>Assign a Salesperson</div></center><p>";
echo "<form id='response' action='notes_add.php' method='post'>";
echo "<label for='mod_num'>MOD Initials: <label>";
echo "<input type='text' name='mod_num' size='2' maxlength='4'><p>";
echo "<label for='sales_num'>Assigned to Sales Person: <label>";
echo "<input type='text' name='sales_num' size='2' maxlength='4'><p>";
echo "<input type='hidden' name='question_num' value='$keyword'>";
echo "<label for='response'>Note</label><br>";
echo "<textarea name='response' cols='30' rows='7 maxlength='510'></textarea><p>";
echo "<input type='submit' value='Assign'>";
echo "</form>";
echo "</div>";
Here is the other:
function changeDept() {
include 'ask_search.php';
echo "<div id='search_result'>";
echo "<form action='change_dept.php' method='post'>";
echo "<label for='current_num'>Enter the Question Number to be Changed: <label>";
echo "<input type='text' name='current_num' size='4'><p>";
echo "<label for='store'>Select New Store/Department: <label>";
echo "<select name='store'>";
echo "<option>Please Select</option>";
echo "<option value='Albany'>Sales (Albany Store)</option>";
echo "<option value='Saratoga'>Sales (Saratoga Store)</option>";
echo "<option value='Web Sales'>Sales (TaftFurniture.com)</option>";
echo "<option value='Financing'>Financing</option>";
echo "<option value='Customer Service'>Customer Service</option>";
echo "<option value='Delivery'>Delivery</option>";
echo "<option value='HR'>Human Resources</option>";
echo "<option value='Web Contact'>Website Comment</option>";
echo "<input type='submit' value='Change' id='dropdown'>";
echo "</select></form></div>";
}
Thanks in advance.
Your labels are not closing properly:
echo "<label for='mod_num'>MOD Initials: <label>";
Should be:
echo "<label for='mod_num'>MOD Initials: </label>";
Also, in the second example, you have an input inside the select. The input must be outside:
echo "<option value='Web Contact'>Website Comment</option>";
echo "<input type='submit' value='Change' id='dropdown'>";
echo "</select></form></div>";
Should be:
echo "<option value='Web Contact'>Website Comment</option>";
echo "</select>";
echo "<input type='submit' value='Change' id='dropdown'></form></div>";
And another one, you're not closing your P tags:
echo "<input type='text' name='mod_num' size='2' maxlength='4'><p>";
Should be:
echo "<p><input type='text' name='mod_num' size='2' maxlength='4'></p>";
Try to be more careful with your tags. Some browsers are more forgiving about malformed HTML, but others are not.
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]'