mardi 12 janvier 2016

Error in php checkbox when page is updated

I need help with this problem: Wen i update the page and if page has one or more checked checkboxes the checkboxes is doubled or tripled with same value.

I want the checkbox to stay checked or not.

here is 2 files:

file.php

<?php

elseif ($action == 'edit_category') {
    $category_id = (int) $_GET['categ'];
    if (isset($_POST['submit_edit_category'])) {
    $edit_category_query = mysql_query("UPDATE categories SET name = '". $_POST['name'] ."', description = '". $_POST['description'] ."' WHERE catid = '". $category_id ."'");


// categories sidebar   
$delete_category_query = mysql_query("delete FROM map_category WHERE catid2 = '". $category_id ."'");

if (empty($_POST['check_list'])) {echo '';}
if (is_array($_POST['check_list']))
{
foreach ($_POST['check_list'] as $selected) {         
$insert_category_query = mysql_query("INSERT INTO map_category(catid1, catid2) VALUES ('". $category_id ."','". $selected ."')");
}
}

// categories footer    
$delete_category_query = mysql_query("delete FROM map_category_footer WHERE catid2 = '". $category_id ."'");

if (empty($_POST['check_list_footer'])) {echo '';}
if (is_array($_POST['check_list_footer']))
{
foreach ($_POST['check_list_footer'] as $selected) {          
$insert_category_query = mysql_query("INSERT INTO map_category_footer(catid1, catid2) VALUES ('". $category_id ."','". $selected ."')");
}
}

    redirect_page('/admin/file.php?select=edit_category&categ='. $category_id);
    }

    $category_query = mysql_query("SELECT * FROM categories WHERE catid = '". $category_id ."' LIMIT 1");   
    $category_row = mysql_fetch_assoc($category_query);
    $edit_category = array (
        'name'          =>  $category_row['name'],  
        'description'   =>  $category_row['description']
    );

// select categories sidebar
    $top_category_query = mysql_query("SELECT c.catid, c.name, (CASE WHEN mc.catid1 IS NULL THEN 0 ELSE 1 END) AS is_selected
                                    FROM categories c
                                    LEFT JOIN map_category mc 
                                        ON c.catid = mc.catid2
                                        and mc.catid1 = '". $category_id ."' 
                                    WHERE c.catid <> '". $category_id ."' 
                                    ORDER BY c.name ASC "); 

    while($top_category_row = mysql_fetch_assoc($top_category_query))
        {
            $top_edit_category[] = $top_category_row;
        }   


// select categories footer     
    $top_category_footer_query = mysql_query("SELECT c.catid, c.name, (CASE WHEN mc.catid1 IS NULL THEN 0 ELSE 1 END) AS is_selected
                                    FROM categories c
                                    LEFT JOIN map_category_footer mc 
                                        ON c.catid = mc.catid2
                                        and mc.catid1 = '". $category_id ."' 
                                    WHERE c.catid <> '". $category_id ."' 
                                    ORDER BY c.name ASC "); 

    while($top_category_footer_row = mysql_fetch_assoc($top_category_footer_query))
        {
            $top_edit_category_footer[] = $top_category_footer_row;
        }       

edit_category_admin();
}

?>

and template.php

<?php

function edit_category_admin()
    {global $edit_category,$top_edit_category,$top_edit_category_footer;
    main_header();
    main_menu();
    echo'<div id="center"><div id="admin"><h3>Edit category</h3>';
    echo '<form action="" method="POST" name="form" onsubmit="return verify_add()">
          <div class="left">Name</div>
          <div class="right"><input type="text" name="name" size="55" value="'. $edit_category['name'] .'" /></div>
          <div class="clear"></div>   
          <div class="left">Description</div>
          <div class="right"><textarea name="description" rows="5" cols="95">'. $edit_category['description'] .'</textarea></div>
          <div class="clear"></div>
          <div class="left">Sidebar Category:</div>       
          <div class="right"><div id="cat" style="background-color:#fcfebc;width: 100%px;height: 400px;overflow: scroll;">';
foreach ($top_edit_category as $top_cat)
{         
echo '<label><input type="checkbox" name="check_list[]" value="'. $top_cat['catid'] .'"  '. ($top_cat['is_selected'] == '1' ? 'checked' : '') .' /> '. $top_cat['name'] .'</label>';
}   

echo '</div></div>                    
          <div class="clear"></div>
          <div class="left">Footer Category:</div>        
          <div class="right"><div id="cat" style="background-color:#DAF7A6;width: 100%px;height: 200px;overflow: scroll;">';          
foreach ($top_edit_category_footer as $top_cat)
{
echo '<label><input type="checkbox" name="check_list_footer[]" value="'. $top_cat['catid'] .'"  '. ($top_cat['is_selected'] == '1' ? 'checked' : '') .' /> '. $top_cat['name'] .'</label>';
}

echo '</div></div>                    
          <div class="clear"></div>
           <div align="center">
            <input type="submit" name="submit_edit_category" value="Edit category" />
          </div><br />
         </form>'; 
    echo '</div></div>
    <div class="clear"></div>'; 
    main_footer();
    }

?>  

Many thanks!




Aucun commentaire:

Enregistrer un commentaire