0

そのため、スレッドのエントリごとに投票システムをまとめようとしています。各エントリには一連のラジオ ボタン (1、2、3) があり、下部に送信ボタンがあります。投票する人には、エントリごとに 3 つのラジオ ボタンのいずれかを必ず選択してもらいたいと思います。私のコードは機能していると思っていましたが、そうではありませんでした。最後のエントリが選択されていて、他のすべてが選択されていない場合、それでも問題ないと表示されます。しかし、最後のエントリを選択しないと機能します。

<form action="vote.php" method="POST" name="form1"> 
<? $sql = "SELECT * FROM contest_entries WHERE contest_id='$contest_id' ORDER BY id desc";  
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); 


while ($list = mysql_fetch_assoc($result)) { $username=$list['username']; 
$date=$list['date_entered']; 
$pl_holder=$list['place_holder1']; 
$contest_entry_id=$list['id']; 


echo "1<input name='attending[$contest_entry_id]' type='radio' value='1'>  
2<input name='attending[$contest_entry_id]' type='radio' value='2'> 
3 <input name='attending[$contest_entry_id]' type='radio' value='3'> />";  
}?> 

<input type="submit" name="submit2" id="submit" value="Submit" />

次に、送信を押した後のvote.phpページで:

foreach($_POST['contest_entry_id'] as $key => $something) {  
$example = $_POST['attending'][$key]; 


}  if (!isset($example))  { 
    echo "You need to vote for all entries"; 
exit(); 
}else{ 
echo "success!"; 
}  

最後のエントリが選択されていて、他のエントリが選択されていない場合でも、すべてのエントリが選択されていると見なされる場合、最後のエントリを除いて機能します

4

2 に答える 2

1
  1. ラジオ オプションの前に同じ名前の非表示の値を追加するか、すべてのオプションを適切に反復処理するためにもう一度 id の db をクエリする必要があります。
  2. foreach ループ内ですべてのグループが 0/isset() と異なるかどうかを確認します。

簡単な解決策:

    ...
    echo '<input type="hidden" name="' . attending[$contest_entry_id] . '" value="0">
    1<input type="radio" name="' . attending[$contest_entry_id] . '" value="1">  
    2<input type="radio" name="' . attending[$contest_entry_id] . '" value="2"> 
    3<input type="radio" name="' . attending[$contest_entry_id] . '" value="3">';
    ...

投票.php

    foreach ($_POST['attending'] as $id => $value) {  
        if ($value == 0) {
            echo 'You need to vote for all entries'; 
            exit; 
        }  
    }  
    echo "success!";  

ところで:値が存在しないと予想される場合は、変数($exampleなど)に値を割り当てないでください- isset($_POST[...]) で直接確認してください

于 2013-05-19T19:42:56.750 に答える
0
foreach($_POST['contest_entry_id'] as $key => $something) {  
        $example = $_POST['attending'][$key];

これはどのように機能しますか?あなたのラジオグループには名前があります-定義されていませんattending[contest-id]-そう$_POST['contenst_entry_id']ですか?

if/else 条件はforeach-loop ブラケット内にある必要があります。

それ以外は何も言えません-エラーを投稿するか、反復する前に を印刷して、またはのglobal $_POST中身を確認してください。var_dump()print_r()

于 2013-05-19T17:56:01.540 に答える