PHP get unknown $var from url - php

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.

Related

How to Get data From url value parameter $_GET in PHP

sorry i a begginer of php. I have url example here: http://example.com?category=software-hardware .Actual of category is Software & Hardware I using this script to get url above
foreach ( $key as $value ) {
$c = array(' ', '&-');
$d = array('-', '');
echo "".$value->kategori_laporan."";
}
How To get data from my condition url.? Sorry, i bad using english. I want to create url like category from slideshare. Please help me.! Thanks.!
you can fetch it like:
$cat = $_GET['category'];
and you don't need any foreach
$_GET is an associative array having all the query string keys values.
You can get value of any query string parameter from this array by passing the key of query string parameter.
To get category you can use $_GET["category"] in your case.

POST with unique name as a variable

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'

Use POST variables without calling them specifically

All,
I have a form that has some text inputs, checkboxes, select box and a textarea. I want to submit the form with PHP using a submit button to a page for processing.
That part is fine but what I would like to do is basically get the results into an array so I can do a foreach loop to process the results.
Basically I'm trying to create a dynamic form the submits to my backend processing script and don't want to hard code in the post values like the following:
$var1 = $_POST['var1'];
echo $var1;
$var2 = $_POST['var2'];
echo $var2;
Does anyone know how to go about doing something like this or give any recommendations?
If there're no other data in your POST but these generated elements, just do
foreach( $_POST as $key => $val ) {
// do your job
}
and process what you have. If you want to mix your generated entries with predefined you may want to put these generated in nested array:
<input ... name="generated[fieldname]" />
and then you iterate
foreach( $_POST['generated'] as $key => $val ) {
// do your job
}
Just use array notation:
<input name="vars[]" value="" />
Then you will have something like this as $_POST:
Array ('vars' => Array(
0 => 'val1'
1 => 'val2'
)
)
foreach ($_POST as $param_name => $param_val) {
echo "Param: $param_name; Value: $param_val";
}
Actually, the $_POST variable is an array. you just need to extract the array values by using a simple foreach loop. that's it.
I hope this example help you.
foreach($_POST as $field_name => $val)
{
$asig = "\$".$field_name."='".addslashes($val)."';";
eval($asig);
}
After running this script all the POST values will be put in a variable with the same name than the field's name.

Foreach value from POST from form

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

Get post values when the key is unknown in CodeIgniter

CodeIgniter allows access to POSTed data via:
$this->input->post('input_name');
where 'input_name' is the name of a form field. This works well for a static form where each input name in known ahead of time.
In my case, I am loading a collection of key/value pairs from the database. The form contains a text input for each key/value pair.
I am wondering, is there a way to get an array of posted data via the CodeIgniter api?
Thanks!
According to the documentation, no. I would suggest just using array_keys($_POST) to get the keys.
foreach($this->input->post() as $key => $val) { echo "<p>Key: ".$key. " Value:" . $val . "</p>\n"; }
that can be used to
Surely if you have an array of keys from the database you can use that, like :
foreach ($arrayFromDb as $key => $value) {
$newValue = $this->input->post($key);
}
Then you have the advantage that people if people submit additional fields (e.g. by modifying the form and posting it themselves) those fields will be ignored
$array_db_columns = $this->db->query('SHOW COLUMNS FROM ci_props');
$array_db_columns = $array_db_columns->result_array();
$array_save_values = array();
foreach ( $array_db_columns as $value )
{
$array_save_values[$value['Field']] = $this->input->post($value['Field']);
}
insert :
$this->db->insert('props', $array_save_values);
update :
$this->db->where('id',$id); $this->db->update('props',$array_save_values);

Categories