I am trying to store the values of selected checkboxes on a multi page form so I can include these on the final page of the form (and in the email that is sent to the site owner).
I have worked out how to display the values, but saving them for later has got me stumped. I'm learning as I go so I wouldn't be surprised if this is quite easy...
This is the code I'm using:
<?php foreach ($_POST['fooby'] as $key => $entry) {
if(is_array($entry)){
print $key . ": " . implode(',',$entry) . "<br>";
}
else {
print $key . ": " . $entry . "<br>";
}
} ?>
And this is the result I get:
1: Minor Service £129
2: plus MOT £35
That's exactly what I'm after - though I don't need the numbers at the beginning. How do I save that information for later?
With the updated code below, I now get the following result:
Minor Service £129
plus MOT £35
That's perfect, but I'm struggling to work out how to store that information to a session variable. I should point out that the values returned from the form are dynamic and unknown beforehand. There might also be 10 items in the array, not just the two shown above.
What I have so far:
<?php if (isset($_POST['fooby'])){
foreach ($_POST['fooby'] as $entry) {
if(is_array($entry)){
$dokval = implode(',',$entry) . "<br>";
echo $dokval; //echoes the expected result on the page
$_SESSION['dokvalues'] = $dokval; //only stores the last item
}
else {
print $entry . "<br>"; //not rewritten this part yet
}
}
} ?>
Simply insert these values in hidden input elements in a form on the following pages to use them again:
<input type="hidden" name="<?php echo $key1; ?>" value="<?php echo $value1; ?>" />
<input type="hidden" name="<?php echo $key2; ?>" value="<?php echo $value2; ?>" />
...
The foreach loop iterates through the array and for each iteration $key variable is the current index and $entry is the value of that index. The numbers in the list are just representation of index values. If you don't need them, you can go for this:
<?php foreach ($_POST['fooby'] as $entry) {
if(is_array($entry)){
print implode(',',$entry) . "<br>";
}
else {
print $entry . "<br>";
}
} ?>
The keys will still stay in $_POST['fooby'] array.
The variable $entry holds one value of the array each time the foreach loop iterates. So the variable $dokval will also hold only one value, which it echos each time the loop iterates. By the time you look at the session variable it's value is going to be the last value that $dokval held. Make $dokval an array and push the $entry value into it. array_push. Also, make sure you start a session at the beginning of every page you want to use the $_SESSION variable.
Jeff
Related
In this code, I can't fetch id value. How can I give a number instead of if(strlen) ? And how can I give remove one by one item ?
<?Php
#$product=$_POST['product'];
if (strlen($product)>3) {
array_push($_SESSION['cart'],$product); // Items added to cart
}
?>
<?Php
if (isset($_SESSION['cart'])) {
echo " <br><a href=cart-remove-all.php>Remove all</a><br>";
while (list ($key, $val) = each ($_SESSION['cart'])) {
echo "$key -> $val <br>";
} else {
echo " Session Cart is not created. Visit <a href=cart.php>cart.php</a> page
to create the array and add products to it. ";
}
?>
<form method="post">
<input type="hidden" name="product" value="<?php echo $row['jobtitle'];?>">
<input type="submit" style="float:right;font-size:15px;font-weight:600;" value="+ Add to Short List">
</form>
to remove all, just do :
unset($_SESSION['cart']);
and, rather than using a while loop, you can use foreach loop.you can remove the item by using its array index in $_SESSION['cart'];
If I have understood your question clearly, you can use
if(isset($_SESSION['cart'][0]))
unset($_SESSION['cart'][0]);
Likewise, you can change 0,1,2...n to unset the nth record. BTW, once you unset one record the number of records in the array will become n-1 from n. if you keep doing the above code repeatedly until the count of the records become 0 also will work
Use foreach
foreach($_SESSION['cart'] as $key=>$value){
if($ke==="yourkey"){
unset($_SESSION['cart'][$key]);
}
}
yourkey is the key, you want to remove.
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 been trying to do this for a few days, and I cant seem to find anything on the net about it, but then again I am not really sure what I should be searching, plus it may not even be possible.
So I have a form which is looped dependent on how many parcels they wish to send, I loop the names using the index in the loop, as you can see below. so it goes like 'Weight . $i' = 'Weight1' and so on...
I then stores these into session variables, but now I want to pull through all the session variables for those that are set, and I wanted to be able to loop through the session variables as I am trying to display like a summary page of all the parcel details, as shown in below code?
I have sessions called PWE1, PWE2 etc and want it to loop through them rather than calling them individually.
Is this possible? if so how?
<h4>Parcel Details</h4>
<?php
if($SV != null){
$i = 0;
do {
$i++;
?>
<h3>Parcel <?php echo $i;?> </h3>
<p> Weight: <?php echo $_SESSION['PWE' . echo $i ]; ?> </p>
<?php
} while ($i != $SV);
}
?>
Why not use a 2d array and loop through it?
// store your PWE1, PWE2 etc in here
$_SESSION["weights"]["PWE1"] = $value;
$_SESSION["weights"]["PWE2"] = $value2;
$_SESSION["weights"]["PWE3"] = $value3;
$i = 0;
foreach($_SESSION["weights"] as $pwe) {
echo '<h3>Parcel' . $i . '</h3>';
echo '<p>Weight:' . $pwe . '</p>';
$i++;
};
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 new to php and I was wondering how I would be able to create something like a delete button for deleting items in a list that would be generated from a dynamically growing array.
An example of what I mean is this:
<?php
if (isset($_REQUEST['foo']))
{
if (isset($_SESSION['words']))
{
$_SESSION['words'][] = 'added word';
}
else
{
$_SESSION['words'] = array('cat', 'dog', 'you', 'me');
}
foreach ($_SESSION['words'] as $key => &$value)
{
echo "<p>" .
$value .
" - <input type='submit' name='delete_" .
$value .
"' value='Delete Entry' /></p>";
}
if (isset($_REQUEST['clear']))
{
session_destroy();
}
?>
Where, on every button click that gets sent to my script it would echo out the array with the buttons.
I'd like to link the delete buttons to a function that looked something like:
function delete_entry( $index )
{
unset($_SESSION['words'][$index]);
$_SESSION['words'] = array_values($_SESSION['words']);
}
Is what I'm asking even possible?
Your array of words seem to be stored in your session variable, so I'm assuming that you want to remove/add words to it. How about this...?
Have a separate form for each word with a hidden field saying what the word is:
So in the for loop:
echo "<form><p>".$value." - <input type='submit' value='Delete Entry' /></p><input type=\"hidden\" name=\"delword\" value=\"".$value."\"/></form>";
if(isset[$_REQUEST['delword']]) remove it from the session array (do this before you do your echoing for loop. (You could use array_search to find the element, then run unset as you suggested)
Let me know if you want me to elaborate on this suggestion.