I have displayed the values from mysql table along with radio buttons with different values. On submit button it should redirect to next page and display the selected values. My code on first file is as follows :
$query1 = "select * from booking";
$result1 = $connection->query($query1);
echo "<form method='post' action='edit.php'>";
echo "<center>";
echo "<table border='1'>";
$count = 0;
while($row = $result1->fetch_row())
{
echo "<tr>";
echo "<td width=".'10'.">";
$count = $count + 1;
#$sr=$sr+1;
echo "<input type='radio' name='select' value='".$count."' />";
#$row = mysqli_fetch_row($result1);
#echo "<tr>";
echo "<td width=".'10'.">";
$rooms = $row[0];
$srrooms = $rooms;
echo "<input type='text' name='srrooms' value='".$srrooms."' hidden='hidden'>";
#echo $srrooms;
echo $rooms;
echo "</td>";
echo "<td width=".'10'.">";
$no_roomss = $row[1];
$nosrrooms = $no_roomss;
echo "<input type='text' name='nosrrooms' value='".$nosrrooms."' hidden='hidden'>";
echo $no_roomss;
echo "</td>";
echo "<td width=".'10'.">";
$no_dayss = $row[2];
$nodays = $no_dayss;
echo "<input type='text' name='nodays' value='".$nodays."' hidden='hidden'>";
echo $no_dayss;
echo "</td>";
echo "<td width=".'10'.">";
$name = $row[3];
echo $name;
echo "</td>";
echo "</tr>";
#$count++;
}
echo "<input name='count' type='hidden' id='count'>"; // hidden input where counter value will be stored
echo "</table>";
echo "<input type='submit' name='submitRooms' />";
echo "</center>";
echo "</form>";
The javascript code is :
$(function(){
$('input[name="select"]').change(function(){
$('#count').val($("input[name='select']:checked").val());
});
});
edit.php code is :
<?php
if(isset($_POST['submitRooms'])){
#$count = $_POST['count'];
#$srrooms=$_POST['srrooms'.$count];
#$nosrrooms=$_POST['nosrrooms'.$count];
#$nodays=$_POST['nodays'.$count];
echo 'Selected values are: '. $srrooms . $nosrrooms . $nodays;
}
?>
You can use counter for submitting the values of the selected radiobutton's row as:
<?php
$query1 = "select * from booking";
$result1 = $connection->query($query1);
echo "<form method='post' action='edit.php'>";
echo "<center>";
echo "<table border='1'>";
$count = 0; // counter variable
while($row = $result1->fetch_row())
{
echo "<tr>";
echo "<td width=".'10'.">";
echo "<input type='radio' name='select' value='".$count."' />";
echo "<td width=".'10'.">";
$rooms = $row[0];
$srrooms = $rooms;
echo "<input type='hidden' name='srrooms".$count."' value='".$srrooms."' >"; // add counter value to every name of the input fields
echo $rooms;
echo "</td>";
echo "<td width=".'10'.">";
$no_roomss = $row[1];
$nosrrooms = $no_roomss;
echo "<input type='hidden' name='nosrrooms".$count."' value='".$nosrrooms."'>";
echo $no_roomss;
echo "</td>";
echo "<td width=".'10'.">";
$no_dayss = $row[2];
$nodays = $no_dayss;
echo "<input type='hidden' name='nodays".$count."' value='".$nodays."' >";
echo $no_dayss;
echo "</td>";
echo "<td width=".'10'.">";
$name = $row[3];
echo $name;
echo "</td>";
echo "</tr>";
$count++;
}
echo "<input name='count' type='hidden' id='count'>"; // hidden input where counter value will be stored
echo "</table>";
echo "<input type='submit' name='submitRooms' />";
echo "</center>";
echo "</form>";
?>
and then use jquery for saving the active radiobuttons count in the hidden count input field [after the above php code]
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type='text/javascript'>
$(function(){
$('input[name="select"]').change(function(){
$('#count').val($("input[name='select']:checked").val());
});
});
</script>
then on your edit.php fetch the posted data as:
<?php
if(isset($_POST['submitRooms'])){
#$count = $_POST['count'];
#$srrooms = $_POST['srrooms'.$count];
#$nosrrooms = $_POST['nosrrooms'.$count];
#$nodays = $_POST['nodays'.$count];
echo 'Selected values are: '. $srrooms . $nosrrooms . $nodays;
}
?>
You didn't have added the whole form code so please add the above mentioned form code and the reason why your code is not working is that you haven't added the count with the input field names. And their is no use of fetching the posted data with count in the edit.php till you don't have the input fields named with the $count variable
echo "<input type='hidden' name='srrooms".$count."' value='".$srrooms."' >";
Related
I have this edit form:
<?php
while($rows=mysql_fetch_array($query)){
$area=$rows['area'];
$dates=$rows['dates'];
$number_of_local_hires=$rows['number_of_local_hires'];
$salary_per_local_hire_per_day=$rows['salary_per_local_hire_per_day'];
$amount=$rows['amount'];
echo "<tr id='addr0'>";
echo "<td>";
echo "<input type='text' name='ca_salary_area[]' placeholder='Area' class='form-control' value='$area' required/>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ca_salary_date[]' placeholder='Date/s' class='form-control' value='$dates' required/>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ca_salary_localhires[]' placeholder='Number of Local Hires' class='form-control' value='$number_of_local_hires' required/>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ca_salary_perday[]' placeholder='Salary Per Local Hire Per Day' class='form-control' value='$salary_per_local_hire_per_day'required/>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='ca_salary_amount[]' placeholder='Amount' class='form-control' value='$amount'/>";
echo "</td>";
echo "</tr>";
}
?>
But I can't update it using this query in my edit_salary.php:
$sql_rows = mysql_query("SELECT *FROM `tblcasalaryformdetails` WHERE casalaryform_id =$id");
$num_rows = mysql_num_rows($sql_rows);
if ($num_rows == count($_POST['ca_salary_area'])){
for($i=0;$i<$num_rows;$i++)
{
$nos_hire = $_POST['ca_salary_localhires'][$i];
$nos_salaray = $_POST['ca_salary_perday'][$i];
$subtotal_salary_amount = $nos_hire * $nos_salaray;
$sql = "UPDATE `tblcasalaryformdetails`
SET area = '". $_POST['ca_salary_area'][$i]."',
dates = ". $_POST['ca_salary_date'][$i].",
number_of_local_hires = ". $_POST['ca_salary_localhires'][$i].",
salary_per_local_hire_per_day = ". $_POST['ca_salary_perday'][$i].",
amount = ". $subtotal_salary_amount."
WHERE casalaryform_id = $id";
mysql_query($sql);
}
}
The result is all the values will be updated same as the last row value. What did I missed? I tried show its index, it looks fine.
I am creating an online examination system where am fetching questions and answers from database and displays options in radio button. But, the problem is if i click save it just saves one answer not answer from all questions
php codes for fetching questions
$quer=" SELECT * FROM questionset ORDER BY RAND() ";
$query=mysqli_query($sql,$quer);
while($row = mysqli_fetch_array($query))
{
$na= $row['qus'];
$opa= $row['opa'];
$opb= $row['opb'];
$opc= $row['opc'];
$opd= $row['opd'];
echo "<div class='pr3'>";
echo "<div id='question'>";
echo $na;
echo "</div>";
echo "<br/>";
echo "<form name='cart' method='post'>";
echo "<input type='radio' name='ans' value='" . $opa. "' onclick='answ(this.value)' >";
echo $opa;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opb. "' onclick='answ(this.value)'>";
echo $opb;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opc. "' onclick='answ(this.value)'>";
echo $opc;
echo "<br/>";
echo "<input type='radio' name='ans' value='" . $opd. "' onclick='answ(this.value)'>";
echo $opd;
echo "<br/>";
echo "<input type='text' id='ansval' name='ansval' >";
echo "</form>";
echo " </div>";
}
Javascript that updates just one text box
<script>
function answ(answer) {
document.getElementById("ansval").value = answer;
}
</script>
The radio buttons name are all same for all questions. You have to make the radio buttons names dynamically for each answer set. You can use the following code :
$quer=" SELECT * FROM questionset ORDER BY RAND() ";
$query=mysqli_query($sql,$quer);
while($row = mysqli_fetch_array($query))
{
$questionId = $row['questionId']; //primary key of question table
$na= $row['qus'];
$opa= $row['opa'];
$opb= $row['opb'];
$opc= $row['opc'];
$opd= $row['opd'];
echo "<div class='pr3'>";
echo "<div id='question'>";
echo $na;
echo "</div>";
echo "<br/>";
echo "<form name='cart' method='post'>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opa. "' onclick='answ(this.value,'<?php echo $questionId; ?>')' >";
echo $opa;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opb. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opb;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opc. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opc;
echo "<br/>";
echo "<input type='radio' name='ans<?php echo $questionId; ?>' value='" . $opd. "' onclick='answ(this.value,'<?php echo $questionId; ?>')'>";
echo $opd;
echo "<br/>";
echo "<input type='text' id='ansval<?php echo $questionId; ?>' name='ansval<?php echo $questionId; ?>' >";
echo "</form>";
echo " </div>";
}
And You javascript function will be looks like :
<script>
function answ(answer,qId) {
document.getElementById("ansval"+qId).value = answer;
}
</script>
After the answer submit you can get the submitted values in same way. i.e $_POST['ansval'.$questionId]; Before get the value you have to create another loop to get all question Ids.
ID field must be unique
++$counter;
echo "";
$_POST['ansval1'],$_POST['ansval2'],$_POST['ansval3'].....
I'm trying to design a control page for my website that allows admins to delete requests to use a 3D printer at my school. The page displays a query of the schedule from the MySQL database and puts a delete button next to every entry. However, I would like to link the information in each entry to its respective delete button and have no idea how to do that. You can see the page itself here: http://kepler.covenant.edu/~techclub/PHP/manage%20printer%20schedule.php and this is my code for displaying the information
if ($result = $mysqli->query($query)) {
// see if any rows were returned
if ($result->num_rows > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
echo "<tr>";
echo "<th>Control</th>";
echo "<th>Student ID</th>";
echo "<th>Last Name</th>";
echo "<th>First Name</th>";
echo "<th>Date and Time</th>";
echo "<th>Description</th>";
echo "</tr>";
$key = 1;
while($row = $result->fetch_array()) {
echo '<form method="POST" name="deleterequest" action = "delete request.php">';
echo "<tr>";
echo "<td><input name='".$row[5]."'type='submit' value='Delete' ></td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
}
else {
// no
// print status message
echo "No upcoming prints found!";
}
// free result set memory
$result->close();
}
I can't figure out how to link the information in a row with the delete button that I put in the row to send to the delete request.php program (which I have no code for yet)
you should put hidden field that hold each record id.
As below
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>";
echo '<form method="POST" name="deleterequest" action = "delete request.php">';
echo "<input name='recored_id' type='hidden' value='".$row['id']."' >";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
echo "</form>";
}
Now from your request.php page you should do something like this
$recored_id = (int) $_POST['recored_id'];
$sql = "DELETE FROM table_name WHERE recored_id='$recored_id'";
Don't forget to give this code some security validation
all the best,
You're going to need some hidden inputs, like so:
while($row = $result->fetch_array()) {
echo "<tr>";
echo '<td><form method="POST" name="deleterequest" action = "deleterequest.php">';
echo "<input type='hidden' name='val0' value='" . $row[0] . "'>";
echo "<input type='hidden' name='val1' value='" . $row[1] . "'>";
echo "<input type='hidden' name='val2' value='" . $row[2] . "'>";
echo "<input type='hidden' name='val3' value='" . $row[3] . "'>";
echo "<input type='hidden' name='val4' value='" . $row[4] . "'>";
echo "<input name='".$row[5]."'type='submit' value='Delete' >";
echo "</form></td>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
You should change the name of these inputs though
How to pass one value of the current row of multiple hidden input in PHP. I have the following code:
foreach($portfolio as $portfolio){
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</td>";
echo "</tr>";
}
I also have submit button each row that triggers the form. When I click the submit button, it submits all the value of the row of the hidden input. I only want the clicked button row value.
URL is like this:
/portfolio?portfolio_id=1&portfolio_id=2&portfolio_id=3&portfolio_id=4 and so on
I only want
/portfolio?portfolio_id=3
Have a new form for each row...
<form method="get">
</form>
Like this:
foreach($portfolio as $portfolio){
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo '<form method="get">';
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}
If all you're submitting is that ID, and you're using GET instead of POST you might as well not even use forms and use links instead. You can still style a link to look like a button if you want, but using a link would make a lot more sense semantically. Remove the hidden input, and have each link have the URL you want.
The form tag should be included on the looping.
change this
foreach ($portfolio as $portfolio) {
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</td>";
echo "</tr>";
}
to:
foreach ($portfolio as $portfolio) {
echo "<tr class ='table-comments'>";
echo "<td>".$portfolio['portfolio_title']."</td>";
echo "<td class = 'comment-content'>".$portfolio['portfolio_client']."</td>";
echo "<td><a target = '_blank' href = ".$portfolio['portfolio_link'].">".$portfolio['portfolio_link']."</a></td>";
echo "<td>";
echo "<form action='process.php' method='get'>";
echo "<input type='hidden' name='portfolio_id' value='" . $portfolio['portfolio_id'] . "' />";
echo "<input type = 'submit' value = 'Edit'>";
echo "<input type = 'submit' value = 'Move to Trash' class = 'action-button'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}
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\"]'>";