Viewed   205 times

I have a system where people fill in their information and later can go back and edit certain parts, basically the enter personal info and check whether they want to know extra info, these bits of extra infos are checkboxes, 4 of them. the user will select up to any of the 4 and the database has 4 fields set to no, if they select one it changes to yes. I want them to be able to go back and deselect or reselect any of these 4 checkboxes, so what i want s for the checkboxes to be selected if the values is yes and unselected if the value is not.

the fields are tag_1, tag_2, tag_3, tag_4

Anyhelp greatly appreciated

I gather some kind of if statement but not sure how to involve it in a checkbox.

<label for="tag_1">Tag 1</label>
<input type="checkbox" name="tag_1" id="tag_1" value="yes" />

Ian

 Answers

4

Extract the information from the database for the checkbox fields. Next change the above example line to: (this code assumes that you've retrieved the information for the user into an associative array called dbvalue and the DB field names match those on the HTML form)

<input type="checkbox" name="tag_1" id="tag_1" value="yes" <?php echo ($dbvalue['tag_1']==1 ? 'checked' : '');?>>

If you're looking for the code to do everything for you, you've come to the wrong place.

Wednesday, October 26, 2022
5

=>Try this Code I hope it's useful.

// html page..

<label>Select State</label><br>
<?php 
$allgroup = mysql_query("SELECT * FROM  state");
$flag=false;
while($state_list = mysql_fetch_array($allgroup))
{
   $parr=explode(',',$er['state_id']);
   $size = sizeof($parr);
   for($i=0;$i<$size;$i++) { 
     if($parr[$i]==$state_list['id']) {                              
        $flag=true;
     } 
   }    

  if($flag==true) {
    ?>
    <input  type='checkbox' name='state[]' style="margin-left:5px;" value="<?php echo $state_list['id']; ?>" checked  > <?php echo $state_list['name']; ?> <br>
    <?php
    $flag=false;
  } else { 
    ?>
    <input  type='checkbox' name='state[]' style="margin-left:5px;" value="<?php echo $state_list['id']; ?>"     > <?php echo $state_list['name']; ?> <br>
    <?php
    }
}
?>

// php code ..

<?php
$states="";
$i=0;
foreach( $_POST['state'] as $selected) {
   echo sizeof($_POST['state']);
   if($i==sizeof($_POST['state'])-1) {
       $states = $states.$selected;
   } else {
     $states = $states.$selected.",";
   }
   $i++;
}
?>
Monday, September 5, 2022
 
tlindig
 
1

You need to give the checkboxes an explicit index in the HTML:

$i = 0;
while($shift_query = $result_shift_query->fetch_assoc())
    {
    echo "<tr>";    
    echo "<td><input type="hidden" name="Shift_ID[]"></td>";
    echo "<td><input type="text" name="Shift_Name[]"></td>";
    echo "<td><input type="text" name="Shift_Short_Name[]"></td>";
    echo "<td><input type="text" name="Shift_Color[]"></td>";
    echo "<td><input type="checkbox" name="Shift_Trig[$i]"";
        if($shift_query['Shift_Trig'] == '1')
            {
            echo " checked="checked"";
            }
    echo " value="1"></td>";
    echo "<td><input type="checkbox" name="deleteBox[$i]"></td>";
    echo "</tr>";
    $i++;
    }

In the update code, at the beginning of the loop, do:

    if (!isset($trig[$i])) { $trig[$i] = 0; }

The rest of the update code is unchanged.

Saturday, November 5, 2022
3

use usort(), like:

function sortById($x, $y) {
    return $x['id'] - $y['id'];
}

usort($array, 'sortById');
echo "<pre>"; print_r($array);
Sunday, September 25, 2022
 
pavel_p
 
3

You can do it like this:

First in sql server:


SELECT 
    CAST(CASE PROCESSED WHEN 'Y' THEN 1 ELSE 0 END AS BIT) AS PROCESSED
    NAME
    DATE
FROM ExampleTable

in c# code:


SqlCommand cmd = new SqlCommand(sql query here); 
SqlDataAdapter da = new SqlDataAdapter(); 
DataTable dt = new DataTable(); 
da.SelectCommand = cmd; 

// Save results of select statement into a datatable 
da.Fill(dt);

gridview_all_applicants.DataSource = dt;          
gridview_all_applicants.DataBind(); 

and finally in aspx:

<asp:TemplateField HeaderText="Complete">
<ItemTemplate>
    <asp:CheckBox ID="process_flag" runat="server" Checked='<%# bool.Parse(Eval("PROCESSED").ToString()) %>' Enable='<%# !bool.Parse(Eval("PROCESSED").ToString()) %>'/>
</ItemTemplate>

Wednesday, November 9, 2022
 
bfuoco
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :