Radio Button Single Result to Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WOWDesign
    New Member
    • May 2010
    • 2

    Radio Button Single Result to Database

    Basically I want a client to tick a radio button, which will then return the value of 1 to the database field PROMO. It's so that they can promote a course on the home page when ticked, out of all courses available.

    The code below shows the PHP gets the course category title then populates a table with the related courses, then gets the next category and so on. There is one database table for all the courses, which has a PROMO field set by default to zero.

    Code:
    <?php while ($EmptyPrint = mysql_fetch_array($EmptyQuery)){
    $CatQuery = mysql_query ("SELECT CSCatCode, CSCatTitle FROM CoursesCat WHERE CSCatCode=".$EmptyPrint['CSCatCode']);
    $CatPrint = mysql_fetch_array($CatQuery);?>
    <h3><?php echo $CatPrint['CSCatTitle'] ?> Courses</h3>
    <table id="TBAmends">
    <tr class="Headers">
    <th class="Head1">Course &ndash; Date</th>
    <th class="Head2">Promote</th>
    </tr>
    <?php $TBQuery = mysql_query ("SELECT CSCode, CSTitle, date_format(CSDate, '%d %M %Y') AS RealDate, CSOrder, Promo FROM Courses WHERE CSCatCode = ".$CatPrint['CSCatCode']." ORDER BY CSDate");
    while ($TBPrint = mysql_fetch_array($TBQuery)) { ?>
    <tr>
    <td class="cmsname"><?php echo $TBPrint['CSTitle']; ?> &ndash; <?php echo $TBPrint['RealDate']; ?></td>
    <td class="cmsedit"><input type="hidden" name="CSCode" value="<? echo $TBPrint['CSCode']; ?>" /><input type="radio" <?php $flag = false;
    if($TBPrint['Promo'] == 1){
    $flag = true;} 
    if($flag) echo "checked=\"checked\""; ?>
    name="Promo" id="Promo" value="1" /></td>
    </tr>
    <?php } ?>
    </table>
    <?php } ?>
    The issue I'm having is the very last radio button is always passed to the database and not the one activated. Is there a way of registering which button has been ticked ONLY? Can this be done with PHP or will it need to be Javascript using onclick?

    I'd like to use radio buttons, as the client MUST ONLY tick one box. I tried using name="CSCode[i]" and name="Promo[i]" giving each field a unique number, but then it was letting me tick multiple radio buttons as the names were all different i.e. name="Promo0(1) ", name="Promo1(1) ", name="Promo2(1) " etc. The name of the radio button must be the same in order for only one selection to be used.

    Any help would be most grateful.
    Cheers
    Glynn
    Attached Files
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    First of all, what's the point of $flag, why not just put "checked=\"chec ked\"" echo
    in the if statement if you're not reusing this $flag elsewhere.

    If They're all Promo (including the last one), than you're going to get a variable called $_POST['Promo'] with a value of one.

    The value must be a unique value to that course. (maybe the CSCode?). Set the promo column to 1 if the $_POST['Promo'] variable contains the CSCode of that row.

    You're issue is not with radio buttons, but with the design and logic of your code.



    Dan

    Comment

    • WOWDesign
      New Member
      • May 2010
      • 2

      #3
      Originally posted by dlite922
      First of all, what's the point of $flag, why not just put "checked=\"chec ked\"" echo
      in the if statement if you're not reusing this $flag elsewhere.

      If They're all Promo (including the last one), than you're going to get a variable called $_POST['Promo'] with a value of one.

      The value must be a unique value to that course. (maybe the CSCode?). Set the promo column to 1 if the $_POST['Promo'] variable contains the CSCode of that row.

      You're issue is not with radio buttons, but with the design and logic of your code.



      Dan
      Hi Dan

      Thanks for the reply.

      I'm learning PHP as I go and haven't been taught, all I know is from books and what I've got people to do for me, hence using the flag statement.

      When the client comes onto the page there will be one already ticked from their previous choice, ergo the flag. I've read some posts on radio buttons and a lot of people seem to think you should have one selected by default.

      I did have the Promo value set to the CSCode, but my question is: how do I know when that button has been selected? If I set the Promo to the CSCode what do I need to write in order to check which one has been ticked and pass the value of 1 to the database for that Course?

      Something like:

      if ($Promo = $CSCode){
      $Promo == 1;
      } else {
      $Promo ==0;}

      That's where I'm falling down. I know the issue is with my logic, hence the question posted on here.

      After the submit button is pressed I re-set the previous promo option to 0 and then just want to post the one selected result to the database.

      Cheers
      Glynn

      Comment

      Working...