I dont know how to manage this situation, I'm a noob coder, I have a page that shows you all available lots where you can unload a specific item from that lot.
This is the foreach that prints out:
$lotto, $totalelotto, $data, and ask for qtyvalue to unload from $lotto (input can be also NULL)
foreach ($dataslotto as $data) {
$totalelotto = totlotto($database, $data['lotto']);
$lotto = $data["lotto"];
$data = $data["data"];
echo "<tr>";
echo "<td>".$lotto."</td>";
echo "<input type=\"hidden\" value=\"".$lotto."\" name=\"array[]\" />";
echo "<td>".$totalelotto."</td>";
echo "<input type=\"hidden\" value=\"".$totalelotto."\" name=\"array[]\" />";
echo "<td>".$data."</td>";
echo "<input type=\"hidden\" value=\"".$data."\" name=\"array[]\" />";
echo "<td><input type=\"text\" class=\"form-control\" placeholder=\"Qta.\" required name=\"qtyvalue\"></td>";
echo "</tr>";
}
I dont know how to set name="" of input fields (because the number of fields can change if there are many lots) and I dont know how to send $_POST data as array, and then foreach group of $lotto, $totalelotto, $data, $qtyvalue where is set $qtyvalue do another query.
I put it in no regular code, I know it looks bad but it's just for giving you an idea.
$_POST[''formarray];
foreach ( /* values recieved in each <tr> inside formarray where $_POST['qtyvalue'] is not empty */ ){
#EXECUTE THIS
}
Thanks for help!!
And sorry for my bad coding skills.
$_POST is an Associative/Key Value pair, it's key is whatever is set as the inputs name.
so if you wanted to send the users input username to the backend PHP script
You set the value and it's name
<input type="text" name="username" value="User123">
then to retrieve the user name you can do
print_r($_POST["username"]);
to print the value.
So you ask how do you loop over each one in $_POST, that's pretty simple you can could a foreach loop over the entire $_POST array.
foreach($_POST as $key => $value)
{
//check something has been entered for the current value we are iterating over
if($value != null)
{
print_r($key . " value is : " . $value);
}
}
this would loop over each item in the $_POST array with key being set to whatever the DOM elements name is.
Don't forget the $_POST array is just that an array, you could do
var_dump($_POST);
and see everything that was sent in the POST request.
You can use array names for your inputs. Say you have a row of your table like this:
<tr>
<td><input ... name="lotto[]"></td>
<td><input ... name="totalelotto[]"></td>
<td><input ... name="data[]"></td>
<td><input ... name="qtyvalue[]"></td>
</tr>
Then you will get arrays $_POST['lotto'], $_POST['totalelotto'] and so on, each with the same number of elements, and elements with same index belonging to one row of the table. You could then process those elements like this
foreach ($_POST['lotto'] as $i=>$lotto) {
if ($_POST['qtyvalue'][$i] > 0) {
...
}
}
I have a form that has several input types all that need to be a handled differently. I did a loop to name them like so in the form:
product-0-length
product-0-size
product-1-length
product-1-size
product-2-length
product-2-size
On my processing php (where the form info gets sent) I want to iterate a loop to handle say size different from length. My thought was this but no luck:
<?php
$i = 0;
foreach($_REQUEST['product-'.$i.'-length'] as $key => $val) {
//style or do what I need with the length information for each product
echo '<li>'.$key.'='.$val .'</li>';
$i++;
}
?>
As I suggested in the comments above you could use a different naming scheme for your input fields. Take a look at this example for the form creation:
<?php
foreach ($i=0; $i<3; $i++) {
echo "<input type=\"text\" name=\"product[$i][length]\">\n";
echo "<input type=\"text\" name=\"product[$i][size]\">\n";
}
When submitting this form the server will translate the notation into a php array which is a very convenient thing. You can simply iterate over that array:
<?php
foreach ($_POST['product'] as $key=>$product) {
echo "<li>Length of product $key: " . $product['length'] . "</li>\n";
echo "<li>Size of product $key: " . $product['size'] . "</li>\n";
}
Note that this is a very primitive example, all error checking and the like is missing to keep things crunch. Also I did not test this code here, it is just meant to point you into the right direction, but I hope there are not too many typos in there...
I'm creating a crossword page on my site, I produce a grid (form) with 8 x 8 input boxes. when I fill in the answers they are sent to a page to assess them but this is where i have the problems.
the values are sent using form method GET, so if the top line read q,w,e,r,t, , ,y then it would pass this over like this :-
submit.php?0[0]=q&0[1]=w&0[2]=e&0[3]=r&0[4]=t&0[5]=&0[6]=&0[7]=y&1[0]
But when I put the values into an array, the empty values are left out, so when printing out the array it reads as "q,w,e,r,t,y" instead of "q,w,e,r,t, , ,y"
I currently fill the array like this :-
$one = $_GET['0'];
My HTML form is:
<form action='submit.php' method='get' name='xword' id='xword1'> <?php
$array=array(
array("h","e","l","l","o","0","0","0"),
array("e","0","0","0","0","0","0","0"),
array("l","0","0","0","0","0","0","0"),
array("l","0","0","0","0","0","0","0"),
array("o","0","0","0","0","0","0","0"),
array("0","0","0","0","0","0","0","0"),
array("0","0","0","0","0","0","0","0"),
array("0","0","0","0","0","0","0","0"),
);
$X = 0;
while ($X < "8") {
$Y = 0;
while ($Y < "8") {
if ($array[$X][$Y] == "0") {
echo "<input type='text' id='text' name='" . $X . "[$Y]' class='blank'>";
} else {
echo "<input type='text' id='text' name='" . $X . "[$Y]' class='text'>";
}
$Y++;
}
echo "<br />";
$X++;
}
?> <input type='submit' value='Submit'>
Where am I going wrong?
I don't see the empty spaces within the array being left out.
This is the output of print_r($_GET) when I type qwer in the first four text fields and ty in the last two in the first row.
Getting the data with $one = $_GET['0']; works fine (without quotes work too), and when I implode the first row, it outputs exactly what you had expected.
echo implode(', ', $_GET[0]);
OUTPUT: q, w, e, r, , , t, y
In PHP, I would like to know how to provide the same name to different elements and still echo them out individually. I think the array mechanism works here but I don’t know how.
echo "<input type='checkbox' name='seat[]' id='something'/>";
echo "<input type='checkbox' name='seat[]' id='something1'/>"
Then to echo their values out I use the following:
<?php
if(isset($_POST['seat[]']))
{
echo ' ', $_POST['seat[]'] ;
}
?>
Please guide me!!
It's just an array of data. You can access elements of it just like any other array:
foreach ($_POST['seat'] as $seat) {
echo $seat . "<br>\n";
}
or using a numerical index:
echo $_POST['seat'][0]; // value of the first submitted checkbox
I'm working on this code in PHP and basically I have 5 checkboxes, for 5 individual items, each called "ItemCheck" and they have values of 0-4. Now I wrote a code where it has it so it displays the numbers that are checked from those 5 check lists.
Form:
for ($i=0;$i<count(5);$i++){
echo "
<input type='checkbox' name='ItemCheck' value='$i.check'>$i</input><br>"}
PHP Process:
if (isset($_POST['ItemCheck'])){
for ($o=0;$o<$ItemCount;$o++){
if($_POST['ItemCheck'] == $o.'.check') {
echo "Item " . $o . "<br>";
}
}
}
else{ echo "You must select at least one product";}
Although say if I check box #1,2 & 3, the final output will only display "Item 3". Now matter how many checkboxes you select, it will only display the one with the highest value and none other. Does anybody know what's wrong with the code and how to have it so it displays each individual number that's selected, and not just the one with the highest value?
What you want to do is set the name of the checkbox element ItemCheck[] this will make $_POST["ItemCheck"] an array.
Example:
for ($i=0;$i<count(5);$i++){
echo "<input type='checkbox' name='ItemCheck[]' value='$i.check'/> $i<br>";
}
Another thing to note is the browser won't post up anything for an unchecked checkbox so I think your processor needs to be like this.
<?php
if (isset($_POST['ItemCheck'])){
for ($o=0;$o<count($_POST['ItemCheck']);$o++){
echo "Item " . $_POST['ItemCheck'] . "<br>";
}
} else {
echo "You must select at least one product";
}
?>
Another thing so make sure of is that your form has the method set to POST