I have problems displaying my checkboxes. Can anyone please help? I search the solution online and I tried the codes but it doesn't work.
Here is my codes
<?php
include "mysqli.connect.php";
// Make a MySQL Connection
$retrieveflavor = "SELECT * FROM flavor";
$result = $mysqli->query($retrieveflavor);
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
//echo "<input type='checkbox' name='candyFlavors[]' value=".$row['flavorname']."/>";
//echo "<input type=\"checkbox\" name=\"candyFlavors[]\" value=\"$row[flavorname]\">";
//echo "<input name=\"candyFlavors[]\" type='checkbox' value='"$row[flavorname]"'/>";
echo "<td><img src=".$row['image']." width='240px' height='190px'></td>";
echo "<input type=\"checkbox\" name=\"candyFlavors[]\" value=\"$row[flavorname]\">";
}
?>
I think it should be like this.
echo "<input type=\"checkbox\" name=\"candyFlavors[]\" value=\"".$row['flavorname']."\">";
You missed single quote in $row[flavorname]
echo '<input type="checkbox" name="candyFlavors[]" value="'.$row['flavorname'].'">';
Try this
Related
I echo the value of the radio-buttons from my database and in this form ( a while loop ) is the fastest way to get them. When I do this it sees all the radio-buttons as in the same "group". How do to make it so that it has multiple "groups" so it is possible to answer multiple questions?
I tried adding a div to it but that didn't work.
<form action="Antwoord.php" method="POST">
<?php
$sql = "SELECT * FROM questionlist_choice";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$vraag = $row['Vraag'];
$vraagA = $row['Vraag_keuzeA'];
$vraagB = $row['Vraag_keuzeB'];
$vraagC = $row['Vraag_keuzeC'];
$vraagD = $row['Vraag_keuzeD'];
$vraagE = $row['Vraag_keuzeE'];
$vraagF = $row['Vraag_keuzeF'];
echo "<div>";
echo "<br><p>$vraag</p>";
echo "<input type='radio' name='gender' value='$vraagA'> $vraagA<br>";
echo "<input type='radio' name='gender' value='$vraagB'> $vraagB<br>";
echo "<input type='radio' name='gender' value='$vraagC'> $vraagC<br>";
echo "<input type='radio' name='gender' value='$vraagD'> $vraagD<br>";
echo "<input type='radio' name='gender' value='$vraagE'> $vraagE<br>";
echo "<input type='radio' name='gender' value='$vraagF'> $vraagF<br>";
echo "</div>";
}
}
?>
<input type="submit">
</form>
Sorry I don't understand your question that much, but if you want to GROUP them you need to add index on name tag like this:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
$vraag = $row['Vraag'];
$vraagA = $row['Vraag_keuzeA'];
$vraagB = $row['Vraag_keuzeB'];
$vraagC = $row['Vraag_keuzeC'];
$vraagD = $row['Vraag_keuzeD'];
$vraagE = $row['Vraag_keuzeE'];
$vraagF = $row['Vraag_keuzeF'];
echo "<div>";
echo "<br><p>$vraag</p>";
echo "<input type='radio' name='gender[$i]' value='$vraagA'> $vraagA<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagB'> $vraagB<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagC'> $vraagC<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagD'> $vraagD<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagE'> $vraagE<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagF'> $vraagF<br>";
echo "</div>";
$i++;
}
I use $i as the index, I think it would be better if you use for loop cause it already has $key value, I think using this in while is a bad habit.
This question already has answers here:
How to get input field value using PHP
(7 answers)
Closed 6 years ago.
I have follow the tutorial of it where i want to update my database using two php files.
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action =update.php method=post>";
echo "<td><input type=text name=Cname value='".$row['CustomerName']."'></td>";
echo "<td><input type=number name=size min=1 value='".$row['TableSize']."'></td>";
echo "<td><input type=date name=Adate value='".$row['DateA']."'></td>";
echo "<td><input type=time name=Atime value='".$row['TimeA']."'></td>";
echo "<td><input type=tel name=phonenumber value='".$row['PhoneNumber']."'></td>";
echo "<input type=hidden name=id value='".$row['TableID']."'>";
echo "<td><input type=submit>";
echo"</form></tr>";
}
?>
this is what i use for the first php file
as for the update.php:
<?php
$con = mysqli_connect('127.0.0.1','root','');
mysqli_select_db($con,'restaurant');
$sql = "UPDATE addtable SET CustomerName='$_POST[Cname]', TableSize='$_POST[size]', DateA='$_POST[Adate]',TimeA='$_POST[Atime]',PhoneNumber='$_POST[phonenumber]', WHERE TableID=$_POST[id]";
if(mysqli_query($con,$sql))
header("refresh:1; url=AssignBooking.php");
else
echo "Not Update";
?>
but the $sql line just doesn't work as it says that
Undefined index: Cname and other indexes too.
put quotes outside the post variable:
$sql = "UPDATE addtable SET CustomerName='".$_POST['Cname']."', TableSize='".$_POST['size']."', DateA='".$_POST['Adate']."',TimeA='".$_POST['Atime']."',PhoneNumber='".$_POST['phonenumber']."', WHERE TableID=".$_POST['id'];
According to your code put the name attributes value ' single quote.
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action =update.php method=post>";
echo "<td><input type=text name='Cname' value='".$row['CustomerName']."'></td>";
echo "<td><input type=number name='size' min=1 value='".$row['TableSize']."'></td>";
echo "<td><input type=date name='Adate' value='".$row['DateA']."'></td>";
echo "<td><input type=time name='Atime' value='".$row['TimeA']."'></td>";
echo "<td><input type=tel name='phonenumber' value='".$row['PhoneNumber']."'></td>";
echo "<input type=hidden name="id" value='".$row['TableID']."'>";
echo "<td><input type=submit>";
echo"</form></tr>";
}
?>
Put quotes accordingly
UPDATE addtable SET CustomerName='".$_POST['Cname']."',TableSize='".$_POST['size']."', DateA='".$_POST['Adate']."',TimeA='".$_POST['Atime']."',PhoneNumber='".$_POST['phonenumber']."' WHERE TableID=$_POST['id'];
Here is the scenario. in my database, I have like 4 questions. each questions have individual 5 radio buttons. I tried to retrieve the information from database. it shows my list of questions and radio buttons individually, but the BIG problem here is they are in the same group. example. in question 1, I picked 1st radio button, then in question 2, I picked the 2nd radio button. in question 1, the radio button I choose disappeared. so basically. the whole loop have single radio button name. how do I fix this dynamically? like auto change of radio-button-name for each question?
<form method='post' action='test.php'>
<?php
include 'db_connect.php';
$query = "SELECT * FROM test";
$result = $conn->query($query);
$num_results = $result->num_rows;
#if ($num_results > 0) {
#}
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
echo "<input type='radio' name='question_button' value='1'>";
echo "<input type='radio' name='question_button' value='2'>";
echo "<input type='radio' name='question_button' value='3'>";
echo "<input type='radio' name='question_button' value='4'>";
echo "<input type='radio' name='question_button' value='5'>";
echo "<br>";
}
$testbutton = isset($_POST['question_button']) ? $_POST['question_button'] : "";
if (isset($_POST['submit'])) {
echo $testbutton;
}
?>
<html>
<input type='submit' name='submit' value='submit'>
</form>
</html>
P.S. Edit. my original intention is to add or get the sum of the radio buttons. what syntax should i use?
to change button name dynamically
change like following
$question=0;
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
$question++;
echo "<input type='radio' name=".$question." value='1'>";
echo "<input type='radio' name=".$question." value='2'>";
echo "<input type='radio' name=".$question." value='3'>";
echo "<input type='radio' name=".$question." value='4'>";
echo "<input type='radio' name=".$question." value='5'>";
echo "<br>";
}
$question is just for example,
you can change redio button name according to your need.
I can't see what your database query is producing, but at a guess, I'd say you should be assigning a radio button id to your question in the database field. Then, when you get the results back, you can assign it with something like:
while ($row = $result->fetch_assoc()) {
extract($row);
echo "<input type='radio' name='".$row['question_name']."' value='".$row[question_id]."'>";
echo "<br>";
}
This should make every radio box unique so long as the data in the database is unique. If this doesn't help, may I see the results of the database query and I can edit my answer.
Simply give the radio button a group name differently as bellow example
// 1. Get the handler for counter.
$counter = 1;
// 2. Iterating through the results.
while ($row = $result->fetch_assoc()) {
$name = "question_" . $counter . "_button";
extract($row);
echo $row['test1'];
echo "<input type='radio' name=$name value='1'>";
echo "<input type='radio' name=$name value='2'>";
echo "<input type='radio' name=$name value='3'>";
echo "<input type='radio' name=$name value='4'>";
echo "<input type='radio' name=$name value='5'>";
// Increment the counter by 1 for each question.
$counter += 1;
}
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\"]'>";
I am building a php based quiz for website. I am a beginner. I coded for getting the questions and options from database and display them in the form in my page.when the user selects the option i want the the answers to be inserted to my database.I wrote some bsic code but its not working pls help. Here is my code
The table for question is having 7rows(qid,question,optiona,optionb,optionc,optiond,answeroption),answers table is having simple2rows(qid,answer)
if(isset($_POST['next']))
{
$a=$_POST['a'];
}
if(!isset($a))
{
$a=0;
}
include('connection.php');
mysql_query("INSERT INTO answers (username,qid, option)
VALUES ($username,a-1,'$_POST('option'))");
$sql1="SELECT * FROM exam1 LIMIT 1 OFFSET $a";
$result=mysql_query($sql1);
echo "<form method='post' action='quiz.php'>";
while ($row = mysql_fetch_array($result))
{
echo $row['question']. "<br/>";
echo "<input type='radio' value='optiona' name='option'>" .$row['optiona'];
echo "<input type='radio' value='optionb' name='option'>" .$row['optionb'];
echo "<input type='radio' value='optionc' name='option'>" .$row['optionc'];
echo "<input type='radio' value='optiond' name='option'>" .$row['optiond']; "<br/>";
}
$c=$a-1;
$b=$a+1;
echo "<input type='hidden' value='$c' name='a'>";
echo "<input type='submit' name='previous' value='previous'> ";
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='submit' name='next' value='next'> ";
echo "<input type='reset' name='reset' value='Reset'>";
echo "</form>";
?>
You need to study up on basic PHP syntax. $_POST is not a function. It's an array. e.g.
$var = $_POST['var'];
^-- ^--- note the bracketing.
Even if the code DID work, you'd be wide open to SQL injection attacks.
Big big atention! I hope this is just an test code note final one!
Your code is Inject Vulnerably here $a=$_POST['a'];
I recomend to replace with this one $a= filter_input(INPUT_POST, 'a', FILTER_SANITIZE_STRING); this is an escaped from attacks!
I hope it hepls you