I have a checkbox table declared like this:
for ($x = 0; $x < 6; $x++) {
echo "<tr>";
echo "<td>"; echo $days[$x]; echo '</td>'; //displays days
echo '<td><input type="checkbox" name="check_list[]" onClick="toggle(this, '.$x.')" value="1"/> All';
echo '<input type="hidden" name="check_list[]" onClick="toggle(this, '.$x.')" value="0"/></td>'; //creates check all buttons
for ($y = 0; $y < 12; $y++){
echo '<td><input type="checkbox" name="'.$x.'" value="1"> Bar 1<br/></td>'; //creates the other buttons
}
echo "</tr>";
}
The name="check_list[]" checkbox selects all the checkboxes in the same row when checked. It is done with this script:
<script language="JavaScript">
function toggle(source, id) {
checkboxes = document.getElementsByName(id);
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
Then the data are stored like this in the database:
<?php
$i=0; $store[20]; $check[20];
foreach($_POST['check_list'] as $value){
$store[$i] = $value; $i= $i + 1;
}
$check = str_split(preg_replace('/10/','1',implode('',$store)));
array_walk($check, 'intval');
if($check[0]) { $monday = 1; } if(!$check[0]) {$monday = 2; }
if($check[1]) { $tuesday = 1; } if(!$check[1]) {$tuesday = 2; }
if($check[2]) { $wednesday = 1; } if(!$check[2]) {$wednesday = 2; }
if($check[3]) { $thursday = 1; } if(!$check[3]) {$thursday = 2; }
if($check[4]) { $friday = 1; } if(!$check[4]) {$friday = 2; }
if($check[5]) { $saturday = 1; } if(!$check[5]) {$saturday = 2; }
?>
Obviously, this is not the whole code (I wrote the sql query to connect and insert the variables for the days. And it worked). But till now I am only taking the values of the first checkbox for each row. Which means that if I check it, all the checkboxes of that row are checked. But I am only reading the fact that all the checkboxes of that row are checked. If I only check a normal checkbox in a row, its value is not posted and I the only info stored is that not all checkboxes are checked (represented by value 2).
What I want to do is take the value of the normal checkboxes for each row instead, and concatenate them in a number. For example, for monday checkboxes (checked, unchecked, unchecked, checked) i would store $monday = 1221.
The same must be done for all days (monday to saturday)
You can do as follows:
for ($y = 0; $y < 12; $y++){
<input type="text" name="matrix['.$x.']['.$y.']" value="Text" />
}
And in your sever:
foreach($_POST['matrix'] as $key => $value) {
echo $key;// Value of $x
foreach($value as $k => $val) {
echo $k;// This would be the value of $y
echo $val;// This would be the value of the inputs
}
}
The way you describe it, it is not possible. It is not possible to put an array declaration as a variable name. However, I somewhat get (and assume) a picture of what you want to do.
After creating the input elements dynamically, also create a hidden input element that contains the "count" of how many elements where created. Then, in PHP code, first access the count element to find out how many elements are present. Then, loop through that index, to build the loop name for each element and access their values.
I appreciate the suggestions given, but I actually tried an alternative solution. Instead of referring to the elements by name or id, I refer to them by class and by name. I use the class (variable) to do the toggle function and the name (which is a one dimensional array) to get their values.
In short, this is the general idea of the code:
The checkbox declaration
echo '<td><input type="checkbox" class="'.$x.'" name="check_list[]" value="1"> <br/>';
The toggle function:
function toggle(source, id) {
checkboxes = document.getElementsByClassName(id);
for(var i=0, n=checkboxes.length;i<n;i++) {checkboxes[i].checked = source.checked;}
}
The php code to get the values:
$i=0; $unclean[180];
foreach($_POST['check_list'] as $value){
$unclean[$i] = $value; $i= $i + 1;
}
Then, knowing the dimensions the matrix had, i simply separate this one dimensional array into an array of one dimensional arrays (basically a matrix) iterating through it with a for loop
Related
My problem is that the below code changes the value of the checked ones, but the index i is assigned to the checked ones only. I want it to pass through all check boxes. For examples if the first and last check boxes were checked, I am getting job[0] is "yes" and job[1] is "yes" and the rest are "no" while I should get "yes" for job[0] and job[4].
Code below:
if(!empty($_POST['job'])){
$i = 0;
$jobArray = array("no","no","no","no","no");
// Loop to store and display values of individual checked checkbox.
foreach($_POST['job'] as $selected){
$jobArray[$i] = "yes";
$i++;
}
} //$_POST['job']
The thing is that checkboxes values exist in your request only if they are checked. You should keep it in mind.
So, you can change your application logic to work with that. But if you really need to have an array with 'yes', 'no' values, just create it.
Let's say you create your checkboxes like so:
<?php for ($i = 0; $i < 10; $i++): ?>
<input type="checkbox" name="job[<?= $i ?>]" value="yes">
<?php endfor; ?>
Then, in your code:
$jobs = [];
for ($i = 0; $i < 10; $i++) {
$jobs[$i] = $_POST['job'][$i] ?? 'no';
}
Good evening,
I'm trying to find a function, where I can border the last table row from a dynamic created table.
<?php
session_start( );
echo '<br><br><br><h1 class="HLast">Your last order</h1>';
if( isset( $_SESSION['oldSession']) === true ){
$price = 0;
$resultsetOldOrder = $dbr->query( ' SELECT sn.preis, sn.name from food_order fo, speisen_neu sn
where fo.SID = "'.$_SESSION['oldSession'].'"
and sn.Artikelnummer = fo.artikelnummer ' );
if( $resultsetOldOrder == true){
echo '<table class="LastText">';
foreach( $resultsetOldOrder as $row ){
echo '<tr><td>'.$row['name'].'</td><td>'.$row['preis'].'</td></tr>';
$price = $price + $row['preis'];
}
echo '<tr><td>Total</td><td>'.$price.'</td></tr></table>
<table class="LastButton"><tr><td><form action = "printBill.php" method = "post">
<input type = "hidden" name = "oldOderID" value ="'.$_SESSION['oldSession'].'">
<button type = "submit">Print bill</button></form></tr></table>';
}else{
echo 'You have no last order';
}
}
?>
This one up here is my code. It works fine.
In a normal table i would simply try to give the last td a class and set a border around. But this is not possible here and I can't find anything in the web aswell.
Hope you can help me, thanks.
Well, while there's a better way to do this with css, to do this without making much changes to your codes, you can count the total number of rows in $resultsetOldOrder and then check within the loop if the current row is the last one.
if( $resultsetOldOrder == true){
$last_index = count($resultsetOldOrder) - 1;
echo '<table class="LastText">';
foreach( $resultsetOldOrder as $index => $row ){
if($index == $last_index) {
//This is the last row, do whatever you want
}
echo '<tr><td>'.$row['name'].'</td><td>'.$row['preis'].'</td></tr>';
$price = $price + $row['preis'];
}
echo '<tr><td>Total</td><td>'.$price.'</td></tr></table>
<table class="LastButton"><tr><td><form action = "printBill.php" method = "post">
<input type = "hidden" name = "oldOderID" value ="'.$_SESSION['oldSession'].'">
<button type = "submit">Print bill</button></form></tr></table>';
}else{
echo 'You have no last order';
}
Of course this is assuming $resultsetOldOrder array is indexed 0 to x. If its not, you can set $i = 0 before the loop and check inside the loop if $i == $last_index then do $i++.
I hope this helps.
With CSS you can select the last element of a type.
Take a look at this.
So in your case, you would do
tr:last-of-type {
//your styles here
}
Below is my code
if (!empty($_POST['ok'])) {
$errorMessage = array();
$loopcount = 0;
$i = 0;
foreach ($_POST['theDate'] AS $i => $theDate) {
if ($_POST['EW'][$i] == 'EW') {
$ew = "yes";
} else {
$ew = "no";
}
$i = $i + 1;
echo $ew;
}
}
its pulls the checkbox value of below and assigns it yes or now if value == ew
E/W<input name="EW[]" ID="EW[]" value="EW" type="checkbox" />
the issue is if check these
row checked
1 no
2 yes
3 no
3 yes
the out result when submitted is
row checked
1 yes
2 yes
3 no
4 no
It seems to stick anything checked as ew to the top and I don't get why here is a live working example that when submit is clicked echos with output.... all fields need to be filled but is u just add a number i will work
http://runningprofiles.com/tests/addbet.php
The checkbox is not submitted, if it does not have a value, thus, the 'yes' are the only ones in the loop. the index numbers for that field only from the submitted ones.
edit - clearification: if not checked - it its value does not get submitted.
foreach ($_POST['theDate'] AS $i => $theDate)
You missing the { at the end
like this
foreach ($_POST['theDate'] AS $i => $theDate){
I have a dynamic web form that creates text after the user tells it how many he wants, what I want to do, is to get the info of those text fields into the next form, I've read a question that is
pretty much what I want to do;
But I haven't had any luck so far;
for ($i = 1; $i <= $quantity; $i++) {
echo "<input type='text' class='text' name='classEmpleado[]' id='empleado$i' />";}
When I try to retrieve them I use this;
$empleado[] = $_POST['classEmpleado[]'];
$i = 0;
for ($i = 1; $i <= $quantity; $i++) {
echo "$empleado[$i]<BR><BR>";
}
But I get the error Undefined index: classEmpleado[]
What am I doing wrong?
ANSWERED!
For anyone looking for the same thing, look at the response of Sherbrow,
And you would just have to edit the loop to this
$empleado[] = $_POST['classEmpleado[]'];
$i = 0;
for ($i = 0; $i < $quantity; $i++) {
echo "$empleado[$i]<BR><BR>";
}
If $empleado is not previously declared or just empty, you are looking for that
$empleado = $_POST['classEmpleado']
But if $empleado is an array, and contains data, you may want to merge everything in one only array
$empleado = array_merge($empleado, $_POST['classEmpleado']);
Whichever way you choose, there should be a check to be certain that $_POST['classEmpleado'] is defined and is an array. Something like :
if(isset($_POST['classEmpleado']) && is_array($_POST['classEmpleado'])) {
/* ... */
}
Try $empleado[] = $_POST['classEmpleado'];
When you put name[] at the end of the fieldname, it will be passed to PHP as an array in $_POST with the key of name like so: $_POST['name'] = array(1,2,3,4);
This statement here is wrong ($empleado[] = $_POST['classEmpleado[]'])
If you want to access $_POST the input field named classEmpleado[], you have to do so :
for($i=0; $i<count($_POST['classEmpleado']); $i++)
{
echo $_POST['classEmpleado'][$i] . '<br /><br />';
}
This is a code to enter date, select in/out and location.
If the user want more fields to enter i also added a addRow function.
<table>
for($i=0;$i<15;$i++){
<tr><td>
<input type='datepick' name='scheduledatepick[$i]' />
<select name='schedulein[$i]' /><option>--</option>
<input type='text' name='location[$i]' />
</td></tr>
}
</table>
Now my question is if a user entered a field in a row(maybe datepick or schedulein or location) then he must enter all the other fields in that same row. How to achieve this?
Assuming you want this to happen on a button click, you can do this: Working Demo
jQuery
$('button').click(function() {
// set up an array to store the invalid rows
var rows = new Array();
$('table tr')
// reset all rows before we validate
.removeClass("error")
// loop over each row
.each(function(i) {
// work out whether the fields are completed or not
var filledFieldCount = 0;
filledFieldCount += $("[name='scheduledatepick[" + i + "]']", this).val().length > 0 ? 1 : 0;
filledFieldCount += $("[name='schedulein[" + i + "]']", this).val() !== "--" ? 1 : 0;
filledFieldCount += $("[name='location[" + i + "]']", this).val().length > 0 ? 1 : 0;
// if the total completed fields for this row
// is greater than none and less than all
// then add the row to the invalid rows list
if (filledFieldCount > 0 && filledFieldCount < 3) {
rows.push(this);
}
});
// finally, change the background of the
// rows to mark them as invalid
if (rows.length > 0){
$(rows).addClass("error");
}
});
CSS
.error { background-color: red; }
PHP-side:
$errs = array();
for ($i = 0; $i < 15; $i++) {
$cnt = empty($_REQUEST['scheduledatepick'][$i]) + empty($_REQUEST['schedulein'][$i]) + empty($_REQUEST['location'][$i]);
if ($cnt > 0) && ($cnt != 3) {
$errs[] = "Row $i not completed";
}
}
if (count($errs) > 0) {
... at least one incomplete row
}
Javascript-side would be somewhat equivalent, with extra code to handle differences between selections, checkbox, text fields, textareas, etc...
You need to parse the form.
If a line is incomplete, throw an error message to the user.
All this in Javascript.
Then you have to implement the same check in PHP. The Javascript check is convenient to give an immediate response to the user, but it can be easily neutralized.