473,659 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Radio Button Single Result to Database

2 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. <?php while ($EmptyPrint = mysql_fetch_array($EmptyQuery)){
  2. $CatQuery = mysql_query ("SELECT CSCatCode, CSCatTitle FROM CoursesCat WHERE CSCatCode=".$EmptyPrint['CSCatCode']);
  3. $CatPrint = mysql_fetch_array($CatQuery);?>
  4. <h3><?php echo $CatPrint['CSCatTitle'] ?> Courses</h3>
  5. <table id="TBAmends">
  6. <tr class="Headers">
  7. <th class="Head1">Course &ndash; Date</th>
  8. <th class="Head2">Promote</th>
  9. </tr>
  10. <?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");
  11. while ($TBPrint = mysql_fetch_array($TBQuery)) { ?>
  12. <tr>
  13. <td class="cmsname"><?php echo $TBPrint['CSTitle']; ?> &ndash; <?php echo $TBPrint['RealDate']; ?></td>
  14. <td class="cmsedit"><input type="hidden" name="CSCode" value="<? echo $TBPrint['CSCode']; ?>" /><input type="radio" <?php $flag = false;
  15. if($TBPrint['Promo'] == 1){
  16. $flag = true;} 
  17. if($flag) echo "checked=\"checked\""; ?>
  18. name="Promo" id="Promo" value="1" /></td>
  19. </tr>
  20. <?php } ?>
  21. </table>
  22. <?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 Images
File Type: jpg cms.jpg (20.3 KB, 176 views)
File Type: jpg db.jpg (18.9 KB, 152 views)
May 21 '10 #1
2 2102
dlite922
1,584 Recognized Expert Top Contributor
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
May 21 '10 #2
WOWDesign
2 New Member
@dlite922
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
May 22 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3242
by: Jay | last post by:
I have a form used to submit data (no surprises there!). I'd like to be able to populate the same form with previously submitted data. The data lives in a database once submitted and using ASP I can populate text boxes/textareas using this data no problem. What about radio buttons? If I have 2 of them named the same rb1 and rb1 with values of yes/no respectively.
4
9890
by: Hazzard | last post by:
What is the best way to do this? Binary with 0 representing off and 1 on? Int16 with 1 representing first button, 2 the second, 3 ... varchar with a character values at certain positions in the string representing radio button values. Thanks, -hazz
1
6876
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and grandtotal the costs ready for the selected items to be inserted back to the database. I did something like this before with Checkboxes, but Radio button have to be named the same to maintain their groupings.
10
6074
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio button with this link of code: echo 'SCRIPT language=JavaScript setCheckedValue("'.$_SESSION.'");</SCRIPT>'; //? <snip of code>
2
12425
by: Eric Layman | last post by:
Hi, I have a radio button and a combo box. Upon changing a value for radio button, the combo box values will be dynamically updated. I have an idea on how to go abt doing it but Im stuck into converting into code.
2
1764
by: kiju8 | last post by:
what are the ways of inserting radio button into the database, is there a way to put it in textual form just like making the path to the images.
2
1179
by: sejal17 | last post by:
hello I want to select radio button with the database value. i have three radio button: <input type='radio' name='method' value='Paypal' checked='check'>Paypal <input type='radio' name='method' value='Creditcard'>Credit Card <input type='radio' name='method' value='Paypalmobile'>Paypal Mobile i have inserted that vaue in database as it is selected or checked.noe when i edit my form i want to that the radio button is selected...
1
7494
by: castron | last post by:
Hi all, I'm reading user information from Active Directory. I'm allowing the users to search by username from a web form. There will be times when the result will be more than one record. What I'm trying to do is to generate radio buttons at run-time, so users can select the user account they are looking for, and then populate the information into a form. My question is how do I create a single event handler for more than one radio...
3
3654
by: kiranbabu | last post by:
<html> <head> <h2 align=center>Blank Tapes Status Form</h2> <br><title>Blank tapes status</title> </head> <body> <br><br> <form name=tapes_status method=post>
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.