Okay so I have this page with a form where you can select a lot of stuff that is then send by POST to the next stage - The name field is tilbehor_ and then ther id from the db. - examble "tilbehor_23".
Now on the next page I need to make them into a variable so I can call on ther value later.
Example.
tilbehor_11, tilbehor_34 and tilbehor_65 are send by POST to the next page, and I needd to show ther value on the page somwere by the use of a variable
How would i do that?
You can use foreach:
foreach($_POST as $key => $val) {
// $key is now 'tilbehor_23' and $val is its value
}
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value'";
echo $_POST['tilbehor_'.$value];
}
The above will display all the data which is posted. and you just check what is coming and what you want to display.
Let's say a POST variable 'tilbehor_34' has a value of '1'. When you do this:
$key = 'tilbehor_34';
$$key = $_POST['tilbehor_34'];
The variable $tilbehor_34 will now have a value of '1'
Related
I've got a from, with 2 information from 2 field.
Everytime you submit the form you came to another page where I want to display them in an array.
What I did is:
$buchtyp = $_POST['buchtyp'];
$a_id_warenkorb = $_POST['a_id_warenkorb'];
$_SESSION['warenkorb'][$a_id_warenkorb] = $a_id_warenkorb;
If I show the content of this array, it failed.
$buchtyp = $_POST['buchtyp'];
$_SESSION['buchtyp'][] = array($buchtyp);
$a_id_warenkorb = $_POST['a_id_warenkorb'];
$_SESSION['warenkorb'][] = array($a_id_warenkorb);
I changed it and now it works. Its not saved in one array, but its fine :)
Try
foreach($_POST as $key => $value) {
$_SESSION[$key] = $value;
}
I've a page that pass dynamically different $var in URL on another page.
My goal is to retrieve and list this var on the second page in order to pass them to another page.
On page A the user can choose a value via select X, Y, Z (just one, both or as the user likes) the select is passed via form GET.
EXAMPLE
user choice is: X,Z
page B receives http://example.com?X=X&Z=Z
my issue is that I don't know the var name so I cant do $_GET['X'], $_GET['Z']
Please can someone can help?
Many THANKS!!
You can illiterate over whole _GET var like
foreach ($_GET as $name => $value)
And build your request URL
Loop through the $_GET array:
foreach ($_GET as $key => $value) {
echo "$key: $value";
}
Use sensible, known key names:
example.com?choices[]=X&choices[]=Z
var_dump($_GET['choices']);
You can use the $_GET super global array also.
$arguments = array();
foreach( $_GET as $key => $value ) {
$arguments[$key] = $value
}
print_r( $arguments );
$_SERVER["QUERY_STRING"] helps you to detect the query string after the ? in the URL.
I have form with changable content of textareas, from 1 to 5, each time with different names. I cannot modify the form itself.
how can i get number of textareas in form and names of them (it would be the best if i could do it clean in php without javascript).
the form is using method="POST" and PHP version is 5.2+
EDIT: i forgot to tell you that i have only textareas in form.
You could do something along the lines of :
$count=0;
$formElements=array();
foreach ($_POST as $key => $val)
{
$count++;
$formElements[]=$key;
}
echo "The form as $count elements.";
var_dump($formElements);
If you want the values of the post you could make a two dimensional array like this:
foreach ($_POST as $key => $val)
{
$count++;
$formElements[]=array($key => $val);
}
if you post form to php script, $_POST variable is array. then you can use something like this:
foreach($_POST as $v){} to get every field.
I have a form with some checkboxes in Drupal and I need to get the checked boxes to add them to a database. To get the values in the checkboxes I use var_export which returns an array indicating if the checkbox has been checked. After I have this array store in a variable I do this:
$checked = array();
if(is_array($data) {
foreach($data as &$value) {
if($value != 0) { //the checkbox was checked
$checked[] = $value;
}
}
However, when I print out the variable $checked there is nothing stored in it. What am I doing wrong?
The normal way to do this in Drupal would be:
$checked = array_filter($form_state['values']['name_of_checkboxes_element']);
That will give you an array of all the values selected in your checkbox element, assuming you're running this code in the submit/validate handler for the form.
Also I should mention the Devel module, it has a wonderful function called dpm() which prints the value of any variable to the messages area in a hierarchical format that you can navigate through easily.
I post some data over to another page from a form. It's a shopping cart, and the form that's being submitted is being generated on the page before depending on how many items are in the cart. For example, if there's only 1 items then we only have the field name 'item_name_1' which should store a value like "Sticker" and 'item_price_1' which stores the price of that item. But if someone has 5 items, we would need 'item_name_2', 'item_name_3', etc. to get the values for each item up to the fifth one.
What would be the best way to loop through those items to get the values?
Here's what I have, which obviously isn't working.
extract($_POST);
$x = 1; // Assuming there's always one item we're on this page, we set the variable to get into the loop
while(${'item_name_' .$x} != '') {
echo ${'item_name' .$x};
$x++;
}
I'm still relatively new to this kind of usage, so I'm not entirely how the best way to deal with it.
Thanks.
First, please do not use extract(), it can be a security problem because it is easy to manipulate POST parameters
In addition, you don't have to use variable variable names (that sounds odd), instead:
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value'";
}
To ensure that you have only parameters beginning with 'item_name' you can check it like so:
$param_name = 'item_name';
if(substr($key, 0, strlen($param_name)) == $param_name) {
// do something
}
Use array-like fields:
<input name="name_for_the_items[]"/>
You can loop through the fields:
foreach($_POST['name_for_the_items'] as $item)
{
//do something with $item
}
If your post keys have to be parsed and the keys are sequences with data, you can try this:
Post data example: Storeitem|14=data14
foreach($_POST as $key => $value){
$key=Filterdata($key); $value=Filterdata($value);
echo($key."=".$value."<br>");
}
then you can use strpos to isolate the end of the key separating the number from the key.
i wouldn't do it this way
I'd use name arrays in the form elements
so i'd get the layout
$_POST['field'][0]['name'] = 'value';
$_POST['field'][0]['price'] = 'value';
$_POST['field'][1]['name'] = 'value';
$_POST['field'][1]['price'] = 'value';
then you could do an array slice to get the amount you need