Jump to content

Archived

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

Donovan

Checkbox ismember validation

Recommended Posts

I have a page that makes edits to values in a table. One of the values is an ismember flag which is a smallint(1). I use the ismember field for access to private pages.

I click on the record to edit and all my fields are popuated in the form including the value of the ismember checkbox. If the value is 1, then the checkbox is checked, else if it is 0 it is not checked.

When I make my edits to the fields and submit the value ismember is reset to 0 and I don't understand why.

My coding is pretty basic as I am learning PHP as a hobby.

Here is my script. Any help is appreciated.



[code]<?php
if ($op == "SaveSoldier") {
   
    $id = intval($_POST['id']);
    $rank_id = intval($rank_id);
    $unit_id = intval($unit_id);
    $weapon_id = intval($weapon_id);
    $subunit_id = intval($subunit_id);
    $adminunits;
    $num_of_adminunits = sizeof($_POST[admin_unit_id]);
    $k = 0;
    for ($i=0; $i < $num_of_adminunits; $i++) {
        $ifDuplicated = false;
        $test_for_duplication = explode(",",$adminunits);
        if ($_POST[admin_unit_id][$i]) {
            for ($j=0; $j < sizeof($test_for_duplication); $j++) {
                if ($_POST[admin_unit_id][$i] == $test_for_duplication[$j])
                    $ifDuplicated = true;
            }
            if (!$ifDuplicated) {
                if ($k > 0)
                    $adminunits .= ",";
                $adminunits .= $_POST[admin_unit_id][$i];
                $k++;
            }
        }
    }
//        Order in increasing numerical order
    $admin_array = explode(",",$adminunits);
    sort($admin_array);
    $adminunits = "";
    for ($i=0; $i <= sizeof($admin_array); $i++) {
        if ($admin_array[$i] != "")
            $adminunits .= $admin_array[$i] . ",";
    }
   
    $sql = "UPDATE " . $prefix . "_milpacs_members set
        uniform = '$uniform',
        rank_id = '$rank_id',
        ismember = '$ismember',
        flag = '$flag',
        u_name = '$soldierName',
        nukeusername = '$nukeusername',
        location = '$location',
        status = '$status',
        p_mos = '$p_mos',
        unit_id = '$unit_id',
        subunit_id = '$subunit_id',
        adminunits = '$adminunits',
        reports = '$reports',
        position = '$position',
        weapon_id = '$weapon_id',
        enlistment_dt = '$enlistment_dt',
        promotion_dt = '$promotion_dt',
        icq = '$icq',
        email = '$email',
        bio = '$bio'
        WHERE uniqueid ='$id'";
    $update = $db->sql_query($sql);
}

    // If not saving, load values from database
if ($op != "SaveSoldier") {
    $id = intval($_GET['id']);
    $result = $db->sql_query("SELECT * FROM " . $prefix . "_milpacs_members mm
    INNER JOIN (" . $prefix . "_milpacs_units mu) ON (mm.unit_id = mu.unit_id)
    LEFT JOIN " . $prefix . "_milpacs_weapons mw ON (mw.weapon_id = mm.weapon_id)
    WHERE mm.uniqueid ='$id'");
    $info = $db->sql_fetchrow($result);       
    if (!$result) {
        echo("<p>Error performing query: " . mysql_error() . "</p>");
        exit();
    } else {
        $soldierName = $info['u_name'];
        $nukeusername = $info['nukeusername'];
        $ismember = $info['ismember'];
        $uniform = $info['uniform'];
        $rank_id = $info['rank_id'];
        $flag = $info['flag'];
        $location = $info['location'];
        $status = $info['status'];
        $p_mos = $info['p_mos'];
        $unit_id = $info['unit_id'];
        $subunit_id = $info['subunit_id'];
        $adminunits = $info['adminunits'];
        $reports = $info['reports'];
        $position = $info['position'];
        $weapon_id = $info['weapon_id'];
        $qual = $info['qual'];
        $enlistment_dt = $info['enlistment_dt'];
        $promotion_dt = $info['promotion_dt'];
        $icq = $info['icq'];
        $email = $info['email'];
        $bio = stripslashes($info['bio']);
    }
}

OpenTable();
echo "<p><a href=\"admin.php?op=milpacs\">Return to Main Administration</a></p>";
echo " <form name=\"editsoldier\" action=\"".$admin_file.".php\" method=\"post\">";
?>
<table width="100%" border="2" cellpadding="2" align="center" cellspacing="0" style="border-collapse: collapse;" bordercolor="#111111">
    <tr>
        <td align="center" bgcolor= "<?php echo $bgcolor2 ?>"><b><font class="title">Personnel File of <?php echo $soldierName ?></font></b></td>
    </tr>   
    <tr>
        <td align="center"> Uniform image: <?php echo $uniformselecthtml ?></td>
    </tr>
    <tr>
        <td align="center"> Rank image: <?php echo $rankselecthtml ?></td>
    </tr>
    <tr>
        <td align="center"> Flag image: <?php echo $flagselecthtml ?></td>
    </tr>
    <tr>   
    <td align="center">Is Member: <input type="checkbox" name="ismember" <?php if($ismember == "1"){echo " CHECKED";}?>>     
    </tr>
</table>

etc
etc [/code]

I can set ismember to 1 via phpmyadmin. Enter the editsoldier page and see that the checkbox is checked which is what I want, however when I submit it sets it to 0. It erases the value. So in short, the editsoldier page is not properly posting the value of the checkbox to the ismember field in the table.

I have tried to use this but it didn't work.

$ismember = $_POST['ismember'];

Share this post


Link to post
Share on other sites
i dont know but i think you need to set the field of ismember to be increment in the database.

Share this post


Link to post
Share on other sites
Hi I think this is the problem
change:
[code]  Is Member: <input type="checkbox" name="ismember" <?php if($ismember == "1"){echo " CHECKED";}?>>     
    </tr>[/code]
to:
[code] Is Member: <input type="checkbox" name="ismember" value=" <?php if($ismember == "1"){echo "1";}
else{echo "0";}?> ">     
    </tr> [/code]
the way it currently is although it is checked ismember has no value assigned to it and so is reverted to default value of 0.

Share this post


Link to post
Share on other sites
<td align="center">Is Member: <input type="checkbox" name="ismember" <?php if($ismember == "1"){echo " [b]value="1"[/b] CHECKED";}?>>

that should fix it.

Share this post


Link to post
Share on other sites
No.  This still doesn't work.  I have to go cut my lawn.  I'll post all my code here for anybody who has any ideas why this wont work.  Basically if the value ismember has a 1, then dispay the checkbox as "checked".  Or rather display the value regardless of what it has...a "0" or a "1".

Be able to change the value of the checkbox and submit back to the table.



[code]
<?php
if ($op == "SaveSoldier") {
// Validations go here
// If all validations passed, save and exit, otherwise, redisplay with errors
$id = intval($_POST['id']);
$ismember = $_POST['ismember'];
$rank_id = intval($rank_id);
$unit_id = intval($unit_id);
$weapon_id = intval($weapon_id);
$subunit_id = intval($subunit_id);
$adminunits;
$num_of_adminunits = sizeof($_POST[admin_unit_id]);
$k = 0;
for ($i=0; $i < $num_of_adminunits; $i++) {
$ifDuplicated = false;
$test_for_duplication = explode(",",$adminunits);
if ($_POST[admin_unit_id][$i]) {
for ($j=0; $j < sizeof($test_for_duplication); $j++) {
if ($_POST[admin_unit_id][$i] == $test_for_duplication[$j])
$ifDuplicated = true;
}
if (!$ifDuplicated) {
if ($k > 0)
$adminunits .= ",";
$adminunits .= $_POST[admin_unit_id][$i];
$k++;
}
}
}
// Order in increasing numerical order
$admin_array = explode(",",$adminunits);
sort($admin_array);
$adminunits = "";
for ($i=0; $i <= sizeof($admin_array); $i++) {
if ($admin_array[$i] != "")
$adminunits .= $admin_array[$i] . ",";
}
// $email = addslashes($email);
$bio = addslashes($bio);
$sql = "UPDATE " . $prefix . "_milpacs_members set
uniform = '$uniform',
rank_id = '$rank_id',
ismember = '$ismember',
flag = '$flag',
u_name = '$soldierName',
nukeusername = '$nukeusername',
location = '$location',
status = '$status',
p_mos = '$p_mos',
unit_id = '$unit_id',
subunit_id = '$subunit_id',
adminunits = '$adminunits',
reports = '$reports',
position = '$position',
weapon_id = '$weapon_id',
enlistment_dt = '$enlistment_dt',
promotion_dt = '$promotion_dt',
icq = '$icq',
email = '$email',
bio = '$bio'
WHERE uniqueid ='$id'";
$update = $db->sql_query($sql);
}

// If not saving, load values from database
if ($op != "SaveSoldier") {
$id = intval($_GET['id']);
$result = $db->sql_query("SELECT * FROM " . $prefix . "_milpacs_members mm
INNER JOIN (" . $prefix . "_milpacs_units mu) ON (mm.unit_id = mu.unit_id)
LEFT JOIN " . $prefix . "_milpacs_weapons mw ON (mw.weapon_id = mm.weapon_id)
WHERE mm.uniqueid ='$id'");
$info = $db->sql_fetchrow($result);
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
} else {
$soldierName = $info['u_name'];
$nukeusername = $info['nukeusername'];
$ismember = $info['ismember'];
$uniform = $info['uniform'];
$rank_id = $info['rank_id'];
$flag = $info['flag'];
$location = $info['location'];
$status = $info['status'];
$p_mos = $info['p_mos'];
$unit_id = $info['unit_id'];
$subunit_id = $info['subunit_id'];
$adminunits = $info['adminunits'];
$reports = $info['reports'];
$position = $info['position'];
$weapon_id = $info['weapon_id'];
$qual = $info['qual'];
$enlistment_dt = $info['enlistment_dt'];
$promotion_dt = $info['promotion_dt'];
$icq = $info['icq'];
$email = $info['email'];
$bio = stripslashes($info['bio']);
}
}

OpenTable();
echo "<p><a href=\"admin.php?op=milpacs\">Return to Main Administration</a></p>";
echo " <form name=\"editsoldier\" action=\"".$admin_file.".php\" method=\"post\">";
?>
<table width="100%" border="2" cellpadding="2" align="center" cellspacing="0" style="border-collapse: collapse;" bordercolor="#111111">
<tr>
<td align="center" bgcolor= "<?php echo $bgcolor2 ?>"><b><font class="title">Personnel File of <?php echo $soldierName ?></font></b></td>
</tr>
<tr>
<td align="center"> Uniform image: <?php echo $uniformselecthtml ?></td>
</tr>
<tr>
<td align="center"> Rank image: <?php echo $rankselecthtml ?></td>
</tr>
<tr>
<td align="center"> Flag image: <?php echo $flagselecthtml ?></td>
</tr>
<tr>
<td align="center">Is Member: <input type="checkbox" name="ismember" value=" <?php if($ismember == "1"){echo "1";} else{echo "0";}?> "> 
</tr>
</table>
<table width="100%" border="2" cellpadding="2" align="center" cellspacing="0" style="border-collapse: collapse;" bordercolor="#111111">
<tr>
<td height="25" colspan="2" align="left" bgcolor= <?php echo $bgcolor1 ?>><b><font class="title">Vital Statistics</font></b></td>
</tr>
<tr>
<td align="center">
<table width="100%" border="1" cellpadding="2" cellspacing="1" style="border-collapse: collapse;" id="AutoNumber1" bordercolor="#111111">
</td>
<tr>
<td width="50%" align="right"><b><font class="content">Name:</font></b></td>
<td width="50%"><input type="text" name="soldierName" value="<?php echo $soldierName ?>"/></td>
</tr>
<tr>
          <td width="50%" align="right"><b><font class="content">(For Drill Access) Nuke Name:</font></b></td>
  <td width="50%"><input type="text" name="nukeusername" value="<?php echo $nukeusername ?>"/></td>
    </tr>
<tr>
<td width="50%" align="right"><b><font class="content">Location:</font></b></td>
<td width="50%"><input type="text" name="location" value="<?php echo $location ?>"/></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">Status:  </font></b></td>
<td width="50%"><?php echo $statusselecthtml ?></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content"><b>Primary MOS:</b></font></td>
<td width="50%"><input type="text" name="p_mos" value="<?php echo $p_mos ?>"/></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">Combat Unit:</font></b></td>
<td width="50%"><?php echo $unitselecthtml ?></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">Combat Subunit:</font></b></td>
<td width="50%"><?php echo $subunitselecthtml ?></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">Admin Units:</font></b></td>
<td width="50%"><?php echo $adminunitselecthtml ?></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">Reports to:</font></b></td>
<td width="50%"><input type="text" name="reports" value="<?php echo $reports ?>"/></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">Position:</font></b></td>
<td width="50%"><input type="text" name="position" value="<?php echo $position ?>"/></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content"><b>Weapon:</b></font></td>
<td width="50%"><?php echo $weaponselecthtml ?></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content"><b>Qualifications:</b></font></td>
<td width="50%"><input type="text" name="qual" value="<?php echo $qual ?>"/></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">Enlisted Date:</font></b></td>
<td width="50%"><input type="text" name="enlistment_dt" value="<?php echo $enlistment_dt ?>"/>
<a href="javascript:showCal('Enlistment');"><img src="modules/MILPACS/images/icon_calendar.gif" title="Select Date" alt="Select Date"></a>
</td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">Promoted Date:</font></b></td>
<td width="50%"><input type="text" name="promotion_dt" value="<?php echo $promotion_dt ?>"/>
<a href="javascript:showCal('Promoted');"><img src="modules/MILPACS/images/icon_calendar.gif" title="Select Date" alt="Select Date"></a>
</td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">ICQ#:  </font></b></td>
<td width="50%"><input type="text" name="icq" value="<?php echo $icq ?>"/></td>
</tr>
<tr>
<td width="50%" align="right"><b><font class="content">E-mail:</font></b></td>
<td width="50%"><input type="text" name="email" size="35" maxlength="35" value="<?php echo $email ?>"/></td>
</tr>
</table>
</table>
<table width="100%" border="2" cellpadding="2" align="center" cellspacing="0" style="border-collapse: collapse;" bordercolor="#111111">
<tr>
<td align="left" bgcolor= "<?php echo $bgcolor1 ?>"><b><font class="title">Personal Bio</font></b></td>
</tr>
<tr>
<td align="left"><textarea name="bio" cols="80" colspan="1" rows="8"><?php echo $bio ?></textarea></td>
</tr>
</table>
<br>
<input type="hidden" name="op" value="SaveSoldier"/>
<input type="hidden" name="profilename" value="<?php echo $soldierName ?>"/>
<input type="hidden" name="nukename" value="<?php echo $nukeusername ?>"/>
<input type="hidden" name="id" value="<?php echo $id ?>"/>
<input type="submit" class="button" align="center" value="Update"/>
</form>
?>
[/code]

Funny after months of learning php and coding pages of this PHPNuke module I have been working on for months I get hung up on a simple checkbox. 

I set up a demo account for people to checkout the progress.

http://milpacs.3rd-infantry-division.net/admin.php

Login: Admin
Password: demo   

Login and go to MILPACS, then choose Roster Manager and try to update a main info record. Such as

http://milpacs.3rd-infantry-division.net/admin.php?op=EditSoldier&id=1

You will see the checkbox with the issue at hand.

Share this post


Link to post
Share on other sites
try this it just may work:
change:
[code]name="ismember" value=" <?php if($ismember == "1"){echo "1";} else{echo "0";}?> [/code]

to:
[code]name="ismember" value=" <?php if($ismember == "1"){echo "on";} else{echo "off";}?> [/code]

change:
[code]$ismember = $_POST['ismember'];[/code]

to:
[code]if($_POST['ismember']=="on"){
$ismember=1;}
else{$ismember=0;}[/code]

Share this post


Link to post
Share on other sites
[code]<input type="checkbox" name="ismember"<?php echo ($ismember == 1) ? " checked=\"checked\"" : "";?>>[/code]

Share this post


Link to post
Share on other sites
At the bottom of my script I added

[code]<input type="hidden" name="ismember" value="<?php echo $ismember ?>"/>[/code]

Now when I update any other fields for a record where the value is "1" in the form, it will remain checked and save the value of "1" to the table.  (Before it was setting the field to "0" regardless)

If I access a record where the checkbox is unchecked, value "0",  I still cannot add the check and save that to the db table.

My code:


[code]if ($op == "SaveSoldier") {
// Validations go here
// If all validations passed, save and exit, otherwise, redisplay with errors
$id = intval($_POST['id']);
$ismember = $_POST['ismember'];
$rank_id = intval($rank_id);
$unit_id = intval($unit_id);
etc[/code]

and further down

[code]$sql = "UPDATE " . $prefix . "_milpacs_members set
uniform = '$uniform',
rank_id = '$rank_id',
ismember = '$ismember',
etc
etc
WHERE uniqueid ='$id'";
$update = $db->sql_query($sql);[/code]

and finally


[code]<tr>
<td align="center">Is Member: <input type="checkbox" name="ismember"<?php echo ($ismember == 1) ? " checked=\"checked\"" : "";?>>  
</tr> [/code]

Share this post


Link to post
Share on other sites

×

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.