943,526 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 7374
  • PHP RSS
Nov 13th, 2007
0

storing the selected value of a drop-down list

Expand Post »
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
php Syntax (Toggle Plain Text)
  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
php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Nov 13th, 2007
0

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

The first problem I can see if the options have no values, so there for they can not post anything?
Quote ...
<option value="4">4</option>
Not sure if that will sort it but worth a try
ezb
Reputation Points: 10
Solved Threads: 3
Newbie Poster
ezb is offline Offline
23 posts
since Jul 2007
Nov 13th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by ezb ...
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.
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Nov 13th, 2007
0

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

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:
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Nov 13th, 2007
0

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

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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007
Nov 13th, 2007
0

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

Thanks for your response JeniF.

Managed to sort my problem out with the following code:
php Syntax (Toggle Plain Text)
  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. }
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Feb 22nd, 2009
0

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

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mad0 is offline Offline
1 posts
since Feb 2009
Feb 23rd, 2009
0

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

Click to Expand / Collapse  Quote originally posted by mad0 ...
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
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: MySQL - Some Records Not Deleting
Next Thread in PHP Forum Timeline: php httprequest class and session





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC