I want to concatenate a variable within the $_POST[] brackets for a while loop. My question is, is it possible. Here is the code.
$m=0;
$_POST['tier_' . $m];
Yes. However your form should probably be more like:
<input type="text" name="tier[]" />
Then $_POST['tier'] will be an array you can loop through.
It is possible, yes, but the code sample you provided would cause a notice because it would try to check an index that does not yet exist. The following would work:
$m=0;
$_POST['tier_' . $m] = 'something';
Related
Look I have a form like this:
<form method="post">
<input name="variable[var1][var2]" value="44"/>
</form>
I want to get the value of variable[var1][var2] in PHP using a string like:
$str = "['variable']['var1']['var2']";
echo "{$_POST{$str}}";
Why I need to do that?
I need it because the code that gets the value is totally dynamic and I cannot get or set manually the value using $_POST['variable']['var1']['var2'].
Can you help?
NOTE: I know this is possible to get it using $_POST['variable']['var1']['var2'], please don't ask me about why I'm not using this way. Simply I need to use as above (using $str).
You can use preg_match_all() to get all the indexes in the string into an array:
$indexes = preg_match_all('/(?<=\[\')(?:[^\']*)(?=\'\])/', $str);
$indexes = $indexes[0]; // Get just the whole matches
Then use a loop to drill into $_POST.
$cur = $_POST;
foreach ($indexes as $index) {
$cur = $cur[$index];
}
At this point $cur will contain the value you want.
You're WAY overthinking it. Anything you put in [] array notation in a form field's name will just become an array key. The following is LITERALLY all you need:
<input name="foo[bar][baz][qux]" ... />
$val = $_POST['foo']['bar']['baz']['qux'];
You cannot use a string as an "address" into the array, not without insanely ugly hacks like eval, or parsing the string and doing a loop to dig into the array.
It's hard to believe that this is a requirement. If you could expand more on what you're trying to achieve, someone undoubtedly has a better solution. However, I will ignore the eval = evil haters.
To echo:
eval("echo \$_POST$str;");
To assign to a variable:
eval("\$result = \$_POST$str;");
If you're open to another syntax then check How to write getter/setter to access multi-level array by key names?
for($i=0;$i<=9;$i++){
$try[$i] = $_POST[''echo $i;'.AA1.'];
}
But i am getting an error like unexpected echo when i try to use a variable in post methods braces.
Please give me a more optimum solution if you guys find out.
Use this:
for($i=0;$i<=9;$i++){
$try[$i] = $_POST[$i.'AA1'];
}
Make sure that the following indexes exist in your POST aray: 0AA1 -> 9AA1.
The error you are getting is caused by several issues:
using echo inside []. (You don't need to echo a variable's value to use it.)
wrong concatenation
How I can add a variable inside other variable definition?
Here is an example: $cars_list_ VARIABLE HERE [$car]
${'cars_list_'.$your_variable}[$var]
But it is extremely ugly. Use an array instead of multiple variables:
$vars_list[$your_variable][$car]
Obviously you need to change your existing code for that to work. But it's worth the time/effort.
While you could use ${'cars_list_'.$somevar}['car'], it is an extremely bad idea. Instead, you should have a multi-dimensional array that you can access with $cars_list[$somevar]['car']
You can do:
${'cars_list_' . 'other_var'} = "stuff";
echo $car_list_other_var;
Returns "stuff"
i have a web called Infomundo and under the site i have a problem with php:
$c=1;
while($c!=17)
{ $fecha_semana$c=$_POST['fecha_semana$c'];
$interes_semana$c=$_POST['interes_semana$c'];
$capital_semana$c=$_POST['capital_semana$c'];
$recargos_semana$c=$_POST['recargos_semana$c'];
$iva_semana$c=$_POST['iva_semana$c'];
$pagado_semana$c=$_POST['pagado_semana$c'];
$c=$c+1;
}
but the variables $fecha_semana$c, $interes_semana$c, etc. are wrong how can i fix it?
You're using single quotes in the array dereference:
$_POST['fecha_semana$c'];
That will not evaluate the value of $c; use double quotes:
$_POST["fecha_semana$c"];
See also: string
Additionally, you need to use variable variables for the left hand of the assignment:
${"fecha_semana$c"} = $_POST["fecha_semana$c"];
Update
This problem would be easier if you'd use array syntax in your form fields:
<input name="fecha_semana[]" value="123" />
<input name="fecha_semana[]" value="456" />
<input name="fecha_semana[]" value="678" />
When that gets posted, you will have an array in PHP:
print_r($_POST['fecha_semana']);
// ["123", "456", "678"]
As an alternative option to Jack's solution, you can use concatention:
$_POST['fecha_semana'.$c];
I personally prefer concatenation as it is easier for me to see where variables are being used, but I would say it is largely a matter of preference.
i'm not sure how to explain it correctly, but i would like to use $_POST values like this $_POST[0] and not like this $_POST['form_field_name'].
Let's say i have 3 form fields so if i'd like to get data from post like this:
echo $_POST[0];
echo $_POST[1];
echo $_POST[2];
I hope you guys understand what i wanna do here.
Try it like this:
$values = array_values($_POST);
No idea why you would do such a thing though.
I would not recommend ever to refer to your $_POST values with indexes, as it is generally a bad idea.
You can access them by indexes if you do this:
$items = array_values($_POST);
$foo = $items[0];
$bar = $items[1]
You can also run through your values with a foreach loop, like this (which is better, but still bad!)
foreach($_POST as $item)
{
// do your thing here
}
$_POSTs variables depends on the name attribute of the form element as you can read in this link.
On the other hand the attribute name of the form elements according to W3C always must begin with a letter.
But I think you can prepare the $_POST variable before all your code (at the begining of your php script) with:
$arrPostVariables = array_values($_POST);
And then call them in the way as you want, but I think you will previously must detect the order of the array in order to avoid errors by not having the text of each variable.
This should work:
<input name="0" value="val0" />
<input name="1" value="val1" />