473,385 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.

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, 175 views)
File Type: jpg db.jpg (18.9 KB, 151 views)
May 21 '10 #1
2 2083
dlite922
1,584 Expert 1GB
First of all, what's the point of $flag, why not just put "checked=\"checked\"" 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
@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
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...
4
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...
1
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...
10
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...
2
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...
2
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
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'...
1
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...
3
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.