I have a foreach loop with a form inside for each result like so:
foreach($this->results() as $that) {
<form>
<input type="text" name="name[]">
<input type="text" name="this[]">
</form>
}
and so on. My question is how do I each forms data. I understand you can do something like the following:
$_POST['name'][0];
$_POST['name'][1];
etc, but is their a way to get this done without knowng how many forms their will be. I mean like foreach loop the $_POST data and get each form?
Many thanks
foreach ($_POST['name'] as $val) { /* do what you want, want you want with my value */ }
$_POST['name'] is just an array. Use count or any array function you want on it then.
You can do it like this:
foreach ($_POST['name'] as $val => $value) {}
assuming these rows are being generated by a while loop, and the variable is named like this
$val = $row['val'];
And then in your form you'd have something like this:
echo '<input type="hidden" value="'.$val.'" name ="val[]" /><input type="text" name="name['.$val.'] />";
basically the name variable would be identified by the value itself being generated but also appended on a named variable, and then can be fed into your foreach.
foreach ($_POST['name'] as $key => $value) {
echo 'key: '.$key.' Value : '.$value;
}
--------------------------
output key: 0 Value : nameValue1
key: 1 Value : nameValue2
$_POST['name'] is an array, if you get multiple value with key, just get with foreach value
Related
in my $_POST, I have a variable whose name changes. the name is modify_0, but the number at the end changes depending on the button that was pressed.
Is there anyway to check what the number is for that variable in $_POST?
Say:
$_POST['modify_(check for number or any character)']
You would need to iterate over all of the keys within the $_POST variable and take a look at their format:
$post_keys = array_keys( $_POST );
foreach($post_keys as $key){
if ( strpos($key, 'modify_' ) != -1 ){
// here you know that $key contains the word modify
}
}
Besides the correct answers given above, I would recommend changing your code slightly so it's easier to work with.
Instead of having inputs with the format:
// works for all types of input
<input type="..." name="modify_1" />
<input type="..." name="modify_2" />
You should try:
<input type="..." name="modify[1]" />
<input type="..." name="modify[2]" />
This way, you can iterate through your data in the following way:
$modify = $_POST['modify'];
foreach ($modify as $key => $value) {
echo $key . " => " . $value . PHP_EOL;
}
This works especially well for multiselects and checkboxes.
Try something like this:
// First we need to see if there are any keys with names that start with
// "modify_". Note: if you have multiple values called "modify_X", this
// will take out the last one.
foreach ($_POST as $key => $value) {
if (substr($key, 0) == 'modify_') {
$action = $key;
}
}
// Proceed to do whatever you need with $action.
I post a form with the same input names+id at the end like that:
<input type="text" name="machine_1">
<input type="text" name="machine_11">
<input type="text" name="machine_23">
How loop through on each of them and get the id's in the loop?
I tried this way, but it will loop a lot without any data and what happens if there is more thank 100 id?
for($i=0; $i<100; $i++){
$_POST["machine"]=$_POST["machine_".$i];
$id=$i;
}
POST is an associate array, so you can loop through everything that's been posted like this:
//$k contains the id
//$v contains the submitted value
foreach($_POST as $k => $v) {
//test if id contains 'machine'
if(stristr($k, 'machine')) {
echo $v;
}
}
You can do this using a foreach loop, like so:
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value';
}
$_POST["machine"]=$_POST["machine_".$i];
here $_POST['machine'] represents noting..how can you assign a value to $_POST['machine']..you havent passed any element having name machine while submitting the form..so first of all you need to check it bro
In your code, you have:
$_POST["machine"]=$_POST["machine_".$i];
That's not the correct way. You want to store the value of $_POST["machine_".$i] to $id and then use it below.
This is probably what you need:
for($i=1; $i<100; $i++){
$id = $_POST["machine_".$i];
echo $id;
}
And if there are more than 100 elements, and you don't know the number of inputs, then you can use a foreach loop, as below:
$i = 1; // counter variable
foreach ($_POST as $key => $input) {
if($key == "machine_".$i) {
$id = $input; // or simply 'echo $input'
echo $id;
}
$i++; // increment counter
}
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.
My code is:
HTML part for POST:
<form action='key.php' method='POST'>
<input type='number' name='consumervar[]' value='512'/>
<input type='number' name='consumervar[]' value='256'/>
<input type='number' name='consumervar[]' value='1024'/>
<input type='submit'/>
</form>
PHP Code for key.php:
<?PHP
foreach ($_POST as $key => $value) {
$consumervar = $value*64;
}
print_r($consumervar); // this is for for debug (see array contents)
?>
BUt when i run everything it reproduces:
Fatal error: Unsupported operand types in /var/blahblah/blahblah/key.php on line 3
Please help. How to do it correctly? It need to multiply every posted value with integer 64.
the loop should be
foreach($_POST['consumervar'] as $key => $value) {
^^^^^^^^^^^^^^^
as written, your code pulls out the ARRAY of consumervar values, which you try to multiply. You cannot "multiply" arrays in php.
As well, note that the $key/$value produced by the loop are simply copies of what exists in the array. You are not changing the array's values. For that, you should be doing
$_POST['consumervar'][$key] = $value * 64;
Try this:
<?php
$arr = isset($_POST['consumervar']) ? $_POST['consumervar'] : array();
if(!is_array($arr)) die('some_error');
foreach($arr as $key => $value) {
$arr[$key] = $value*64;
}
print_r($arr); // this is for for debug (see array contents)
?>
The $key corresponds to the "name" in the input element in HTML.
<input name="auth[]" type="text" id=" " value="<?php echo $cite_aut[$i]; ?>">
Above are text fields in html. Now how to get text fields values in a array variable in another php page.
I tried it by using foreach loop (foreach($val as $_POST['author'])... but it fetch 1 extra value for $val, if there are 5 text boxes then it is fetching 6 values, 6th value is "array".
can someone explain how to do it?
Should be:
foreach($_POST['auth'] as $key => $val) {
......
}
TRY
foreach($_POST['auth'] as $key => $val) {
echo $val;
}