Jump to content

[Resolved] Trouble with a form


jbehen

Recommended Posts

First time here, and I am at my wits end. I have googled until my fingers bled, so I am hoping I can find help here.

I have a MySQL table called osi_menurights, consisting of:

user_name
gen
ind
cg
dcpv
dcpm
admin

With the exception of the username, the other fields are boolean flags, determining whether the user has rights to display that particular menu.

The database is querying correctly, as the following code:

[code]
<?php

require ('includes/db_vars.php');

$user = $_POST['user_name'];
echo "<h1>$user</h1>\n" .
"<FORM ACTION=\"test.php\" METHOD=\"POST\">\n";
$query = "SELECT gen, ind, cg, dcpv, dcpm, admin FROM osi_menurights WHERE user_name = '$user'";
$result = mysql_query($query);
$rowcount = 1;
while ($row = mysql_fetch_assoc($result)) {
foreach($row as $var => $val) {
echo "$var: $val<br/>\n";
}
++$rowcount;
}
?>
[/code]

results in the following:

[quote]
jbehen
gen: 1
ind: 0
cg: 1
dcpv: 0
dcpm: 0
admin: 1
[/quote]

My goal is to display the results as checkboxes, with the "CHECKED" status being determined by the flag. My code for this is:

[code]
<?php

require ('includes/db_vars.php');

$user = $_POST['user_name'];
echo "<h1>$user</h1>\n" .
"<FORM ACTION=\"test.php\" METHOD=\"POST\">\n";
$query = "SELECT gen, ind, cg, dcpv, dcpm, admin FROM osi_menurights WHERE user_name = '$user'";
$result = mysql_query($query);
$rowcount = 1;
while ($row = mysql_fetch_assoc($result)) {
foreach($row as $var => $val) {
echo "<input TYPE=CHECKBOX NAME=\"$var\"";
if ($val = '1') {
echo " CHECKED";
}
echo "/> $var<br/>\n";
}
++$rowcount;
}
?>
[/code]

It displays the checkboxes, followed the table field name. But for some reason, every checkbox is checked.

This is the HTML code generated (Don't know if I can build a form in a post, so displaying the HTML only):

[code]
<h1>jbehen</h1>
<FORM ACTION="test.php" METHOD="POST">
<input TYPE=CHECKBOX NAME="gen" CHECKED/> gen<br/>
<input TYPE=CHECKBOX NAME="ind" CHECKED/> ind<br/>
<input TYPE=CHECKBOX NAME="cg" CHECKED/> cg<br/>
<input TYPE=CHECKBOX NAME="dcpv" CHECKED/> dcpv<br/>
<input TYPE=CHECKBOX NAME="dcpm" CHECKED/> dcpm<br/>
<input TYPE=CHECKBOX NAME="admin" CHECKED/> admin<br/>

[/code]

Any ideas?
Link to comment
Share on other sites

Well, son of a ....

[code]
<h1>jbehen</h1>
<FORM ACTION="test.php" METHOD="POST">
<input TYPE=CHECKBOX NAME="gen" CHECKED/> gen<br/>
<input TYPE=CHECKBOX NAME="ind"/> ind<br/>
<input TYPE=CHECKBOX NAME="cg" CHECKED/> cg<br/>
<input TYPE=CHECKBOX NAME="dcpv"/> dcpv<br/>
<input TYPE=CHECKBOX NAME="dcpm"/> dcpm<br/>

<input TYPE=CHECKBOX NAME="admin" CHECKED/> admin<br/>

[/code]

It worked! Much thanks!!!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.