How to make sure at least one radio button is selected in each group, php -


so trying put voting system each entry of thread. each entry gets set of radio buttons (1, 2, 3) , submit button @ bottom. want people voting make sure select 1 of 3 radio buttons each entry. thought code working, it's not. last entry if selected, , others aren't, still says fine. if don't select last entry working.

<form action="vote.php" method="post" name="form1">  <? $sql = "select * contest_entries contest_id='$contest_id' order 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" /> 

so on vote.php page after hitting submit:

foreach($_post['contest_entry_id'] $key => $something) {   $example = $_post['attending'][$key];    }  if (!isset($example))  {      echo "you need vote entries";  exit();  }else{  echo "success!";  }   

it works except last entry, if last entry selected , others aren't still thinks entries have been selected

  1. you should either add hidden value same name before radio options or query db id's once again proper iteration through options.
  2. check if every group different 0/isset() inside foreach loop.

simple solution:

    ...     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">';     ... 

vote.php

    foreach ($_post['attending'] $id => $value) {           if ($value == 0) {             echo 'you need vote entries';              exit;          }       }       echo "success!";   

btw: not assign values variable (like $example) if you're expecting doesn't exist - check them directly isset($_post[...])


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -