Rahul Singh
Rahul Singh

Reputation: 1632

Issue with for loop of checkboxes

I have series of checkboxes whom value I am taking and generating a string like....if checkbox is selected I add '1' to string and if it is not selected I add '0' to string.

<input type="checkbox" name="auth_0" id="auth_0" class="checkboxes" value="Yes"/>

My php script is...

if (isset($_REQUEST["save"])) {
/*      echo $_REQUEST['auth_0'];*/
   for ($i = 0; $i <= 49; $i++) {
        if ($_REQUEST['auth_[$i]'] == 'Yes') {
            $auth_string .= '1';
        } else {
            $auth_string .= '0';
        }
    }
    echo $auth_string;
}

Though string is generating but its value is always 0 in both cases that if checkbox is selected or not.

Upvotes: 3

Views: 71

Answers (4)

Marc B
Marc B

Reputation: 360602

    if ($_REQUEST['auth_[$i]'] == 'Yes') {
    //            ^---------^--- should be " instead

using ' tells PHP to not parse/interpolate variable values inside the string. use " instead.

Upvotes: 4

Tudor Constantin
Tudor Constantin

Reputation: 26861

In HTML you have id="auth_0" and in PHP you check for auth_[$i] - see the square brackets. Also, you have apostrophes in $_REQUEST['auth_[$i]'] which does not interpolate $i's value - change that to double quotes

Upvotes: 1

Rukmi Patel
Rukmi Patel

Reputation: 2561

if ($_REQUEST['auth_'.[$i]] == 'Yes') {

this will work for you too.. above mentioned answers will also work .

Upvotes: 2

alex
alex

Reputation: 490173

You need to use double quotes for string interpolation to work. Also, you don't use brackets for array subscripting.

You probably want (judging by your HTML example)...

$_REQUEST["auth_$i"]

Upvotes: 2

Related Questions