2

I have a simple php script that handles form data. In the form I have several checkboxes, and when those are checked, the form contains "on" as a value.

For the remainder of the script, I need to have the string "1" instead of "on", but I can't change it for some reason.

Here is the script:

$posted=$_POST;
foreach ($posted as $key=>$val) {
    if ($val == "on") {
        $posted[$key] = "1";
    }
}
// The $_POST parameters
echo 'The $_POST parameters:';
echo '<pre>';
var_dump($_POST);
echo '</pre>';

// The $posted parameters after conversion
echo 'The $posted parameters (after \'conversion\'):';
echo '<pre>';
var_dump($posted);
echo '</pre>';

And the output:

The $_POST parameters:
array(3) {
  ["id"]=>
  string(6) "142892"
  ["help"]=>
  string(2) "on"
  ["vm"]=>
  string(2) "on"
}

The $posted parameters (after 'conversion'):

array(3) {
  ["id"]=>
  string(6) "142892"
  ["help"]=>
  string(2) "on"
  ["vm"]=>
  string(2) "on"
}
18
  • 1
    You cannot modify $_POST variable. Commented Jan 10, 2014 at 11:02
  • Yes, I know. I do not try that. see script.
    – Kolja
    Commented Jan 10, 2014 at 11:02
  • but he is already modifying $posted variable not the $_POST variable itself.
    – rccoros
    Commented Jan 10, 2014 at 11:02
  • 3
    @MurtazaHussain I beg to differ, given that I use $_POST = json_decode(file_get_contents("php://input"),true); all the time. Commented Jan 10, 2014 at 11:03
  • 3
    Your problem can't be reproduced. It works fine.
    – Alma Do
    Commented Jan 10, 2014 at 11:12

4 Answers 4

3

First the code you supplied does what you expect and modifies the 'posted' array correctly (PHP 5.3.18).

According to the PHP 'foreach' documentation, to modify the 'value' in the loop you need to use a 'reference' to the 'value' as follows:

As you are only interested in modifying all the 'on' values in the posted' array then the following code does that and is clear as to what is being changed.

$posted=$_POST;
foreach ($posted as &$val) { // note the 'reference' on $val
    if ($val == "on") {
        $val = "1";
    }
}

This does what you want without any extra variables.

0

Here it is:

 $posted = array();
    $posted=$_POST;
    foreach ($posted as $key=>$val) {
        if ($val == "on") {
            $_POST[$key] = "1";
        }
    }
    var_dump($_POST);
0

You should use another array when you use a foreach loop

$posted = array();
$postArray = $_POST;
foreach ($postArray as $key=>$val) {
    if ($val == "on") {
        $posted[$key] = "1";
    }
    else {
        $posted[$key] = $val;
    }
}
var_dump($posted);

An array used in a foreach loop is not editable and it doesn't work.

-1

It should be:

$posted = array();
$posted=$_POST;
foreach ($posted as $key=>$val) {
    if ($val == "on") {
        $posted[$key] = "1";
    }
}
1
  • It is the same code that is used by koljanep in his question.
    – Afroz Alam
    Commented Jan 10, 2014 at 11:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.