I have a dynamically formed table with radio buttons to choose pass or fail in each rows. Below are my codes:
<form name="form2" method="post" action="/admin/testresults/update/added">
<?php if(isset($rows)){ $i=1; $pass=$ fail='' ; foreach ($rows as $row) { (($row->result == 1) ? $pass='checked' : $fail='checked'); print "
<tr>
<td>".$i."</td>
<td>".$row->name." ".$row->last_name."</td>
<td>
<input type='radio' name='".$row->userId."' value='1' ".$pass.">Pass
<br/>
<input type='radio' name='".$row->userId."' value='0' ".$fail.">Fail
<input type='hidden' name='".$row->userId."' value='".$row->userId."' </td>
</tr>
"; $i++; } print "
<tr>
<td colspan='3'>
<input class='ember-view btn btn-danger' type='submit' />
</td>
</tr>"; } ?>
</form>
I'm using the below codes in my controller:
$inputs = Input::get();
foreach($inputs as $input){
TestResults::updateCandidate($input);
}
What I want to pass into updateCandidate() is the student id and the selected radio button value (1/0). How can I do this?
Thanks
Firstly, I agree with #Chelius comments, look at laravel's documentation around blade templating and #if / #foreach and the use of {{ Form::input }}.
However in answer to your question - you will need to name you inputs better to retrieve the user id and the verbose PHP version should end up looking like this:
<td>
<input type='radio' name='results[".$row->userId."]' value='1' ".$pass.">Pass
<br/>
<input type='radio' name='results[".$row->userId."]' value='0' ".$fail.">Fail
<input type='hidden' name='".$row->userId."' value='".$row->userId."'
</td>
When processing, your code should then be:
$inputs = Input::get();
foreach($inputs['results'] as $userId => $result){
TestResults::updateCandidate($userId, $result);
}
Note that you may need to update your "updateCandidate" function to accept the user id and the result.
Related
I have got a while loop that runs through all the records of the database printing them on a table. Now i also have some checkboxes within that very loop that I want to use to submit a form when clicked. Now when I click the checkbox it will indeed submit the form thanks to a Jquery script I found, BUT whenever i submit it it submits with the ID of the first record of the table.This Image
shows the table, as you see the first record has ID 34. Now every checkbox I click will send the $id 34.
This does not happen with normal submit buttons.
Is there a way I can submit with the individual userID's
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
I'm sorry if i'm not very clear with the explanation it is quite hard to explain this situation. Thank you guys!
Probably your problem is that you are submiting the same form always and its because you create a form for each row but it has the same id
For you the easy way is to put each form with the id cointaining the unique value of the row and doing submit with that.
Something like this
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete_<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete_<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
It looks like the <form> your creating has a static id, so ALL forms will have id='resComplete'. The jQuery submit function will grab the first element with id='resComplete' and submit it. You need to make it unique for every form and make the onchange='$("#resComplete").submit();' code match it.
Eg.
<?php
while ($openResInfo = mysql_fetch_array($openResQuery))
{
$id = $openResInfo[0];
$complete = $openResInfo[7];
?>
<form id='resComplete-<?php echo $id; ?>' action='dashboard_openReserveringen_PHP.php' method='GET'>
<?php
echo "<input type='hidden' name='userID' value='$id'>";
?>
<input type="hidden" name="complete" value="0" >
<input id='complete' type='checkbox' name='complete' value='1' onchange='$("#resComplete-<?php echo $id; ?>").submit();' <?php if($complete == 1){echo "checked";}?>>
</form>
Better yet, use jQuery to find out what form it's in by chaning the onchange to something like:
<input id='complete' type='checkbox' name='complete' value='1' onchange='$(this).closest('form').submit();' <?php if($complete == 1){echo "checked";}?>>
I'm having a trouble finding a solution to this. Here is the case, assume that I have 5 books in the database, i will display them by doing a while then mysql_fetch_array assume that I create a table. This is the plot
echo"
<td>
<form action='bookext.php' method='post'>
<input type='submit' value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
the $row['book_title'] works fine because it must display what is the title of the book in the database. But, how can I get it's unique value which is book_id and send it to bookext.php?
PS: Sorry for my title, I can't pull the right english
First of all your current input needs a name attribute:
"<input type='submit' name="title" value='".$row['book_title']."' style='border: 0; background: transparent';>WHAT TO PUT HERE</form></td>";
Since you're using the POST method this will insure the value is available in the $_POST array, like $_POST['title'].
For the id you can add a hidden input to your form:
"<input type='hidden' name='book_id' value='".$row['book_id']."'>"
This will be available to you in $_POST['book_id'] when you submit.
Just insert hidden field to your form
<input type="hidden" name="POST_NAME" value="YOUR_VALUE_TO_IDENTIFY">
Don't use forms in tables.
<form action='bookext.php' method='post'>
<table>
<tr>
<?php foreach($books AS $book){ ?>
<td>
<button type="submit" name="bookId" value="<?php echo $book['id']; ?>">
Title of the book
</button>
</td>
<?php } ?>
</tr>
</table>
</form>
bookext.php
<?php
$id = $_POST['bookId'];
I want to create an attendance record form where teacher can record attendance of the students and send it to database to store. Here I am using radio buttons and I want these buttons to only select either present or absent or authorise however right now it is not implementing what I want to do. Right now the selection of the radio buttons is not working properly so can you please me to put the radio buttons in specific way so that it selects only present or absent or authorise. I think we have to use CSS or javaScript for this HTML form syntax but the question is how. Thank you in advance.
echo '<table action="process.php" method="POST" class="tableEchoPupilAttendance" border="1">
<tr>
<th>Image</th>
<th>Name</th>
<th>Present</th>
<th>Absent</th>
<th>Authorise</th>
</tr>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>
<td><img width='70' height='60' src='data:image/jpeg;base64,".base64_encode($row['image'])."'/></td>
<td>" .$row['name']. $row['surname']."</td>
<td> <input type='radio' name='present' value=''/> </td>
<td> <input type='radio' name='absent' value=''/> </td>
<td> <input type='radio' name='late' value=''/> </td>
</tr>";
}
echo "</table>";
Radio buttons are grouped by the name attribute. To have just one of the three radio buttons on each row selectable, their names must be the same, and their desired values stored in the value attribute.
I think it's fair to assume you have an id of some sort on your result rows. Therefore the following code might work:
while ($row = mysqli_fetch_array($result)) {
?>
<td><input type="radio" name="att[<?=$row['id'];?>]" value="present" /><td>
<td><input type="radio" name="att[<?=$row['id'];?>]" value="absent" /><td>
<td><input type="radio" name="att[<?=$row['id'];?>]" value="late" /><td>
<?
}
Submitting this will give you an array with content such as this:
[1] => 'present',
[3] => 'absent',
[4] => 'present',
[6] => 'late'
Using numeric values is usually a good idea, however the code won't be as readable.
Radio buttons are grouped only if they have the same name. So what you want to do is:
<td> <input type='radio' name='attendance[<?php print $row['id']; ?>]' value='present'/> </td>
<td> <input type='radio' name='attendance[<?php print $row['id']; ?>]' value='absent'/> </td>
<td> <input type='radio' name='attendance[<?php print $row['id']; ?>]' value='late'/> </td>
I am trying to create a table showing multiple users data. The data in the table will then be able to be edited and updated. Below is an example of the way the form is laid out:
echo "<form action=AdminUpdateLecInfo.php method=post>";
while ($return = mysql_fetch_assoc($result)) {
$phonenumber = "$return[PhoneNumber]";
$number = str_pad($phonenumber, 11, "0", STR_PAD_LEFT);
echo " <tr class='data'>
<input type='hidden' name='id'".$return['ID']."'' value= '".$return['ID']."' />
<td class = 'title'><input class = 'title' type='text' name='title'".$return['ID']."'' value= '".$return['Title']."' /></td>
}
echo "</table>
<input class='submit' type='submit' value='Update Info' />
</form>
Once the table is created the information is passed to the 'update.php' script.
$sql="UPDATE completeinfo SET Title='".$_POST['title'][$return['ID']]."'
WHERE ID = '".$_POST['id'][$return['ID']]."'";
header("Location:Home.html");
The problem I'm having is that I need to add the '".$return['ID']."' to the name of each input field so that not all users details are updated with the same values. I am unsure if I need to apply a foreach loop around this query so that it applies to each user and updates their details. Currently however the update query is not working presumably because the post method is not fetching the values from the form correctly.
Your problem is the name of your fields, use that :
$return_id = $return['ID'];
echo "
<tr class='data'>
<td class = 'title'>
<input type='hidden' name='id$return_id' value='$return_id' />
<input class='title' type='text' name='title$return_id' value='$return_id' />
</td>
</tr>";
Before doing your mysql update, do var_dump($_POST);, you'll be shown the content of the HTTP POST parameters so you can see what they are and how to use them in the query.
You have to use the names of your text fields as array so you can iterate your array
name='title'".$return['ID']."'
name='id'".$return['ID']."'
I am trying to submit multiple arrays with a checkbox form but I am only able to submit one array at the moment, here is what I have so far
In this example I am submitting an array of numbers with the delete[] array, this array gets processed properly, I also want to submit the array condition[] this does not get processed properly, what is the best way to solve this issue?
php code
$catalog = $database->getInventory();
if($catalog){
$numRows = sizeof($catalog);//count
echo "<b>Book Count:</b> ".$numRows."<br>";
echo "<form method='post' action='inventory.php'>";
echo "<table id='example' class='tablesorter' border='0' cellpadding='0' cellspacing='1'>";
echo "
<thead>
<tr>
<th>ISBN</th>
<th>Title </th>
<th>Rank </th>
<th>Condition </th>
<th><input type='checkbox' name='delete' value='all' /></th>
</tr>
</thead>\n";
foreach($catalog as $elem){
echo "
<tr>
<td>".$elem["isbn"]."</td>
<td>".$elem["title"]."</td>
<td>".$elem["rank"]."</td>
<td>".$elem["condition"]."</td>
<td>
<input type='checkbox' name='add[]'
value='".$elem['isbn']."_".$elem['condition']."_"."' />
</td>
</tr>";
}
echo "</table>";
echo "</form>";
}
example html markup
<form method='post' action='inventory.php'>
<table>
<tr>
<td>
<input type='hidden' name='addInventoryBook' value='1'>
<input type='submit' value='Add' />
</td>
</tr>
<tr>
<td>
<input type='checkbox' name='add[]' value='100001_used' />
</td>
</tr>
<tr>
<td>
<input type='checkbox' name='add[]' value='100001_new' />
</td>
</tr>
<tr>
<td>
<input type='checkbox' name='add[]' value='100003_new' />
</td>
</tr>
</table>
</form>
php function
function Inventory(){
if(isset($_POST['addInventoryBook'])){
if(isset($_POST['add']) && is_array($_POST['add'])){
$arr = array();
foreach($_POST['add'] as $checkbox){
$temp = explode("_", $checkbox);
$arr[] = array(
"isbn" => $temp[0],
"condition" => $temp[1],
"sub_condition" => $temp[2]
);
}
$this->addInventoryBook($arr);
}
else{
echo "No values have been set";
}
}
function addInventoryBook($arr){
foreach($arr as $elem){
//if used get sub-category
if($elem['condition']=='used'){
echo $elem['isbn']."-".ucfirst($elem['condition'])
.ucfirst($elem['sub_condition'])."<br>";
}
else if($elem['condition']=='new'){
echo $elem['isbn']."-".ucfirst($elem['condition'])."<br>";
}
}
}
All I want is to basically be able to pass two arrays to my php script
current output
100001
100002
100003
desired output
100001 good
100002 new
100003 new
The problem that you are having, I suspect, is that only the checkboxes that are checked will be passed back to the server, whereas all the hidden fields will always be passed so the lengths of the arrays will differ and the keys wont correspond.
The solution to this is actually relatively simple - you just need to specify the keys for the condition array so you can match the values up again. Something like this:
HTML:
<tr>
<td>
<input type='hidden' name='condition[100001]' value='good' />
<input type='checkbox' name='delete[]' value='100001' />
</td>
</tr>
<tr>
<td>
<input type='hidden' name='condition[100002]' value='new' />
<input type='checkbox' name='delete[]' value='100002' />
</td>
</tr>
PHP:
foreach ($_POST['delete'] as $delete) {
$condition = $_POST['condition'][$delete];
// Do stuff
}
This ties the values in the $_POST['condition'] array back up with the $_POST['delete'] array so everything will match up again.
EDIT
The way the keys are being created above is not great and in retrospect it is the wrong way to do it.
To demonstrate the right way to do it, let's imagine we have the following array, nice and simple:
$books = array(
10001 => 'good',
10002 => 'new',
10003 => 'new',
10004 => 'good'
);
What we need to do is tie up the two inputs that are associated with each book, which means we need a set of key/value pairs that can be matched up. That sounds like an array to me. But unlike the example above, the keys should be irrelevant to the data - they don't need to mean anything, because all we want is the data.
What we need to do is specify every single key explicitly (no stack-style array pushes) and make the keys agnostic of the data they relate to.
We can do this:
$i = 0;
foreach ($books as $isbn => $condition) {
echo "
<tr>
<td>
<input type='hidden' name='condition[$i]' value='$condition' />
<input type='checkbox' name='delete[$i]' value='$isbn' />
</td>
</tr>
";
$i++;
}
...and then, when the form is submitted, we can do this:
// We still base our code on $_POST['delete'] - because this is the array that
// depends on the user input. This time, though, we'll look at the keys as well
foreach ($_POST['delete'] as $key => $isbn) {
$condition = $_POST['condition'][$key];
// Do stuff
}
I'm a little confused about what you are asking, but I think I can simplify this for you. I don't know how you are generating the values for the hidden fields, are they hard coded? Regardless, this system would work much better if it were simplified.
Try this out....
This will combine the info for the numbers and condition, then it will split them on the backend for handling. This way the information is passed at the same time.
<tr>
<td>
<input type='checkbox' name='delete[]' value='100001-good' />
</td>
</tr>
<tr>
<td>
<input type='checkbox' name='delete[]' value='100002-new' />
</td>
</tr>
<tr>
<td>
<input type='checkbox' name='delete[]' value='100003-new' />
</td>
</tr>
<?php
if(isset($_POST['deleteInventoryBook'])){
if(isset($_POST['delete']) && is_array($_POST['delete'])){
foreach($_POST['delete'] as $checkbox){
$checkbox = explode('-', $checkbox);
echo $checkbox[1];
echo '<br />';
echo $checkbox[0];
echo '<br />';
}
}else{
echo "No values have been set";
}
}
?>
Again, I don't know if this is helpful or not because I was a little misunderstood about what exactly you were trying to achieve, but I hope it was helpful.
You're going to have to find a creative way to pass multiple hidden fields as an array to the PHP handler, or change how that data is collected. A "serialized" array seems to be the best bet.
This StackOverflow answer really outlines what you can do, and should still match your script's behavior. Good luck!