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
https://forums.phpfreaks.com/topic/30136-resolved-trouble-with-a-form/
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!!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.