OK this is my script:-
<form action="results.php" method="post">
<?php mysql_select_db($database, $databasename) or die("Opps some things went wrong");
$sqlQueryTestDisplay = mysql_query("SELECT * FROM questions WHERE test_id='$testtaken_id' ORDER BY question_id ASC");
$i = 0;
while($DisplayItems = mysql_fetch_array($sqlQueryTestDisplay))
{
$i = $i + 1;
$question_id = $DisplayItems['question_id'];
$question = $DisplayItems['question'];
$opta = $DisplayItems['opta'];
$optb = $DisplayItems['optb'];
$optc = $DisplayItems['optc'];
$optd = $DisplayItems['optd'];
$answer[$i] = $DisplayItems['answer'];
$thisAnswer = $answer[$i];
echo '<li>'.$question.'</li>';
echo '<p>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_0" />'.$opta.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_1" />'.$optb.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_2" />'.$optc.'</label>';
echo '<label><input type="radio" name="optans'.$i.'" value="radio" id="RadioGroup'.$i.'_3" />'.$optd.'</label>';
echo '<input name="ans'.$i.'" type="hidden" value="'.$thisAnswer.'" />';
echo '</p>';
}
echo '<input name="total_questions" type="hidden" value="'.$i.'" />';
echo '<input name="test_id" type="hidden" value="'.$TestID.'" />';
?>
<input name="submittest" type="submit" />
</form>
As you can see i am using array to store values in different fields. Now on the next page i.e on my result.php page I am writing this:-
<?php
if(isset($_POST['submittest']))
{
global $ans1;
$TotalQuestions = $_POST['total_questions'];
$TestID = $_POST['test_id'];
$TestResult = 0;
for ($i=1; $i<=$TotalQuestions; $i++)
{
$ansValue = 'ans'.$i;
$optansValue = 'optans'.$i;
$ans = $_POST[$ansValue];
$optans = $_POST[$optansValue];
if ($ans == $optans)
{
$TestResult = $TestResult + 1;
}
}
$st_id = $row_Recordset1['id'];
mysql_select_db($database, $databasename) or die("Opps some things went wrong");
$sqlQueryInsertResult = mysql_query("INSERT INTO results (student_id, test_id, test_result) VALUES ('$st_id', '$TestID', '$TestResult')");
header('location:results.php');
}
?>
Now my script is not reading ans1, ans2.....and so on AND even quesans1, quesans2.....and so on.
I think problem is in the way i am calling the array using the $_POST method.
Is the syntax correct, how can i FIX it... Please help :|
All of your radio buttons are returning "radio" as their value. Make them return the answer and you should be OK
Change your radio definitions to:
'<INPUT type="radio" name="optans'.$i.'" value="'.$opta.'" >'.$opta.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optb.'" >'.$optb.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optc.'" >'.$optc.'</INPUT>'
'<INPUT type="radio" name="optans'.$i.'" value="'.$optd.'" >'.$optd.'</INPUT>'
Might be worth not putting the answer onto the page as a hidden field - makes a quiz quite easy. Do another SQL query to check the answers when they come back
Use some more loops to display the radio buttons and you can shorten your code a bit.
0Change your test options to this:
echo '<label><input type="radio" name="optans'.$i.'[]" value="1" id="RadioGroup'.$i.'_0" />'.$opta.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="2" id="RadioGroup'.$i.'_1" />'.$optb.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="3" id="RadioGroup'.$i.'_2" />'.$optc.'</label>';
echo '<label><input type="radio" name="optans'.$i.'[]" value="4" id="RadioGroup'.$i.'_3" />'.$optd.'</label>';
and your processor to this:
<?php
if(isset($_POST['submittest']))
{
global $ans1;
$TotalQuestions = $_POST['total_questions'];
$TestID = $_POST['test_id'];
$TestResult = 0;
for ($i=1; $i<=$TotalQuestions; $i++)
{
$ans = $_POST['ans'.$i];
$optans = $_POST['optans'.$i];
for ($j=0;$j<count($optans);$j++) {
if ($optans[$j]==$ans) {
$TestResult = $TestResult + 1;
}
}
}
This is a very insecure way to compare test answers. Someone could easily View Source and see the correct answers. You should validate the test answers after the $_POST
Related
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);
}
}
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im wanting to repeat radio buttons using php. Below is the html form
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
<input type="radio" name="RadioGroup1" value="1">1
Basically, i have 4 groups of questions with rating 1-6 (how would i do it so i dont need to write out all this html code over and over, is there a shorthand method using php so that the selected one also goes into a mysql database too?
Completely confused and new to php, any help would be great.
Look at loop for
//$i => groups (/4)
//$a => radio buttons (/6)
for($i = 1; $i <= 4; $i++){
for($a = 1; $a <= 6; $a++){
echo '<input type="radio" name="RadioGroup'.$i.'" value="'.$a.'">'.$a.'<br/>';
}
echo '<br/><br/>';
}
And the function
function loopMe($group, $answer){
for($i = 1; $i <= $group; $i++){
for($a = 1; $a <= $answer; $a++){
echo '<input type="radio" name="RadioGroup'.$i.'" value="'.$a.'">'.$a.'<br/>';
}
echo '<br/><br/>';
}
}
For use it
loopMe(3, 4);
As others have suggested, the FOR loop is your best bet here however, you can expand a little by adding simplicity and expanding the commands.
$rating = 6; //This sets the highest rating number
for($i=0 $i<$rating; $i++){
//This echos the input by rating, if rating is 6, it'll repeat 6 times.
echo "<input type=radio name=RadioGroup1 value='" . $i . "'>\r\n";
}
Now for the other half of the problem.
When posting information to a database, you need to send it to another page for processing and storage (or the same page if you handle it correctly).
This is a basic way to store THIS radio group (this is in procedural style):
$link = mysqli_connect('localhost', 'username', 'password', 'dbname');
$query = "INSERT INTO myTable (RadioGroup1) VALUES ($_POST['RadioGroup1'])";
mysqli_query($link, $query); //replace link with your database connections variable
Try a FOR loop function...as below:
<?php
for ($x=0; $x<=6; $x++)
{
echo '<input type="radio" name="RadioGroup1" value="'.$x.'">'.$x;
}
?>
To learn more about this function, visit:
http://php.net/manual/en/control-structures.for.php
see this example below... hope this helps you...
page1.php
<html>
<body>
<form id="frmQuestion" method="post" action="page2.php">
<p>Question 1</p>
<input type="radio" name="question1" value="1" checked="checked" />1
<input type="radio" name="question1" value="2" />2
<input type="radio" name="question1" value="3" />3
<input type="radio" name="question1" value="4" />4
<input type="radio" name="question1" value="5" />5
<input type="radio" name="question1" value="6" />6
<br/>
<p>Question 2</p>
<input type="radio" name="question2" value="1" checked="checked" />1
<input type="radio" name="question2" value="2" />2
<input type="radio" name="question2" value="3" />3
<input type="radio" name="question2" value="4" />4
<input type="radio" name="question2" value="5" />5
<input type="radio" name="question2" value="6" />6
<br/>
<?php
for($i=3; $i<=20; $i++)
{
echo "<p>Question " . $i . "</p>";
for ($j=1; $j<=6; $j++)
{
if($j == 1)
{
echo "<input type='radio' name='question". $i ."' value='". $j."' checked='checked' />" . $j;
}
else
{
echo "<input type='radio' name='question". $i ."' value='". $j."' />". $j;
}
}
echo "<br/>";
}
?>
<input type="submit" value="Send" />
</form>
</body>
</htm>
page2.php
<?php
echo "question 1:" . $_POST["question1"] . "<br/>";
echo "question 2:" . $_POST["question2"] . "<br/>";
echo "question 3:" . $_POST["question3"] . "<br/>";
echo "question 4:" . $_POST["question4"] . "<br/>";
// for more.
for ($n = 5; $n<=20; $n++)
{
$question = "question" . $n;
echo "question". $n . ":" . $_POST[$question] . "<br/>";
}
?>
Suppose that I have the code php as below:
<?php
include("connectdb.php");
$check1 = "";
$check2 = "";
$check3 = "";
$check4 = "";
$check5 = "";
$query = "SELECT * FROM tblworkfaire";
$res = mysql_query($query) or die(mysql_error());
if($res){
while($data = mysql_fetch_array($res)){
if($data['1.DesTechnique'] == 1){
$check1 = "CHECKED";
echo $check1;
}
else if($data['1.DesTechnique'] == 2){
$check2 = " CHECKED";
}
else if($data['1.DesTechnique'] == 3){
$check3 = "CHECKED";
}
else if($data['1.DesTechnique'] == 4){
$check4 = "CHECKED";
}
else if($data['1.DesTechnique'] == 5){
$check5 = "CHECKED";
}
}
}
else{
echo "Fail";
}
?>
And Html code:
<form action="word2html.php" method="post">
<input type="radio" name="number1" value="1" checked="<? $check1; ?>">
</b><b>
<input name="number1" type="radio" value="2" checked="<? $check2; ?>">
</b><b>
<input name="number1" type="radio" value="3" checked="<? $check3; ?>">
</b><b>
<input name="number1" type="radio" value="4" checked="<? $check4; ?>">
</b><b>
<input name="number1" type="radio" value="5" checked="<? $check5; ?>">
</b>
What I need:
I have stored the radio box value in database,if I select value from database equal 1,it will check the radio box that have the value equal 1.
Problem:
I can only select value of radio box from database and it dose not work.How do I fix this? Anyone help me please, Thanks.
That's some rather horrible code.
First problem: You're doing a while() loop to fetch results from your query. If you're fetching multiple rows of data, the you'll be setting all or some of those $checkX variables, yet not actually doing anyhing with them during that particular loop iteration. Assuming you fetch enough rows, eventually ALL of those variables will be checked and you end up selecting all of your radio buttons.
If you're expecting only a single row of data, then the while() is simply cargo cult programming.
Second problem. You've repeated a lot of code in there, and define a lot of variables, for something that could be done far easier/cleaner with a loop. e.g.
for ($i = 1; $i <= 5; $i++) {
$checked = ($data['1.DesTechnique'] == $i) ? ' checked="checked"' : '';
echo <<<EOL
<input name="number1" type="radio" value="$i"$checked>$i<br />
EOL;
}
No $checkX variables, no repeated inputs. just a nice loop doing the output for you.
You need to output your variables.
<input type="radio" name="number1" value="1" checked="<?php echo $check1; ?>">
I am getting the value for the single tsid for each record, however the checked radio button value is not returned in the array, I just get 0? Any help is appreciated.
PHP:
// Set the timesheets to set status approved/rejected
// find out how many records there are to update
$size = count($_POST['tsid']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$tsid = intval($_POST['tsid'][$i]);
$personnelid = intval($_POST['personnel'][$i]);
print "TSID: " . $tsid . "<br>";
print "TSuser: " . $personnelid . "<br>";
if ($tsid > 0 && $personnelid > 0) {
// do the update and print out some info just to provide some visual feedback
$query = "Update timesheets set status='1' where id= '$tsid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
}
++$i;
}
mysql_close();
HTML:
<input type="hidden" name="tsid[]" value="<?PHP echo $row['id']; ?>">
<li data-role="fieldcontain">
<a href="tsapprove.php?id=<?PHP echo $row['id']; ?>"><p><?PHP echo $row['personnel']; ?></p>
<p><?PHP echo $row['name']; ?></p>
<p class="ui-li-aside"><strong><?PHP echo $row['totalhrs']; ?> H</strong></p>
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="radio" name="personnel[]" id="1" value="<?PHP echo $row['personnel']; ?>" />
<label for="1">Approve</label>
<input type="radio" name="personnel[]" id="2" value="<?PHP echo $row['personnel']; ?>" />
<label for="2">Reject</label>
</fieldset>
</a>
View Details
</li>
Hopefully you can piece this together:
<?
$size = count($_POST['tsid']);
echo "<pre>";print_r($_POST);echo "</pre>";
$i = 0;
while ($i < $size) {
$tsid = $_POST['tsid'][$i];
$pid = $_POST['personnel_'.$i];
print "TSID: " . $tsid . "<br />";
print "TSuser: " . $pid . "<br />";
$i++;
}
?>
<form method="post">
<!--tsid[0] - $i = 0-->
<input type="hidden" name="tsid[]" value="1" />
<input type="radio" name="personnel_0" value="111" />
<input type="radio" name="personnel_0" value="222" />
<!--tsid[1] - $i = 1-->
<input type="hidden" name="tsid[]" value="2" />
<input type="radio" name="personnel_1" value="333" />
<input type="radio" name="personnel_1" value="444" />
<input type="submit" />
</form>
Basically, the way you are returning personnel doesnt really work. So I simplified this a bit, and basically set the radios for personnel differently. Instead of an array, its setting the actual name with the TSID value.
I hard coded 1 and 2, but you would replace with your $row['id'], as well has the number after "personnel_" would be 0..1..2 etc
Hopefully this helps
edit: few issues with code
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 />';
}
}
}
?>