html/php forms with survey database - php

I hope someone can help me! I try to make a form with input from mysql. I want a statement to come from a database. Following this I want a user to be able to give a value 1-7(strongly disagree - strongly agree). I want the number of questions be based on my MySQL query. The main reason for this is that I can reuse the code for different types of surveys.
The problem is: I can't get the questions into a single form with separated values, linked to the question. I'm guessing it has something to do with the name= part. I think the name should be related to the database query somehow but I can't find out what the solution is.
If someone could help me I would be very thankful. Also if someone knows a good book or online course/video explaining how to make a online survey tool it would be very helpful.
This is what I have so far:
<?php
echo '<form action="results.php" method="post">';
global $connection;
$sql = "SELECT * FROM questions";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["question"];
echo "<br>";
echo ' <input type="radio" name="likkert" value=1>
<input type="radio" name="likkert" value=2>
<input type="radio" name="likkert" value=3>
<input type="radio" name="likkert" value=4>
<input type="radio" name="likkert" value=5>
<input type="radio" name="likkert" value=6>
<input type="radio" name="likkert" value=7>
<br>';
}
} else {
echo "0 results";
}
echo '<input type="submit"></form>';
?>

Assuming the fact that you have an id column, associated with each question, in questions table, you have to include this id column value (which should be unique) with each question's rating options, like this:
// your code
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["question"];
echo "<br />";
?>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=1>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=2>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=3>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=4>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=5>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=6>
<input type="radio" name="likkert[<?php echo $row["id"]; ?>]" value=7>
<br />
<?php
}
} else {
echo "0 results";
}
// your code
And after form submission, process the questions and their corresponding ratings like this:
foreach($_POST['likkert'] as $questionId => $rating){
// your code
}

Related

PHP The value captured from the radio button is "On"

I'm trying to capture question ids and their answers. The problem
is that the value is "ON" when I print the array, it has the
question number correctly in the first cell but then the value in the
second cell is "ON". Here is my code:
function get_questions($quiz_id)
{
include'connection.php';
$stmt = $conn->prepare("select id,question,option1,option2,option3,option4,answer from questions where quiz_id=?");
$stmt->bind_param("i",$quiz_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($qid,$question,$option1,$option2,$option3,$option4,$answer);
$num_of_rows = $stmt->num_rows;
while($stmt->fetch()) {
echo $question;
echo "<br/>";
?>
<form method="POST"action="">
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="x"><?php echo $option1;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="y"><?php echo $option2;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="z"><?php echo $option3;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="f"><?php echo $option4;?><br/>
<!--<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option2;?>"><?php echo $option2;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option3;?>"><?php echo $option3;?><br/>
<input type="radio" name="radio[<?php echo $qid; ?>]" ]value="<?php echo $option4;?>"><?php echo $option4;?><br/>
-->
<?php
}
?>
<input type="submit" name="submit" value="submit" >
</form>
<?php
if(isset($_POST['submit'])) {
$answers=$_POST['radio'];
print_r($answers);
// Iterate through each answer
}
}
Your problem is a simple syntax error:
]value="x">
This square bracket is changing the name of the attribute from value to ]value, so there is no value to be captured upon submission.
Also, make sure to move the opening form tag outside of your loop:
while($stmt->fetch()) {
...
<form method="POST"action=""> // <- this here opens a new tag with each iteration
because you close the tag outside the loop, meaning you're not creating valid html.
And while we're here, I'd suggest the alternative syntax when you have mixed php and html code:
<form method="POST" action="">
<?php while ($stmt->fetch()): ?>
<input type="radio"...
...
<?php endwhile ?>
<input type="submit" name="submit" value="submit" >
</form>
It makes it more readable and easier to follow than having to match curly braces.

Getting radio button value and storing in php

this is radio button code
now what if radio button does not have a constant name how would i store the data in database because to store the data in database we will need a name of form attribute
$sql1="select * from questions where email='". $_SESSION['email'] ."'";
$row=mysqli_query($conn,$sql1);
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer4'];?>
if we know the name of radio button we can access it using
<input type="radio" value="<?php echo $result['answer4'];?>" name="name"><?php echo $result['answer4'];?>
$name=$_POST['name'];
but in the above code name of radio button is not fixed.questions is the table which consists of qid and questions with multiple options that is answer1,answer2 etc
i want to store the option select by user into database.for which i need to know the name of radio button
how should i use post in this case
$name=$_POST['what should go in here'];
You can get the radio button value along with question ID as:
Basic Example:
<?php
$array = array(1,2); // your Question ID array
?>
Your Form:
<form method="post" action="">
<?php
foreach ($array as $key => $qid) {
?>
<input type="radio" value="1" name="radio[<?=$qid?>]">
Answer 1
<input type="radio" value="2" name="radio[<?=$qid?>]">
Answer 2
<input type="radio" value="3" name="radio[<?=$qid?>]">
Answer 3
<input type="radio" value="4" name="radio[<?=$qid?>]">
Answer 4
<?php
echo "<br/>";
}
?>
<input type="submit" name="submit">
</form>
In PHP:
<?php
if(isset($_POST['submit'])) {
$query = array();
foreach ($_POST['radio'] as $key => $value) {
$query[] = "('$value','$key')";
}
$sql = "INSERT INTO table (answer,questionID) VALUES ";
$sql .= implode(",", $query);
echo $sql;
}
?>
In this example query look like:
INSERT INTO table (answer,questionID) VALUES ('2','1'),('3','2')
Few Suggestions:
- Your code is open for SQL Injection, you must need to prevent your code with SQL Attack and this reference will help you to understand: How can I prevent SQL injection in PHP?
- Make sure your column name and table name not having any conflict, currently, you are using same name for both.
Update with Your Code:
<?php
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="radio[<?php echo $result['qid'];?>]">
<?php echo $result['answer4'];?>
<?
}
?>
In PHP:
<?php
if(isset($_POST['submit'])) {
$query = array();
foreach ($_POST['radio'] as $key => $value) {
$query[] = "('$value','$key')";
}
$sql = "INSERT INTO table (answer,questionID) VALUES ";
$sql .= implode(",", $query);
echo $sql; // run this query in mysqli_query()
}
?>
Few More Instructions:
- Change the table name as per your table name
- Change the column name according to your column.
- Use INSERT query at once, no need to use it inside the loop.
Create an array at the end of while loop like this :
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>
<input type="radio" value="<?php echo $result['answer1'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer2'];?>
<input type="radio" value="<?php echo $result['answer3'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer3'];?>
<input type="radio" value="<?php echo $result['answer4'];?>" name="<?php echo $result['qid'];?>"><?php echo $result['answer4'];?>
<? $names[] = $result['qid'];
}
if(isset($_POST['submit'])) {
for ($i=0; $i<count($names); $i++) {
if(isset($_POST[$names[$i]]) {
$rate = $_POST[$names[$i]];
$sql ="INSERT INTO answer (answer, qid) VALUES (".$rate.", ".$names[$i] .")";
mysqli_query($con, $sql);
}
}
}
?>

Dynamic radio button with self-post using PHP & MySQL

I'm drawing data from a MySQL database that dynamically places a question with 4-5 radio button choices for the answer. These radio buttons all belong to the same group, $quest_name. The first pass of the while statement will create 4 radio buttons belonging to radio group "question_1".
It creates 30-40 of these questions on a page, each with 4 radio buttons. I want the user to fill in all there answers and the page to post back to itself with the users answers still selected and then display if they were correct or not (functionality I still have to add).
I'm trying to follow http://www.w3schools.com/php/php_form_complete.asp as an example, but use a dynamically created radio button name instead.
This is what I have thus far:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$quest_num = $row["id"];
$question = $row["question"];
$option_1 = $row["option_1"];
$option_2 = $row["option_2"];
$option_3 = $row["option_3"];
$option_4 = $row["option_4"];
$option_5 = $row["option_5"];
$answer = $row["answer"];
$quest_name = "question_" . $row["id"];
echo "(" . $quest_num . ") " . $question . "<br>";
?>
<label>
<input type="radio" name=<?php echo $quest_name ?>
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
value=<?php echo $option_1 ?>><?php echo $option_1 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_2 ?>><?php echo $option_2 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_3 ?>><?php echo $option_3 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_4 ?>><?php echo $option_4 ?>
</label>
<br>
<br>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
<input type="submit">
</form>
The part causing me grief so far is:
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
I have also tried:
<?php if (isset($quest_name) && $quest_name == $option_1) echo "checked"; ?>
and:
<?php echo (isset($quest_name) && $quest_name == $option_1) ? "checked" : ""; ?>
How do you post back to the same page what they've selected? Like in this case I'm trying to say if "question_1" is set and "question_1" is equal to "converter" (the first radio button option) then have it checked when submit button is clicked.
I'm not that good at web development, but I'm trying to create a website to help my fellow electrical technician classmates.
Thanks for any help.
EDIT :
Using this line of code fixed the issue:
<?php if(isset($_POST[$quest_name]) && $_POST[$quest_name]==$option_1) { echo 'checked="checked"'; } ?>
What you need is called Radio Group. In HTML layer it is created with same name for all and different values for each like this:
<p>
<label>
<input type="radio" name="RadioGroup1" value="Value1" id="RadioGroup1_0">
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="Value2" id="RadioGroup1_1">
Radio</label>
<br>
</p>
And when you want to get the user input in php layer you go like this:
<?php
//check if Radio Group 1 is set
if(isset($_POST['RadioGroup1'])) {
// print the value of Radio Group 1 choice
echo $_POST['RadioGroup1'];
}
?>
When you want to create a Selected Radio in HTML layer you go like this:
<input name="RadioGroup1" type="radio" id="RadioGroup1_1" value="radio" checked="checked">
So you have to check if user inputs the value of which radio like this:
<label>
<input type="radio" name="RadioGroup1" value="value1" id="RadioGroup1_0" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value1') { echo ' checked="checked"'; } ?>>
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="value2" id="RadioGroup1_1" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value2') { echo ' checked="checked"'; } ?> >
Radio</label>
You could use the following:
<input type="radio" name="<?php echo $quest_name ?>" value="<?php echo $option_1 ?>"
<?php if (isset($quest_name) && ($quest_name == $option_1)) echo "checked"; ?> />

Can't access array via $_POST

I have the following html code:
<form method="post" action="arrayplay.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
And the following PHP script:
//arrayplay.php
foreach ($_POST['todelete'] as $id)
{
echo $id . "<br/>";
}
?>
It is supposed to echo out each element value but instead I get an error. I am getting really frustrated. If I use:
<form method="post" action="arrayplay.php">
<?php
$dbc= //connection
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<input type="checkbox" value="' . $row['id'] . '" name="todelete[]" />';
echo $row['first_name'];
echo ' ' . $row['last_name'];
echo ' ' . $row['email'];
echo '<br />';
}
mysqli_close($dbc);
?>
<input type="submit" name="submit" value="Remove" />
</form>
It works perfectly fine! Why? The first (hard coded html) holds the exact same value as the one that retrieves them from the database. I am having a real hard time understanding retrieving values from an array with $_POST. Why does name=foo[] create an array? Is it an associative or numeric array? I'm sorry for all of the questions, I'm just really ready to pull my hair out.
if you just named the input foo it would only get one value. because square brackets are commonly used for arrays, foo[] is how in the html form, you indicate an array. of course on the PHP side you just call it foo as you are aware from your working example.
I've tested this and it should work:
<?php
if ($_POST['delete']) {
foreach ($_POST['todelete'] as $id) {
echo $id.' selected<br />';
}
}
?>
<form method="post" action="arrayplay.php">
<input type="checkbox" value="1" name="todelete[]"/>
<input type="checkbox" value="2" name="todelete[]"/>
<input type="checkbox" value="3" name="todelete[]"/>
<input type="checkbox" value="4" name="todelete[]"/>
<input type="submit" value="delete" name="delete"/>
</form>
If you're still having troubles, you can try:
<?php
if ($_POST['delete']) {
for ($i = 0; $i < 4; $i++) {
if (isset($_POST['todelete'][$i])) {
echo $_POST['todelete'][$i].' selected<br />';
}
}
}
?>

getting the value posted in the form in the array

I am making and computerized examination system in which a student gets 50 questions and three choices for each question. And I have displayed the question in tabs in which each tab contains 10 questions. I have made the form but confused to get the posted value.
Here is my code:
<div id="page-wrap">
<div id="tabs">
<ul>
<li>Questions 1-10</li>
<li>Questions 11-20</li>
<li>Questions 21-30</li>
<li>Questions 31-40</li>
<li>Questions 41-50</li>
</ul>
<form method="post" action="result.php" >
<?php
$data = $objQuestion_set->selectDetail($exam_id, $registration_id);
$objQuestion_bank = new Question_bank();
?>
<div id="fragment-1" class="ui-tabs-panel ui-tabs-hide">
<?php
$value[] = $data['question_1'];
$value[] = $data['question_2'];
$value[] = $data['question_3'];
$value[] = $data['question_4'];
$value[] = $data['question_5'];
$value[] = $data['question_6'];
$value[] = $data['question_7'];
$value[] = $data['question_8'];
$value[] = $data['question_9'];
$value[] = $data['question_10'];
for($i = 1; $i <=10; $i++)
{
$question = $objQuestion_bank->selectDetail($value[$i-1]);
echo "<div><p>".$i . '.' . $question['question']."</p>";
echo "<br />";
?>
<input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>" value="<?php echo $question['answer_1'];?>" /><label for="answer_1"><?php echo $question['answer_1'];?></label>
<input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>" value="<?php echo $question['answer_2'];?>" /><label for="answer_1"><?php echo $question['answer_2'];?></label>
<input type="radio" id="answer_1_2" name="answer_<?php echo $i;?>" value="<?php echo $question['answer_3'];?>" /><label for="answer_1"><?php echo $question['answer_3'];?></label>
<input type="hidden" id="question_id_<?php echo $i;?>" name="question_id_<?php echo $i;?>" value="<?php echo $question['question_id']; ?>"class="hidden_element" />
<?php
echo "</div>";
echo "<br /><br />";
}
?>
</div>
Now i want to get the posted value of question_id which is hidden field and print it.
You haven't really given much information, and there are some errors in your posted code, so I'm going to answer this generally and leave it to you to try to integrate it.
You are assigning essentially useless names to your form values that make it incredibly complicated to retrieve values in a meaningful way. PHP's argument parsing provides useful abilities to parse arguments into arrays, so I would recommend using them.
For instance, if you had the following input boxes and names in your form, the POSTed data would be very straightforward.
<input type="radio" name="values[<?php print $i; ?>][answer]" value="1">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="2">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="3">
<input type="radio" name="values[<?php print $i; ?>][answer]" value="4">
<input type="hidden" name="values[<?php print $i; ?>][question]" value="10">
Or you could just have this if the $i values isn't important.
<input type="radio" name="values[][answer]" value="1">
<input type="radio" name="values[][answer]" value="2">
<input type="radio" name="values[][answer]" value="3">
<input type="radio" name="values[][answer]" value="4">
<input type="hidden" name="values[][question]" value="10">
Both of these would make your $_POST array into a nice nested array like the following.
<?php
$_POST['values'] == array(
array(
'answer' => 3,
'question' => 1
),
array(
'answer' => 2,
'question' => 2
)
)
You haven't actually shown how you are processing the submitted values, so I can't really give more information, but this is the best way to structure form data for easy processing.
This page has some very basic examples.
http://www.askaboutphp.com/23/parsing-url-queryingstring.html

Categories