Reputation: 663
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.
Upvotes: 0
Views: 591
Reputation: 6632
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.
Upvotes: 0
Reputation: 360762
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;
Upvotes: 5