I've 10 question and each have 4 options, i want to check radio values, whether it is correct or not with the result stored in database when a user submit the form.
thanks in advance...
while($row = mysqli_fetch_array($result)){
foreach( $_POST as $key=>$val){
if($row['answer'] == $val){ echo "Correct";}
else{ echo "Incorrect";
}
#broken_heart is right. you should provide your code. then some body will guide you.... anyway, this is the one way.. try once...
Steps:
assign different names with auto increment for each radio button group. name should be like this radio_name[$i] .... here $i is auto increment...
submit the form
Get value like this..
$radio_values=$_POST['radion_name'];
for($i=0;$i<count($radio_values);$i++)
{
$radio_value= $radio_values[$i];
}
Related
My webpage is pulling data from two tables - applications and archiveapps - and displays them using the following:
$sql = "SELECT * FROM applications";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Lots of info, including some html thrown in for style."
I want users to be able to click a checkbox listed next to each row and then hit an "Archive Selections" button that then moves all the selected entries from the applications table to the archiveapps one.
So far I've tried a form with it's code half in the echo above (so that a checkbox would go by each listed row from the $sql query) with the submit button outside (so that there would only be one "Archive Selections" button) but I'm sure this isn't proper syntax.
To actually move the data, I had this for the checkbox:
<form method='post' action=''><input type='checkbox' name='archname' value=".$row["charname"].">
(The above was inside an echo statement, so I assume it was able to pull the $row["charname"] no problem, but am unsure how to verify that.)
A little further down the page is the submit button and </form>.
And then I've got the function I want it to run when the submit button is clicked, to check if boxes have been selected and copy them into the archiveapps table. I'm sure there's something I'm missing here, probably in referencing what exactly is selected, and then copying that row's data to the other table... but honestly I just don't know enough about php to know what I'm missing.
if(!empty($_POST['archname'])) {
foreach($_POST['archname'] as $check) {
function archiveapp() {
$sqli="INSERT INTO archiveapps SELECT * FROM applications";
if ($conn->query($sqli) === TRUE) {
echo "<i>Archived</i>";
} else {
echo "Error: " . $sqli . "<br>" . $conn->error;
}}}}
Most of this has just been gathered from google searches and kind of mushed together, so I'm sure there are a lot of things done wrong. Any pointers or advice would be greatly appreciated!
Oh and my end goal is to copy the data to the archiveapps table and then delete it from the applications table, but for now I've just been focusing on the copying part, since I assume deleting a row will be fairly simple? Either way it's not the priority for this question.
Thanks in advance for any help!
Use a tool like FireBug to see what you are actually posting to server. You have multiple checkboxes with the same name, so you are sending only 1 value (the last checked, I think).
You need to send all of them, so use brackets after the name:
<!-- this is just an example -->
<input type="checkbox" name="archname[]" value="1">
<input type="checkbox" name="archname[]" value="2" checked>
<input type="checkbox" name="archname[]" value="3" checked>
Then on PHP, inside $_POST['archname'] you will get this:
Array(2, 3)
After this, you can do a foreach(...) loop like you are already doing, inserting only the ID you get from the $_POST variable:
foreach($_POST['archname'] as $id){
$sqli = "INSERT INTO archiveapps (id) VALUES (".(int)$id.")";
}
Hi I have a check box dynamically created as follows;(not the complete code here) there are a number of check boxes created, hence name is an array. (Is it right the way I have started the array inside <td></td>?)
while ($rec = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='tbldecor1'><input type='checkbox' name='delete[]' value='$Val1'></td>";
echo "</tr>";
}
I need to get the user selected check box values to a loop in order to delete the records selected. If I forget about the deleting part of it, to get the user selected values to a loop I used something like below;
$AA = $_POST['delete'];
while($AA == 'checked')
{
echo $AA; // trying to print the user checked options so that I can subsequently code to delete them.
}
but seems an erroneous approach! Can someone please give me an idea?
foreach($_POST['delete'] AS $key => $val) {
...
}
checkboxes which aren't checked in the form aren't submitted, so automatically you'll only get the checked checkboxes in the _POST data.
it's input field array which gives you all checked value into array in $_POST, so use following way
if(isset($_POST['delete'])) {
foreach($_POST['delete'] as $value){
echo $value;
//do stuff
}
}
I had generated few textboxes in a loop n named them differntly.. but the retrieving of data from those boxes is not working.. Please help me..
code for creating those textboxes
$i=0;
while($data=mysql_fetch_array( $sql ))
{
echo "<tr><td>".$data['idno']." </td><td>".$data['name'] . " </td><td>
<input type='text' name='obtmarks".$i."'></td></tr>"; $i++;
}
I have to retrieve that data n place it another table
code for retrieving the data
$i=0;
while($data=mysql_fetch_array( $sql1 ))
{
$as=mysql_query("INSERT INTO marks values('".$data['idno']."','".$data['name']."','".mysql_real_escape_string($_POST['obtmarks".$i."'])."')");
$i++; }
Please help me.. thank u in advance..
Assuming you print the textboxes in a form, and post the form back to the retrieval code:
You need to get the variable value from the post array:
$i=0;
while($data=mysql_fetch_array( $sql1 ))
{
var value = $_POST['obtmarks'.$i];
// insert into database
$i++;
}
(Be careful to use the exact same label, in your example you use obmarks and obtmarks.)
WARNING Don't store the value in your database without checking it!! Use prepared statements!!
What about this:
$as=mysql_query("INSERT INTO marks values('".$data['idno']."','".$data['name']."','".$_POST['obmarks'.$i]."')");
this is assuming you are passing your data through a form submit.
Need a little help...
I have a basic html table with text field form in last column and a hidden field on each row of the web table. The data in this table is extracted out of a table in the database.
I want my users to be able to update one field in the database (a score) using this web form (page).
I have a hidden web form component on each row that contains the unique id of the record in the database for each row in the web page table.
I was attempting to create code that would update the entire list of entries on the web form, even if the user is not updating that particular field. (The values of the scores field are populated into the web form at the creation of the table. So if you did not update any scores, but hit the submit button, it would update the database table with the same values.)
Here’s my code: (abbreviated to save bytes…)
<?php
//Do all the database connection stuff up front
if (isset($_POST[‘score’]))
{
$student_id = $_POST[‘student_id’];
$score = $_POST['score'];
$n = count($score);
$i = 0;
echo "You have updated these student scores on this assignment. \r\n" .
"<ol>";
while ($i < $n)
{
echo "<hr><P>{$score[$i]} \r\n";
echo "<hr><P>{$student_id[$i]} \r\n";
$qry = "UPDATE assignments SET score = ".$score[$i]." WHERE student_id = " .$student_id[$i]. '"';
$result=#mysql_query($qry);
$i++;
}
}
if($result) {
header("location: member-index.php");
exit();
}else {
die("Query failed");
}
?>
Am I on the right track? Is there a better way to do what I’m attempting? All suggestions and ideas welcome!
Thank you in advance!
i'm guessing you are using
<input name="scores[]"/>
why not echo the unique id into the input name?
<input name="score[<?php echo $unique_id ?>]" />
this means in the loop, the $i would be the unique id (one less variable and less HTML).
and please use mysql_real_escape_string() when working with DB transactions. I know it's an example code but please don't forget that
Besides that, yes, you are on the right track
I am a newbie in php. I have a form with multiple checkbox values. I wanna retrieve the checked values and diplay these values on other php form.
Below is the code which works perfectly fine if we add the checkboxes without while loop.
But when added through while loop I am not able to fetch the selected items.
xyzhtml.php(html form)
<?PHP
require ("DBConnect.php");
$selectQuery =mysql_query( "SELECT * FROM fruits where approved = 0");
while($row = mysql_fetch_array($selectQuery))
{
$fruit_name = $row['fruit_name'];
echo "<input type=\"checkbox\" name=\"things[]\" value=\"$fruit_name\">";
echo "<br>";
}
?>
On click of submit I call other php clled "xyz.php".
Below is the code used in it.
<?php
$checkBox = $_POST['things'];
echo $checkBox[0];
for($i=0; $i<sizeof($checkBox); $i++){
echo($checkBox[$i]);
}
?>
Please help.
Thanks in advance.
two things to check:
are you getting right values from the MySQL SELECT statement (check your HTML for empty checkboxes values and check your MySQL database for fruits that have approved field set to 0 to see if there are any)
when you don't tick a checkbox and submit the form, unticked checkboxes' values do not get submitted - have you thought about that?
Use foreach to record $_POST['things']
if (!empty($_POST['things'])) {
foreach ($_POST['things'] as $value) {
echo "the value are: ".$value;
}
}
Note: teh $_POST['things'] not $_POST['things[]']
I think your code should work, but it won't display anything if no checkboxes are selected.
I would however remove the explicit echo $checkbox[0] (unless this is for testing purposes).
Try a print_r($checkbox) to see what's actually in that array.