Submitting one value of the Hidden input in the current row clicked - php

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>";
}

Related

Handling Nested Forms in HTML/php

I try to figure out how to handle two nested forms. The first one sends data from select-option dropdown to another_file.php. The nested one should send data from check boxes to current file and should be handled by isset($_POST['...']). Here is my simplified code:
if (isset($_POST['a']))
{
// do something
}
if (isset($_POST['b']))
{
// do something else
}
echo "<form action='another_file.php' method='post'>"; // begin of first form
echo "<table class ='table table-hover table-condensed table-striped table-bordered'>";
echo "<thead>";
echo "<th>ID</th><th>Jídlo</th><th>Množství</th><th>por_cislo</th><th>Odebrat</th>";
echo "</thead>";
while($rows = $stmt->fetch()){
echo "<tr><td>" . $rows['id'] . "</td><td>".$rows['jidlo'];
echo "<select name =".$rows['id']."_".$rows['por_cislo']."> ";
$stmt2 = $db->query($q2);
echo "<option value ='nic'> (vyberte potravinu) </option>";
$max_por_cislo = $rows['por_cislo'];
while($rows2 = $stmt2->fetch())
{
echo '<option value="'.$rows2['id'].'">'.$rows2['jidlo'].'</option>';
};
echo "</select>";
echo "</td> <td>" . $rows['mnozstvi'] . "g <input name = '".$rows['por_cislo']."' type='text' value = '-'></td><td>".$rows['por_cislo']."</td>";
echo "<td><form action ='this_file.php' method = 'post'>"; // begin of nested form
echo "<input type='checkbox' id='atur_peg' name='idecko[]' value=".$rows['id']."*".$rows['por_cislo']." /></td></tr> ";
};
echo "<input type='hidden' name='pc' value=".$plan_cislo.">";
echo "<tr><td colspan='2'><input name = 'go' type='submit' value='OK'/></td><td colspan='2'><input type = 'submit' name ='a' value='ADD'/></td>";
echo "<td><input type = 'submit' name ='b' value='DELETE checked'/></form></td></tr>"; // end of nested form
echo "</table>";
echo "</form>"; // end of first form
Is
there any way to do this correctly?
Instead of nested forms, use jquery to detect checkbox being checked and then change the form action url. Something on this line.. You will have to modify the same to suit your needs.
$(document).ready(function(){
$("#formname").on("change", "input:checkbox", function(){
if( $(this).is(":checked") ) {
$('#formName').attr('action', 'this_file.php');
$("#formname").submit();
}
});
});

Get values of radio selected values of entire row to next file

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."' >";

how to store answers from an online examination system to database

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'].....

Delete MySQL entry from HTML page

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

Loop through $_POST

I have a for loop which actually displays a product name and several buttons like: Edit, Update , Cancel
For each product i am displaying , it will have its own set of Edir, Update, and Cancel button as below.
Paint Edit Update Cancel
I want to loop through the buttons so that for each category, I can perform a different action. I was thinking about using something like btn_edit1, btn_edit2 for the name of the button and use a for loop. 1, 2 are the category ids.
Maybe Im not clear enough. Sorry for that. Can anyone give me some suggestions?
for($i = 0; $i<count($obj_categories_admin->categories);$i++)
{
echo "<tr>";
echo "<td width='1500'>";
echo "<input type='text' name='name' size = '30' value='" . $obj_categories_admin->categories[$i]['name'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Update details' name='submit_update_category_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Edit Sub Categories' name='submit_edit_sub_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Delete' name='submit_delete_category_" .
$obj_categories_admin->categories[$i]['category_id'] . "'/>";
echo "</td>";
echo "<td width='500'>";
echo "<input type='submit' value = 'Cancel' name='cancel'" . "'/>" ;
echo "</td>";
echo "</tr>";
}
I want to do something like
foreach($_POST as $key => $value)
{
}
so that when i click on a button it performs an action depending on the category_id.
I have tried this as suggested:
echo "<input type='submit' name='submit[add_category]'" .
"[" . $obj_categories_admin->categories[$i]['category_id'] . "]". " value='Add' />";
Now in my class, i have:
$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
But, i am getting the error : undefined index submit
I would give the name attributes of my submit buttons using following pattern:
name="submit[which_action][which_category]"
For example for your 'Update' button for category 123:
name="submit[update][123]"
When the user clicks any of the submit buttons, to determine which specific button the user has clicked you just need check for $_POST['submit'] in your PHP code:
$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));
Well i would use something like this:
<fieldset>
<!-- product info -->
<input name="productName[paint]" />
<input name="productName[edit]" />
<input name="productName[delete]" />
<input name="productName[cancel]" />
</fieldset>
that way when you get it to the serverside everything will be nice and tidy in nested arrays.

Categories