2015-11-14 56 views
-1

我有3個複選框,可以將它們提交到MySQL。它插入完美,但問題是,當我回到編輯遊戲時,複選框根本沒有被正確的值檢查(它們是空的)。 我知道這個網站上有很多關於這個問題的答案,但是我沒有發現它們可以工作。也許它是我的代碼做錯了。我會在這裏發佈。如何在提交表單後檢查複選框

這是我的代碼的簡短版本,因爲它太大了。

if (isset($_GET['add']) || isset($_GET['edit'])) { 

    $checkbox = ((isset($_POST['available_consoles']) && $_POST['available_consoles'] != '') ? sanitize($_POST['available_consoles']) : ''); 

if (isset($_GET['edit'])) { 
    $checkbox = ((isset($_POST['available_consoles']) && $_POST['available_consoles'] != '') ? $_POST['available_consoles'] : $game['available_consoles']); 
} 

if ($_POST) { 
    // Separate ech checkbox value with a SPACE into the database. 
    $checkbox = implode(', ', $_POST['available_consoles']); 

    // DO INSERT HERE 
} 

} 
<!-- Add the Add Form --> 
     <form action="games.php?<?php echo ((isset($_GET['edit'])) ? 'edit='.$edit_id : 'add=1'); ?>" method="post" enctype="multipart/form-data"> 

      <!-------------- AVAILABLE CONSOLES -------------------> 
      <div class="form-group col-md-6"> 
       <label for="checkbox">Available Consoles:&nbsp;</label> 
       <label class="checkbox"> 
        <input type="checkbox" <?php if (in_array('Xbox One', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="Xbox One"> 
       </label> 
       <label class="checkbox"> 
        <input type="checkbox" <?php if (in_array('PS4', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="PS4"> 
       </label> 
       <label class="checkbox"> 
        <input type="checkbox" <?php if (in_array('PC', $_POST['available_consoles'])) {echo 'checked';} ?> name="available_consoles[]" value="PC"> 
       </label> 
      </div> 
      <div class="form-group pull-right"> 
       <a href="games.php" class="btn btn-default">Cancel</a> 
       <input type="submit" value="<?php echo ((isset($_GET['edit'])) ? 'Edit ' : 'Add '); ?> Game" class="btn btn-success"> 
      </div> 

</form> 

獲取未來這些錯誤消息複選框:

Notice: Undefined index: available_consoles in C:\xampp\htdocs\Gamesite\admin\games.php on line 425 

Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\Gamesite\admin\games.php on line 425 
name="available_consoles[]" value="Xbox One"> 

獲得它爲所有3個複選框

+0

<?php if($ _ POST ['available_consoles'])echo'checked = checked'; ?> – Jah

+0

這是行不通的。 – David

+0

在xbox上試試這個:<?php if($ _ POST ['available_consoles'] ['0'])echo'checked = checked'; ?> – Jah

回答

0

這就是你需要

<input type="checkbox" <?php if (in_array('Xbox One', $_POST['available_consoles'])) {echo 'checked';} ?>id="checkbox" name="available_consoles[]" value="Xbox One"> 

取代in_array('Xbox One' 爲每個輸入

這裏的其他遊戲機的名字是簡單的功能:

function checked($t,$v){ 
    if (in_array("{$t}", $v)) { 
    return 'checked'; 
    } 
} 

使用:<?php echo checked('Xbox One',$con); ?>

它不是一個數組也不是從$ _POST它實際上來自數據庫,所以我追加了我的代碼來匹配答案。

+0

警告:in_array()期望參數2爲數組,null中給出的C:\ xampp \ htdocs \ Gamesite \ admin \ games.php行425 > – David

+0

嘗試使用@ $ POST,仍給出錯誤消息 – David

+0

立即嘗試對不起..我犯了小錯字 – Jah