views:

49

answers:

2

Hello, I'm currently using checkboxes to set permissions within my PHP application. On the user management page, if the user has the permission, defined by 1 or 0 in the database, the box with be checked or unchecked respectively. Below is the code that is being used to check whether if the box should be checked or not:

<div><input type="checkbox" tabindex="1" name="permission[$app]" value="1" <? if ($currentperms['newapp'] = "1") {echo " checked ";}?> />New Application</label></div>

Interestingly, before this line, echo($currentperms['newapp']) gives 0, as per what's set in the database, however, after this line, echo($currentperms['newapp']) gives 1, indicating that the variable has changed.

What could the reason be for this?

I've tested the code, and it is these lines which are redefining the variables, and is preventing me from completing the script, as I can't test it's functionality.

I've checked that $currentperms['newapp'] = "0" :

Array ( [userid] => 1 [ptodo] => 1 [usercp] => 1 [pm] => 1 [bug] => 1 [abug] => 1 [admincp] => 1 [intmgs] => 1 [adduser] => 1 [pass] => 1 [useredit] => 1 [listuser] => 1 [newapp] => 0 )

Thanks :)

+3  A: 

I believe = is the assignment operator in php. You need to use ==, the equality operator.

Try changing the test from

<? if ($currentperms['newapp'] = "1") {echo " checked ";}?>

to

<? if ($currentperms['newapp'] == "1") {echo " checked ";}?>
Bryan Kyle
and that's the reason why. Thanks :)
Shamil
+1  A: 

A single = assigns the value, while a double == compares the value. You need to use ==.

Also ... I assume you are doing xhtml 1.0 strict since you've got a self closing input.

The proper xhtml markup is <input type="checkbox" checked="checked">. Your code will output <input type="checkbox" checked>.

rpflo
I was, and thanks. I'll makes the changes immediately :)
Shamil