2016-08-03 9 views
1

ここでの問題は、チェックボックスのステータスがすべて0の場合にのみチェックボックスが無効になることです。ただし、ここではチェックボックスを無効にしたいだけですが、無効にはなりません。ここ は私のコードです:だからチェックボックスを無効にすることができません

<?php 
    $username = $_SESSION['username']; 
    $query = "SELECT * FROM box WHERE status = 1"; 
    $result = @mysqli_query($con, $query); 
    $num_rows = @mysqli_num_rows($result); 
    $disable = ''; 
    if (!$num_rows) { 
     $disable = 'disabled="disabled"'; 
    }  
?> 

<form method = "post" action = "">  
    <input type='checkbox' name="boxs[]" id="1.1" value ="1.1" <?php echo $disable ?>/> 
    <label for="1.1" class="background1"></label> <br/> 
    <input type='checkbox' name="boxs[]" id="1.2" value ="1.2"<?php echo $disable ?>/> 
    <label for="1.2" class="background2"></label> 
    <br/> 
    <input type='checkbox' name="boxs[]" id="1.3" value ="1.3"<?php echo $disable ?>/> 
    <label for="1.3" class="background2"></label> 
    <input type="submit" name="Next" id="Next" value="next" /> 
</form> 
<?php  
    if(isset($_POST['Next'])) { 
     foreach($_POST['boxs'] as $f) { 
      $sql = "UPDATE box SET status = '0' WHERE boxid = '$f'"; 
      mysqli_query($con,$sql) or die(mysqli_error($con)); 

      $result = "INSERT INTO booked(username, boxid) VALUES('$username', '$f')"; 
      mysqli_query($con,$result) or die(mysqli_error($con)); 
     }  
    } 
?> 

、私のコードが間違っているでしょうか?

+0

です。コードをチェックしました。問題は見つけられません。 –

+0

チェックボックスを無効にする考えはありますか? – June

+0

あなたのコードで確認しました。ゼロの場合、すべてのチェックボックスは無効になります。他の数字が0になると、チェックボックスが有効になります。私は何かエラーを見つけることはありません。 –

答えて

1
<?php $username = $_SESSION['username'];?> 
<form method = "post" action = ""> 
<?php 
    $username = $_SESSION['username']; 
    $query = "SELECT * FROM box WHERE boxid=1.1 AND status = 1"; 
    $result = @mysqli_query($con, $query); 
    $num_rows = @mysqli_num_rows($result); 
    $disable = ''; 
    if (!$num_rows){ 
    $disable = 'disabled="disabled"'; 
    } 
?> 

<input type='checkbox' name="boxs[]" id="1.1" value ="1.1" <?php echo $disable ?>/> 
<label for="1.1" class="background1"></label> <br/> 
<?php 
    $query = "SELECT * FROM box WHERE boxid=1.2 AND status = 1"; 
    $result = @mysqli_query($con, $query); 
    $num_rows = @mysqli_num_rows($result); 
    $disable = ''; 
    if (!$num_rows){ 
    $disable = 'disabled="disabled"'; 
    } 
?> 

<input type='checkbox' name="boxs[]" id="1.2" value ="1.2"<?php echo $disable ?>/> 
<label for="1.2" class="background2"></label> 
<br/> 
<?php 
    $query = "SELECT * FROM box WHERE boxid=1.3 AND status = 1"; 
    $result = @mysqli_query($con, $query); 
    $num_rows = @mysqli_num_rows($result); 
    $disable = ''; 
    if (!$num_rows){ 
    $disable = 'disabled="disabled"'; 
    } 
?> 

<input type='checkbox' name="boxs[]" id="1.3" value ="1.3"<?php echo $disable ?>/> 
<label for="1.3" class="background2"></label> 
<input type="submit" name="Next" id="Next" value="next" /> 
</form> 
<?php 

if(isset($_POST['Next'])) 
{ 
foreach($_POST['boxs'] as $f){ 
$sql = "UPDATE box SET status = '0' WHERE boxid = '$f'"; 
mysqli_query($con,$sql) or die(mysqli_error($con)); 

$result = "INSERT INTO booked(username, boxid) VALUES('$username', '$f')"; 
mysqli_query($con,$result) or die(mysqli_error($con)); 
} 

} 
?> 
0

あなたの正確な要件はわかりませんが、ソリューションは

<?php $username = $_SESSION['username']; ?> 
<form method = "post" action = ""> 
<?php 
$username = $_SESSION['username']; 
$query = "SELECT * FROM box"; 
$result = @mysqli_query($con, $query); 
$i=1; 
while ($raw = $result->fetch_assoc()) { 

    if ($raw['status'] == 1) { 
     $disable = 'disabled="disabled"'; 
    } else { 
     $disable = ''; 
    } 
    echo "<input type='checkbox' name='boxs[]' id='" . $raw['boxid'] . "' value ='" . $raw['boxid'] . "' $disable/>"; 
    echo " <label for='" . $raw['boxid'] . "' class='background$i'></label> <br/>"; 
$i++; 
} 
?> 
</form> 
+0

私のチェックボックスはラベルを持っているので、それぞれ異なるクラスを持っています – June

+0

修正された答えを見てください。 – Chintan7027

+0

私は3列で私のチェックボックスを手配したいと思います。どうすればいいですか?列は次のように見える必要があります。1.1 2.1 3.1 – June

関連する問題