while($row = mysql_fetch_array($result)){
echo '<tr>
<td>'.$row[0].'</td>
<td>'.$row[1].'</td>
<td>'.$row[2].'</td>';
$price=$row['price'];
echo "<td><input type='checkbox' name='er' value='$price'></td>";
echo "</tr>";
PHP file
<?php
$ercharge=$_POST['er'];
echo $ercharge;
?>
I have a list of charges that was from a mysql table and it has a checkbox for each item so it would compute the sum. The above code works and it outputs the price of the checked item. The problem is, it's only one item. When multiple items are checked, only one is outputted.
Try this:-
echo "<td><input type='checkbox' name='er[]' value='$price'></td>";
Try this it may help you
echo "<td><input type='checkbox' name='er[]' value='$price'></td>";
echo "</tr>";
$recharge=$_POST['er'];
foreach($recharge as $val)
{
echo $val;
}
Or
you can just do this without using foreach
$arra_val=array_sum($recharge);
you just need to put the value in to some variable and echo it.
If you want to pass a number of elements in an array append the name with square brackets, so it would be:
echo "<td><input type='checkbox' name='er[]' value='$price'></td>";
And the result of $_POST['er'] would be an array of all of the checkbox values.
The input box name should reflect that it's an array by adding the [] suffix:
echo '<td><input type="checkbox" name="er[]" value=" . htmlspecialchars($row['price']) . "></td>';
Then, to process the sum after making sure the input validates as proper values:
if ($ers = filter_input(INPUT_POST, 'er', FILTER_VALIDATE_FLOAT, FILTER_FORCE_ARRAY)) {
$ercharge = array_sum($ers);
}
Related
I have a table with rows that are in the form:
ID, Name, First Name, IntA, IntB, IntC, ...
The last columns are textfields.
I want to select multiple rows with a Checkbox and send the ID and the recorded integers with POST to another PHP-File.
This is my code-example (with a little bit German in it):
echo "<td><input type='checkbox' name='auswahl[]' value='$daten[0]'></td>";
echo "<td>" . $daten['ID'] . "</td>";
echo "<td><input type='number' name='Schr' value='".$punkte['PktSchr']."'></td>";
echo "<td><input type='number' name='Mdl' value='".$punkte['PktMdl']."'></td>";
So in the other PHP-File I want to SQL for all checked rows like this (in a Loop):
$Eintraege = $_POST['auswahl'];
$Schr = $_POST['Schr'];
$Mdl = $_POST['Mdl'];
foreach ($Eintraege as $i){
$sql = "INSERT INTO aufnahme (IDSchueler, PktSchr, PktMdl) Values ('$i', '$Schr', '$Mdl')";
}
My problem is this: $Eintraege contains only the IDs of the selected rows (because of value='$daten[0]').
$Schr and $Mdl contains the values of the textfields of the last row (doesn't matter, wich rows are selected).
So I tried to set value='$daten' and use it lika an array, but then I get an Exception.
I think I have to change value='$daten[0]', but don't know how.
Thanks for your help!
Change two other names to array type too and you will get all values.
So do like below:-
echo "<td><input type='number' name='Schr[]' value='".$punkte['PktSchr']."'></td>";
echo "<td><input type='number' name='Mdl[]' value='".$punkte['PktMdl']."'></td>";
And then you can do like below:-
foreach ($Eintraege as $k=> $i){
$sql = "INSERT INTO aufnahme (IDSchueler, PktSchr, PktMdl) Values ('$i', '$Schr[$k]', '$Mdl[$k]')";
}
As #Alive-to-Die mentioned, turn the names into arrays.
<input type='checkbox' name='auswahl[]' value='$daten[0]'>
This itself doesn't look good. Consider making it
<input type='checkbox' name='auswahl[]' value='{$daten[0]}'>
or
<input type='checkbox' name='auswahl[]' value='".$daten[0]."'>
These are two proper ways to use variables inside strings.
I'm trying to create a very easy stock managing system. I'm able to show all the items in my table 'parts' and i'm showing the amount in a textbox. However, when i change the value from, for example, 0 to 5 in the textbox and i press my submit button, it doesn't update the stock.
Below is my code, i don't have alot of experience with update querys but i've read about it on php.net, obviously.
<?php
echo "<table width=\"800\" class=\"nieuws\">";
$db=mysqli_connect("localhost","root","","lichtwinkel");
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<form method='post' action=''>";
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal' value=$row[voorraad] /></td>";
echo "<td><input type='submit' id='update' name='update' value='Update' /></td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['aantal']) && $_POST['update']) {
$y = $_POST['aantal'];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '$y' WHERE partnr = $row[0]");
}
echo "</form>"
?>
Simply said, what i'm trying to achieve is the following:
Whenever i change the value displayed in the texbox, and i press my submit button, i want it to update the value in the database.
Does anyone know what i'm doing wrong? Any ideas? Articles i should read?
All help would be appreciated.
Thank you.
As i see, you were doing it wrong at all.
First you can't use form tag within more then one td element.
You were didn't close the form tag, only at end. (So if it loops 6 times, you will have 6 forms open, but one ended!).
At update, you're selecting row[0] - it's outside of loop with rows?
Even if you update it, it will show wrong results again. Update should be above selects! So it picks up newly updated value.
What to do:
First make one form for all updates.
Use your submit button to have value DATABASE_ID.
Make the name of "aantal" to "aantalDATABASE_ID".
At submit check for $_POST['update'], and use it's value (DATABASE_ID) to get input $_POST["aantal".$_POST['update']].
Do update, you have all you need.
Example:
<?php
echo "<form method='post' action=''>";
echo "<table width=\"800\" class=\"nieuws\">"
$db=mysqli_connect("localhost","root","","lichtwinkel");
if(isset($_POST['update']) && !empty($_POST['update'])) {
$y = $_POST['aantal'.$_POST['update']];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '".$y."' WHERE partnr = '".$_POST['update']."'");
}
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal".$row[0]."' value='".$row[voorraad]."' /></td>";
echo "<td><input type='submit' id='update' name='update' value='".$row[0]."' /></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>';
?>
After all, take care about SQL Injections. "aantal" value is user input. As the submit value can be changed.
I have 12 keys inside my $_post[].
I can reach them without any issue but I want to make it easier, as there will be more keys (I am thinking of 30+).
Here is an example of what I have inside my $_post:
$_post['var1']
$_post['qty1']
$_post['var2']
$_post['qty2']
$_post['var3']
$_post['qty3']
This continues till 12 (at the moment).
It is being posted this way:
$count = 0;
foreach ($results as $mat) {
$count++;
echo "<tr><td>{$count}</td>";
echo "<td><input type='text' name='var{$count}' value='{$mat['var']}' /></td>";
echo "<td><input type='text' name='qty{$count}' value='{$mat['qty']}' /></td></tr>";
}
I need to use those variables later to update my sql table, and to have it done one-by-one is a nightmare ( as I am still not sure how many it will be ).
What can I do to achieve it in an easier way ?
you can make name as array like this. var[] and qty[]
echo "<td><input type='text' name='var[]' value='{$mat['var']}' /></td>";
echo "<td><input type='text' name='qty[]' value='{$mat['qty']}' /></td></tr>";
and receive in php like this
$arr_var = $_POST['var'];
$arr_qty = $_POST['qty'];
foreach($arr_var as $key=>$value)
{
$var = $value;
$qty = $arr_qty[$key];
}
foreach($_POST as $key=>$value)
{
$$key = $value; // notice the $$, it's known as variable variable
// remove the below comment if you want to see how they are coming out
// echo "$".$key." = ". $value."<br />";
}
This will create variables names based on the name attribute of the input box.
I want to create a list of checkboxes with values from a php array as its label. I want it to look like
Here is the list of students whose schedules are saved:
[checkbox] Robb
[checkbox] Catelyn
[checkbox] Lady Stoneheart
but my code does not work.
Here's my code:
<?php
$students = $_SESSION['students'];
echo "Here is the list of students whose schedules are saved:<br/><br/>";
echo "<form action='checkbox-form.php' method='post'>
Load student?<br/>";
foreach ($students as $stud) {
echo "<br/><input type='checkbox' name=" . $stud . " value='load' />";
}
echo "<br/><br/><input type='submit' name='formSubmit' value='Submit' />
</form>";
?>
The array is not the problem because it contains the proper values when I print it through foreach.
It might be easier to do it this way:
On the form:
foreach ($students as $stud) {
echo "<br/><input type='checkbox' name=\"students[]\" value='$stud' />$stud<br>";
}
On the handler to see what it's passing:
print_r($_POST);
If all of the "value" fields are "load", which in this case they are, nothing can happen because your PHP won't see anything different value-wise.
You should set the name value of all of these checkboxes to the same thing, and set the value to the student's name (though that's bad design- you should be setting the value to the numeric DB id that represents the student- what if you have students with the same name?)
So:
for($i = 0; i < count($students); $i++) {
$s = $students[$i];
echo "<br/><input type='checkbox' name="students[]" value='$s' />";
}
In this case name="students[]" is the students ARRAY which you can access via $_POST['students'] as an array.
It looks like you've confused the "name" attribute for the label. A few notes:
"name" is used as the name of the parameter passed to the backend
"value" is the value that will be assigned to that parameter if the checkbox is checked
So the line in your foreach should look more like:
echo '<br /><input type="checkbox" name="students[]" value="'.$stud.'" />'.$stud;
If Robb and Catelyn are checked you will get the below in the $_POST['students'] variable server side:
Array
(
[0] => Robb
[1] => Catelyn
)
foreach($students as $student){
echo "<br/><input type='checkbox' name=" . $student . " value=" . $student . " />";
echo "<label for name=" . $student . ">" . $student . "</label>";
}
I'm trying to create an Array from multiple checkboxes that are created via a while loop. the check boxes are named 'to_delete[1]', 'to_delete[2]', etc. etc.
the array statements i've tried are these:
$toDelete = array($_POST['to_delete']);
$toDelete = array($_POST['to_delete'][]);
to veryify there is an array, i go to print but find it is empty print_r($toDelete). What am i doing wrong?
the code is below. Yes, i'm doing this procedurally, and then will re-write as OOP.
big thanks for any help on this!,
$showQ = "SELECT * FROM urls";
$result = mysql_query($showQ);
$numRows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
if(!$result) {
mysql_error();
} else {
echo "<form action='".$_SERVER["PHP_SELF"]."' method='post'>";
$i=0;
while($row = mysql_fetch_array($result)) {
echo "<input type='text' class='manId' name='manId[]' value=" . $row['id'] . " />";
echo "<input type='text' class='url' name='url[]' value=" . $row['url'] . " />";
echo "<input type='checkbox' name='to_delete[" .$i. "]' /><br/>\n";
$i++;
}
echo "<input type='submit' value='delete' name='submit' />";
echo "</form>";
}
$toDelete = array($_POST['to_delete[]']);
print_r($toDelete);
Ultimately, i want to traverse the array, see which ones are checked and then delete the corresponding row from the table.
You don't want to create an array with the array() call, but rather to access one. $_POST will receive input names ending with [] as arrays, so the corresponding array keys in $_POST will already be arrays. The way you've written your code, you are assigning a one-element array containing a sub-array from $_POST. Instead, try the following:
// Test if $_POST['to_delete'] is set and non-empty
// If it's empty, create an empty array, otherwise assign it to $toDelete
$toDelete = empty($_POST['to_delete']) ? array() : $_POST['to_delete'];
print_$($toDelete);