I have a code which retrieves data from db and show it to a table. It is working fine but I want to implement a checkbox as part of the header which would allow me to select/deselect all rows. The header is just being pulled from db comments so I have no idea on how to implement the checkbox-select/deselect all header.
Here is my code,
<div>
<form action="test4.php" method="post"><?php
if(is_array($result)){
echo '
<fieldset>
<legend>Assign</legend>
<div>Changes will affect selected rows only.</div>
<table width=auto cellpadding=1px cellspacing=0px border=1 align=center>
<thead>
<tr>';
// column comment from DB as column header
foreach($result[0] as $key => $val){
echo '<th>'.$colcomments[$key].'</th>';
}
echo '
</tr>
</thead>
<tbody>';
foreach($result as $row => $info){
echo '
<tr>';
foreach($info as $key => $val){
if($key=='id'){
echo '
<td title="'.$colcomments[$key].'">'.$val.'.
<input type="checkbox" name="'.$key.'['.$info['id'].']"
value="'.$val.'" id="rowid_'.$val.'" />
<label for="rowid_'.$val.'"></label></td>';
}
else {
echo '
<td title="'.$colcomments[$key].'">
<input type="text" name="'.$key.'['.$info['id'].']"
value="'.$val.'" />
</td>';
}
}
echo '
</tr>';
}
echo '
</tbody>
</table>
</fieldset>';
}
?>
<fieldset>
<legend>Select Date</legend>
<div>Select Date from and Date to</div>
<input type="date" name="from" id="from" value="<?=$date['from']; ?>" />
<input type="date" name="to" id="to" value="<?=$date['to']; ?>" />
<div><input type="submit" value="Submit" /></div>
</fieldset>
</form>
</div>
This is the my script for the checkbox, and I dont know where I can insert a code to create a checkbox header. This is different from the ones posted previously as my issue is how I can create a checkbox header, if i only used the commments section in my db to make it as the header.
<script>
jQuery(document).ready(function () {
jQuery("input[name=checkall]").click(function () {
jQuery('input[name=checkall]').prop('checked', this.checked);
jQuery('input[name=checkbox]').prop('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
jQuery("input[name=checkbox]").click(function(){
if(jQuery("input[name=checkbox]").length == jQuery("input[name=checkbox]:checked").length) {
jQuery("input[name=checkall]").prop("checked", true);
} else {
jQuery("input[name=checkall]").prop("checked", false);
}
});
});
</script>
I'm marking this as resolved. Easy fix is just to set the type to hidden.
Change from
<input type="checkbox" name="'.$key.'['.$info['id'].']"
To
<input type="hidden" name="'.$key.'['.$info['id'].']"
Checkbox is just a redundant since I need to update all rows anyways.
Related
I have two submits in the same form. One to filter the mySQL db and generate a list of checkboxes (works fine) and the other to display the values of the selected items using PHP (no go). Later on, I'll store these values to built a sql query. Please visit https://wintoweb.com/sandbox/question.php
I've tried everything you can imagine. Going nuts...
PHP function called from the [Show selected] button :
<?php
function getNames() {
//echo 'GOT IT !';
if(isset($_GET['choices[]'])){
if(!empty($_GET['choices'])){
foreach($_GET['choices'] as $selected){
echo $selected."</br>";
}
}
}
}
?>
Code for the [Show selected] button :
EXPECTED : the list of selected values below the
ACTUAL OUTPUT : no list of selected values. The PHP function getNames() is never traversed.
I simplified your form.
<form name="dig" action="#" method="get">
<input type="search" name="l_name" value="Author's name (whole or part)" style="width:200px;" onfocus="this.value=''" onblur="(this.value=='')? this.value='Author's name (whole or part)':this.value;" />
<input type="submit" name="search" value="Search">
<input type="submit" name="display_all" value="Show selected">
<br /><br />
<table style="width:400px">
<tr>
<td width="100%"><input type="checkbox" name="choices[]" value="ABÉLARD, Pierre et HÉLOÏSE" onclick="">ABÉLARD, Pierre et HÉLOÏSE</td>
</tr>
<tr>
<td width="100%"><input type="checkbox" name="choices[]" value="ABOUT, Edmond" onclick="">ABOUT, Edmond</td>
</tr>
<tr>
<td width="100%"><input type="checkbox" name="choices[]" value="ABRANTÈS, Laure Junot" onclick="">ABRANTÈS, Laure Junot</td>
</tr>
</table>
</form>
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
if(isset($_GET['search'])){
echo 'Search</br>';
} elseif(isset($_GET['display_all'])) {
echo getNames();
}
function getNames() {
$rets = '';
if(isset($_GET['choices']) and !empty($_GET['choices'])){
foreach($_GET['choices'] as $selected){
$rets .= $selected.'</br>';
}
}
return $rets;
}
Thanks to all who responded. I got it to update with a combination of ideas from you. I've amended the code to show what's working.
To implement LIKE functionality by passing the LIKE button value to php so as to update the DB.
This displays the table, complete with a LIKE button per row:
echo '<table>
<tr>
<th>Word ID</th>
<th>User ID</th>
<th>User Name</th>
<th>Word</th>
<th>Meaning</th>
<th>Example</th>
</tr>';
foreach ($data as $row)
{
echo '<tr>';
foreach ($row as $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
echo '<td>
<form method="POST" action="'.$_SERVER["PHP_SELF"].'">
<input type="hidden" name="LIKE" value="'.$row['wordID'].'">
<input type="submit" class="btn btn-success" value="Submit">
</form>
</td>';
echo '</tr>';
}
echo '</table>';
The code to process the form submit is:
if($_POST['submit'])
{
$sql = "UPDATE vocab SET likes = likes+1 where wordID = '{$row['wordID']}'";
$stmt = $db->prepare($sql);
/* Execute */
$stmt->execute();
}
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>
is a sytax you use when you are NOT already in a <?php tag. Here, you already are writing in PHP and in an echo and you just apen again a <?php, and another echo in it, this makes no sense.
echo '... <form method="post" action="'.$_SERVER["PHP_SELF"].'"> ...';
is the way you want to include your php_self value.
also
<input type="submit" value="LIKE" name="<?php echo $row["wordID"]; ?></form>
should be
<input type="submit" value="LIKE" name="'.$row['wordID'].'"></form>
but you cant the search for $_POST['wordID'], search for $_POST[$row['wordID']] in a for loop.
You're doing an echo inside an echo here :
echo '<td>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>
<input type="submit" value="LIKE" name="<?php echo $row["wordID"]; ?></form>
</td>';
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>
should be :
<form method="post" action="'.$_SERVER["PHP_SELF"].'">
i would do this:
<td>
<form method='POST'>
<input type='hidden' name='LIKE' value='".$row['wordId']."'>
<input type='submit' value='Submit'>
</form>
</td>
Action will always default to : PHP_SELF so no point adding that, additionally adding the hidden field instead of the submit button is preferable to me as it's easier to read, plus adds an easy option incase you actually want to add more to the form (which is entirely possible)
I need assistance in taking the next step in inserting data entered into a dynamically-generated form. I have looked for a number of days at other similar questions and am still missing a piece.
Here is my HTML form (minus error trapping/validations):
<form id="form" name="form" method="post" action="recipeaddsubmit.php">
<table id="myTable">
<tr>
<td width="10%">Amount</td>
<td width="10%">Measure</td>
<td width="35%">Ingredient</td>
</tr>
<tr>
<td valign="top">
<input type="text" name="Amt1" id="Amt1" size="1" />
<input type="text" name="Amt2" id="Amt2" size="1" />
</td>
<td valign="top">
<select name="Measure" id="Measure">
<option>Cup</option>
<option>Ounce</option>
</select>
</td>
<td valign="top">
<input type="text" name="Ing" id="Ing" size="40" />
</td>
</tr>
</table>
<button type="button" onclick="displayResult()">Insert new row</button>
<input type="submit" name="button" id="button" value="Submit" />
</form>
Here is the javascript in my header:
<script>
function displayResult()
{
var table=document.getElementById("myTable");
var row=table.insertRow(-1);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
var cell3=row.insertCell(2);
cell1.innerHTML="<input type='text' name='Amt1' id='Amt1' size='1' />
<input type='text' name='Amt2' id='Amt2' size='1' />";
cell2.innerHTML="<select name='Measure' id='Measure'> <option>Cup</option>
<option>Ounce</option></select>";
cell3.innerHTML="<input type='text' name='Ing' id='Ing' size='40' />";
}
</script>
And here is my attempted/partial mysqli insert statement (checkInput is a self-created validation function I have used before without issue):
$amt1 = checkInput($_POST['Amt1']);
$amt2 = checkInput($_POST['Amt2']);
$measure = checkInput($_POST['Measure']);
$ing = checkInput($_POST['Ing']);
$ingAddQuery ="INSERT INTO Ingredient (Amt1, Amt2, Measure, Ing)
VALUES ({$amt1}, {$amt2}, {$measure}, {$ing})";
mysqli_query($mysqli,$ingAddQuery);
if (!mysqli_query($mysqli,$ingAddQuery))
{
die('Error: ' . mysqli_error());
}
What I don't understand is how to incorporate a foreach loop in terms of how to increment for a certain number of rows; I understand the concept but not the application in this case.
Thank you for your assistance!
change the name from amt1 to amt1[] and etcetera...
so
<input type="text" name="Amt1[]" id="Amt1[]" size="1" />
When you submit the form,
$_POST['Amt1']; //is an array
will be an array, so you could do
$Amt1Array = $_POST['Amt1'];
foreach($Amt1Array => $Amt1){
//do stuff here
}
Better yet you can index the array using the js to create names like...
<input type="text" name="Amt1[0]" id="Amt1[0]" size="1" />
<input type="text" name="Amt2[0]" id="Amt2[0]" size="1" />
<input type="text" name="Amt1[1]" id="Amt1[1]" size="1" />
<input type="text" name="Amt2[1]" id="Amt2[1]" size="1" />
Then in the PHP you could
$Amt1Array = $_POST['Amt1'];
$Amt2Array = $_POST['Amt2'];
foreach($Amt1Array as $key => $Amt1Value){
$Amt2Value = $Amt2Array[$key];
//do stuff with rows
}
I have a suggestion.
Why don't u create a hidden input type.
<input type="hidden" name="cellCount" value="0" />
Each time u insert a new row, change value of this hidden field. This will not show on browser. This hidden element will also get submit when u submit form. this can be retrieved on server by $_POST["cellCount"].
Hope this will help u.
Is there a way that I can post two values at the same time from a single field in a table but hide one from the user?
I would like the following form to post the values ID and reason_name when it is submitted but have the user only be able to see (and edit in the text box) the reason_name.
<form name="add_positioning" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1" class="autoTable_pos">
<tr>
<td>Positioning</td><td> </td></tr>
<?
$sql= "SELECT * FROM gradeReason WHERE reason_userID = $user_id AND category = 'positioning' AND current = 1";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="text" name="reason[]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<?
}
?>
<tr>
<td>
<input type="text" name="reason[]" size="25"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Save" name="save_reasons"/>
</td>
</tr>
</table>
</form>
The form POST action so far (basic echo at the moment for my own sanity to check that it posts the values correctly, which it does...)
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $item) {
echo $item.'<br/>';
}
}
This table displays the values that are held in a database but enables the user to add more values by dynamically adding a new row (using JQUERY I haven't included) to the table when they type in an empty one at the bottom and also allows them to edit or delete existing values.
For each value posted I intend to check if the ID value is empty, if it is it means that it is a new value and enter a new record into the database, if it isn't update the existing record in the database with the corresponding ID. I don't have a problem writing that bit, I just can't think how to get the ID value posted as well as the reason_name while keeping ID hidden from the user.
Add the ID to the name attribute of the Text boxes of the reasons loaded from the DB. Leave the other Text boxes added using JQ without the ID.
E.g.
Text box which shows the existing reason loaded from the DB
<input type="text" name="reason[][ID]" size="25"/>
Text box added with JQ
<input type="text" name="reason[]" size="25"/>
Then once you submit the form you get you will get the following array.
array
'reason' =>
array
0 =>
array
14 => string 'VAL1' (length=4)
1 => string 'VAL2' (length=5)
By checking for an array in the each element in the "reason" array, you can differentiate two type of Text Boxes.
Here is the complete code I have tested.
<?PHP
//var_dump($_POST);
foreach($_POST['reason'] as $item=>$value)
{
if(is_array($value)){
foreach($value as $ID=>$reason)
{
echo "Existing Reason : ".$ID." - ".$reason."<br>";
}
}
else{
echo "New Reason : ".$item." - ".$value.'<br/>';
}
}
?>
<form action="" method="POST">
<input type="text" name="reason[][14]" size="25" value="aaaa"/>
<input type="text" name="reason[]" size="25" value="bbbbb"/>
<input name="" type="submit">
</form>
Assuming the id is at $row['reason_id'] I think you want:
$i = 0;
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="hidden" name="reason_id[<? echo $i; ?>]" size="25" value="<? echo $row['reason_id']; ?>"/>
<input type="text" name="reason[<? echo $i; ?>]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/></td></tr>
<? $i++ } ?>
This way you can later
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $key => $item) {
$id = $_POST['reason_id'][$key];
echo $id . " " . $item.'<br/>';
}
}
I have a form that has a foreach loop and inside the loop I have an input type radio, so every time it has a new value like the codes below:
<?php
$aircrafts = AircraftPermit::getaircraft();
foreach($aircrafts as $aircraft)
{
$pacas = AircraftPermit::getrouteaircraft($aircraft->name ,$pilotid);
if($pacas)
{
?>
<tr>
<td>
<input type="radio" id="ac" value="" disabled="disabled"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit" disabled="disabled">
</td>
<td align="center">
<font color="green">Granted</font>
</td>
</tr>
<?php
}
else
{
?>
<tr>
<td>
<input type="radio" id="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
</td>
<td align="center">
<input name="id" type="hidden" value="<?php echo $pilotid ;?>">
<input name="submit" title="Give permission to selected aircraft" type="submit" value="Give Permit">
</td>
<td align="center">
<font color="red">Restricted</font>
</td>
</tr>
<?php
}
}
?>
Now at the end I have a script to check if the radio button is selected like below:
<script type="text/javascript">
function radio_is_on() {
var elementId = document.getElementById('ac');
if (elementId.checked==false)
{
alert('Please select the aircraft first!');
return false;
}
if (elementId.value)
{
return true;
}
}
</script>
When the radio button is not pushed the message pops up okay but when it's pushed it returns no value and the form send null value. Please tell me where I'm wrong.
Thanks
You cannot have multiple id tags, which is probably why it's not working. Try changing it to a class instead. See below for example in jquery.
HTML:
<input type="radio" class="ac" value="<?php echo $aircraft->name ;?>"><?php echo $aircraft->name ;?><br />
JAVASCRIPT:
function radio_is_on() {
if ($('.ac:checked').val()) {
return true;
}
else {
alert('Please select the aircraft first!');
return false;
}
}
You have same id for all radio buttons - that is not correct.
And only one button is considered all the time.
You can number them, eg. ac1, ac2 and so on.
as far as I can remember a radio button is "pushed" when it has the checked attribute, and it is not pushed when it doesn't have it.
...
if (elementId.hasOwnProperty('checked'))
...
instead of
...
if (elementId.checked==false)
...