storing the selected value of a drop-down list

Thread Solved

Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

storing the selected value of a drop-down list

 
0
  #1
Nov 13th, 2007
What I'd like to do is store the "selected" value of the drop down box in a DB. At the moment the selected value for the drop down list is pulled from the DB. Now what I want to happen is if I don't want to change that specific option from the drop-down list then the variable gets put back into the DB. At the moment if I don't select anything then my the value that is put back into the DB is 0.

My code is as follows:

The drop-down list
  1. <?php
  2. // open connection to MySQL server
  3. $connection = mysql_connect('localhost', 'username', 'password')
  4. or die ('Unable to connect!');
  5.  
  6. //select database
  7. mysql_select_db('database') or die ('Unable to select database!');
  8.  
  9. //create and execute query
  10. $query = "SELECT day(farmFullStartDate) FROM rates";
  11. $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
  12.  
  13. //create selection list
  14. echo "<select name='startDay' class='eventDay' id='startDay'>\n";
  15. while($row = mysql_fetch_row($result))
  16. {
  17. echo "<option selected='selected'>$row[0]</option>";
  18. }
  19. echo "<option>01</option>";
  20. echo "<option>02</option>";
  21. echo "<option>03</option>";
  22. echo "<option>04</option>";
  23. echo "<option>05</option>";
  24. echo "<option>06</option>";
  25. echo "<option>07</option>";
  26. echo "<option>08</option>";
  27. echo "<option>09</option>";
  28. echo "<option>10</option>";
  29. echo "<option>11</option>";
  30. echo "<option>12</option>";
  31. echo "<option>13</option>";
  32. echo "<option>14</option>";
  33. echo "<option>15</option>";
  34. echo "<option>16</option>";
  35. echo "<option>17</option>";
  36. echo "<option>18</option>";
  37. echo "<option>19</option>";
  38. echo "<option>20</option>";
  39. echo "<option>21</option>";
  40. echo "<option>22</option>";
  41. echo "<option>23</option>";
  42. echo "<option>24</option>";
  43. echo "<option>25</option>";
  44. echo "<option>26</option>";
  45. echo "<option>27</option>";
  46. echo "<option>28</option>";
  47. echo "<option>29</option>";
  48. echo "<option>30</option>";
  49. echo "<option>31</option>";
  50. echo "</select>";
  51. ?>

And this is the php that updates the DB
  1. // open connection to MySQL server
  2. $connection = mysql_connect('localhost', 'username', 'password')
  3. or die ('Unable to connect!');
  4.  
  5. //select database
  6. mysql_select_db('database') or die ('Unable to select database!');
  7.  
  8. //define variables
  9. $startDay = $_POST['startDay'];
  10. $startMonth = $_POST['startMonth'];
  11. $startYear = $_POST['startYear'];
  12. $startDate = $startYear."".$startMonth."".$startDay;
  13. $endDay = $_POST['endDay'];
  14. $endMonth = $_POST['endMonth'];
  15. $endYear = $_POST['endYear'];
  16. $endDate = $endYear."".$endMonth."".$endDay;
  17. $luxury = $_POST['luxury'];
  18. $orchid = $_POST['orchid'];
  19.  
  20. //create query
  21. $query = "UPDATE rates SET farmFullStartDate = '$startDate', farmFullEndDate = '$endDate', farmFullLuxury = '$luxury', farmFullOrchid = '$orchid'";
  22. //$query = "INSERT INTO rates (farmFullStartDate, farmFullEndDate, farmFullLuxury, farmFullOrchid) VALUES ('$startDate', '$endDate', '$luxury', '$orchid')";
  23.  
  24. //execute query
  25. $result = mysql_query($query)
  26. or die ("Error in query: $query. " . mysql_error());
  27.  
  28. // close connection
  29. mysql_close($connection);

Any help would be greatly appreciated.

P.S. If I haven't explained myself well enough then here's an example situation:

I select 05 from startDay and hit submit. The page refreshes and 5 is displayed in the startDay drop-down list. Now I don't want to change startDay but I want to change other fields in the form so I do so and hit submit. What I have changed gets sent to the DB and 0 gets sent to the DB for startDay. What should be happening is it should be sending 5 to the DB.
Last edited by Venom Rush; Nov 13th, 2007 at 4:55 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 23
Reputation: ezb is an unknown quantity at this point 
Solved Threads: 3
ezb ezb is offline Offline
Newbie Poster

Re: storing the selected value of a drop-down list

 
0
  #2
Nov 13th, 2007
The first problem I can see if the options have no values, so there for they can not post anything?
<option value="4">4</option>
Not sure if that will sort it but worth a try
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: storing the selected value of a drop-down list

 
0
  #3
Nov 13th, 2007
Originally Posted by ezb View Post
The first problem I can see if the options have no values, so there for they can not post anything?

Not sure if that will sort it but worth a try
The values get posted without the value specified. So if I select <option>04</option> then it gets posted to the database.

My problem is that <option selected='selected'>$row[0]</option> isn't being posted to the DB. And adding a value to it doesn't help. 0 still gets processed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: storing the selected value of a drop-down list

 
0
  #4
Nov 13th, 2007
I've come up with an idea. What I could do is use an if statement in the code that posts the variales to the DB. Something along the lines of the following:
  1. if (farmHouse.startDay.value = $row[0]) {
  2. $startDay = $row[0];
  3. }
  4. else {
  5. $startDay = $_POST['startDay'];
  6. }

I know this won't work. I have no idea how to point to the form using php as I'm somewhat new to it.

Could anyone help me with this please.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Re: storing the selected value of a drop-down list

 
0
  #5
Nov 13th, 2007
I have built a dynamic select box for something like this.
Here I populate the select options with information from a table:
(I also created the "sticky" to hold the users input upon error checking

<?php
$result = mysql_query("SELECT * FROM facilities ORDER BY Street1 ASC") or
die(mysql_error());
$sticky= '';
if (isset($_POST['facility']))
$sticky = ($_POST['facility']);
$pulldown1 = '<select name="facility">';
$pulldown1 .= '<option></option>';
while($row = mysql_fetch_array($result))
{
if($row['FacilitiesID'] == $sticky) {
$pulldown1 .= "<option selected value=\"{$row['FacilitiesID']}\">
{$row['Street1']}&nbsp;-&nbsp;
{$row['City']}&nbsp;-&nbsp;
{$row['Name']}
</option>\n";
} else {
$pulldown1 .= "<option value=\"{$row['FacilitiesID']}\">
{$row['Street1']}&nbsp;-&nbsp;
{$row['City']}&nbsp;-&nbsp;
{$row['Name']}
</option>\n";
}
}
$pulldown1 .= '</select>';
echo $pulldown1;
?>

Then all I do is place the value in the insert statement
I keep hitting "escape", but I'm still here!!!!
:}
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: storing the selected value of a drop-down list

 
0
  #6
Nov 13th, 2007
Thanks for your response JeniF.

Managed to sort my problem out with the following code:
  1. if ($_GET['startDay'] = $row[0]) {
  2. $day = $_GET['startDay'];
  3. if ($day < 10) {
  4. $startDay = "0".$day;
  5. } else {
  6. $startDay = $_GET['startDay'];
  7. }
  8. } else {
  9. $startDay = $_POST['startDay'];
  10. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: mad0 is an unknown quantity at this point 
Solved Threads: 0
mad0 mad0 is offline Offline
Newbie Poster

Re: storing the selected value of a drop-down list

 
0
  #7
Feb 22nd, 2009
i know this thread if quite old, but i just wanted to say thanks for lingering web pages, i almost tore my hair out trying to figure this out and literally just as I was about to call it a day, found quite the easy solution, so thanks JeniF and Venom Rush and Daniweb. i created an account just for this today. THANKS!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: storing the selected value of a drop-down list

 
0
  #8
Feb 23rd, 2009
Originally Posted by mad0 View Post
i know this thread if quite old, but i just wanted to say thanks for lingering web pages, i almost tore my hair out trying to figure this out and literally just as I was about to call it a day, found quite the easy solution, so thanks JeniF and Venom Rush and Daniweb. i created an account just for this today. THANKS!
Welcome to the community and glad this thread helped someone other than myself
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC