简体   繁体   中英

Updating a html form with multiple checkboxs using php

I have a html form which i want to check some items in the form and then update the form. (actually there is no order in which checkbox is going to be mark, it's totally random) my problem is when i mark some of the checkboxes and press update my code will mark the top checkboxes in order. This is what i mean :

before submiting : 在此处输入图片说明

after submiting : 在此处输入图片说明

this is my form code :

$row_counter=0;                     

if ($_SESSION['user_row_num']=="1")
{
?>
<form method="POST" action="" name="frm1">
    <table class='styled-table' cellspacing='0' border='1'>
        <tr>
            <th  scope='col' style='font-size:13px;'>number</th>
            <th  scope='col' style='font-size:13px;'>view</th>
            <th  scope='col' style='font-size:13px;'>edit</th>
            <th  scope='col' style='font-size:13px;'></th>      
        </tr>               
<?php   while($row_form = mysqli_fetch_assoc($query_formsearch))
        { 
            //fetching from profile previous valuse of form access
            $query_profile_check = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION['user_id']}' ");
            $row_profile = mysqli_fetch_assoc($query_profile_check);

            //creating form name from forms table for profile table
            $profile_form_name="form_".$row_form['num'];
            $profile_frm_name=$row_profile[$profile_form_name];

            $a_form_aces=explode("-", $profile_frm_name);
            $frm_view=$a_form_aces[0];
            $frm_edit=$a_form_aces[1];?>    

        <tr>
            <td align='center'><input  class='styled-input' type='hidden' name='form_num[]' id='form_num' value="<?php echo $row_form['num']; ?>"/></td>
            <td align='center'><input  class='styled-input' type='text' name='form_name[]' id='form_name' value="<?php echo $row_form['name']; ?>"/></td>
            <td align='center'><input  class='styled-input' type='checkbox' name='view[]' id='view' <?php if($frm_view=="1")echo "checked"; ?> /></td>
            <td align='center'><input  class='styled-input' type='checkbox' name='edit[]' id='edit' <?php if($frm_edit=="1")echo "checked"; ?> /></td>
            <td align='center'><input  type='hidden' name='row_counter' id='row_counter' value="<?php echo $row_counter++; ?>"/></td>
        </tr>   
<?php   } ?>
    </table>

    <input  class='styled-input_2' style='padding: 5px; width: 140px;' type='submit' name='save_setting' id='save_setting' value="update" >
    <div class='cleaner h30'></div> 
    </form>

this is my update code :

<?php   
    } // end of if($_SESSION['user_row_num']=="1")                  



// Check if button name "edit-msb" is active, do this 
    if(isset($_POST['save_setting']) && $_POST['save_setting'] == 'update')
    {

            for($i=0;$i<=$_SESSION['user_count'];$i++)
            {
                $row_no = ($_REQUEST['row_counter'][$i]);
                $form_numb = $_REQUEST['form_num'][$row_no];

                if(isset($_REQUEST['view'][$row_no])){$_REQUEST['view'][$row_no]="1";}else{$_REQUEST['view'][$row_no]="0";}
                if(isset($_REQUEST['edit'][$row_no])){$_REQUEST['edit'][$row_no]="1";}else{$_REQUEST['edit'][$row_no]="0";}

                $form_access=$_REQUEST['view'][$row_no]. "-" .$_REQUEST['edit'][$row_no];
                $profile_form_num="form_". $form_numb;

                $access_query=mysqli_query($con,"UPDATE `profile` SET `{$profile_form_num}`='{$form_access}' WHERE `id`='{$_SESSION['user_id']}'");
            }

            if($access_query!='')
            {
                echo "<div class='cleaner h30'></div>";
                echo "<b style='color:green;margin-left:10px;font-size:15px;'>the form successfully updated.</b>";

            }




    }                   
?>      

I want each check box shows it's updated value after i submiting the form and in the order that i marking them but i don't know where is my mistake.

I've changed your code from checkbox into select option and it's working see if it'll work for you

form code :

<form method="POST" action="" name="frm1">
 <table class='styled-table' cellspacing='0' border='1'>
    <tr>
    <th  scope='col' style='font-size:13px;'>form number</th>
    <th  scope='col' style='font-size:13px;'>form name</th>
    <th  scope='col' style='font-size:13px;'>view</th>
    <th  scope='col' style='font-size:13px;'>edit</th>
    <th  scope='col' style='font-size:13px;'></th>      
    </tr>               
<?php   while($row_form = mysqli_fetch_assoc($query_formsearch))
        { 
            //fetching from profile previous valuse of form access
            $query_profile_check = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION['user_id']}' ");
            $row_profile = mysqli_fetch_assoc($query_profile_check);

            //creating form name from forms table for profile table
            $profile_form_name="form_".$row_form['num'];
            $profile_frm_name=$row_profile[$profile_form_name];

            $a_form_aces=explode("-", $profile_frm_name);
            $frm_view=$a_form_aces[0];
            $frm_edit=$a_form_aces[1];?>    

    <tr>
        <td align='center'><input  class='styled-input'  type='text' name='form_num[]' id='form_num' value="<?php echo $row_form['num']; ?>"/></td>
        <td align='center'><input  class='styled-input'  type='text' name='form_name[]' id='form_name' value="<?php echo $row_form['name']; ?>"/></td>
        <td align='center'>
            <select class='styled-input'  name='view[]' id='view'  />
                <option value="<?php echo $frm_view; ?>"><?php if($frm_view=="1"){echo "yes";}else{echo "no";} ?></option>
                <option></option>
                <option value="1" style="color:#1A75FF;">yes</option>
                <option value="0" style="color:red;">no</option>
            </select>
        </td>
        <td align='center'>
            <select class='styled-input' name='edit[]' id='edit' />
                <option value="<?php echo $frm_edit; ?>" ><?php if($frm_edit=="1"){echo "yes";}else{echo "no";} ?></option>
                <option></option>
                <option value="1" style="color:#1A75FF;">yes</option>
                <option value="0" style="color:red;">no</option>
            </select>
        </td>
        <td align='center'><input  type='hidden' name='row_counter' id='row_counter' value="<?php echo $row_counter++; ?>"/></td>
     </tr>   
<?php   } ?>
</table>

update code :

<?php   

// Check if button name "edit-msb" is active, do this 
    if(isset($_POST['save_setting']) && $_POST['save_setting'] == 'update')
    {


            for($i=0;$i<$_SESSION['user_count'];$i++)
            {
                //$row_no = ($_REQUEST['row_counter'][$i]);


                $form_access=$_POST['view'][$i]. "-" .$_POST['edit'][$i];
                echo $profile_form_num="form_". $_POST['form_num'][$i];echo"<br>";
                echo $form_access;echo"<br>";

                $access_query=mysqli_query($con,"UPDATE `profile` SET `{$profile_form_num}`='{$form_access}' WHERE `id`='{$_SESSION['user_id']}'");


            }

            if($access_query!='')
            {
                echo "<div class='cleaner h30'></div>";
                echo "<b style='color:green;margin-left:10px;font-size:15px;'>form successfully updated.</b>";
            }

    }                   
?>      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM