| | |
storing the selected value of a drop-down list
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
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
And this is the php that updates the DB
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.
My code is as follows:
The drop-down list
php Syntax (Toggle Plain Text)
<?php // open connection to MySQL server $connection = mysql_connect('localhost', 'username', 'password') or die ('Unable to connect!'); //select database mysql_select_db('database') or die ('Unable to select database!'); //create and execute query $query = "SELECT day(farmFullStartDate) FROM rates"; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); //create selection list echo "<select name='startDay' class='eventDay' id='startDay'>\n"; while($row = mysql_fetch_row($result)) { echo "<option selected='selected'>$row[0]</option>"; } echo "<option>01</option>"; echo "<option>02</option>"; echo "<option>03</option>"; echo "<option>04</option>"; echo "<option>05</option>"; echo "<option>06</option>"; echo "<option>07</option>"; echo "<option>08</option>"; echo "<option>09</option>"; echo "<option>10</option>"; echo "<option>11</option>"; echo "<option>12</option>"; echo "<option>13</option>"; echo "<option>14</option>"; echo "<option>15</option>"; echo "<option>16</option>"; echo "<option>17</option>"; echo "<option>18</option>"; echo "<option>19</option>"; echo "<option>20</option>"; echo "<option>21</option>"; echo "<option>22</option>"; echo "<option>23</option>"; echo "<option>24</option>"; echo "<option>25</option>"; echo "<option>26</option>"; echo "<option>27</option>"; echo "<option>28</option>"; echo "<option>29</option>"; echo "<option>30</option>"; echo "<option>31</option>"; echo "</select>"; ?>
And this is the php that updates the DB
php Syntax (Toggle Plain Text)
// open connection to MySQL server $connection = mysql_connect('localhost', 'username', 'password') or die ('Unable to connect!'); //select database mysql_select_db('database') or die ('Unable to select database!'); //define variables $startDay = $_POST['startDay']; $startMonth = $_POST['startMonth']; $startYear = $_POST['startYear']; $startDate = $startYear."".$startMonth."".$startDay; $endDay = $_POST['endDay']; $endMonth = $_POST['endMonth']; $endYear = $_POST['endYear']; $endDate = $endYear."".$endMonth."".$endDay; $luxury = $_POST['luxury']; $orchid = $_POST['orchid']; //create query $query = "UPDATE rates SET farmFullStartDate = '$startDate', farmFullEndDate = '$endDate', farmFullLuxury = '$luxury', farmFullOrchid = '$orchid'"; //$query = "INSERT INTO rates (farmFullStartDate, farmFullEndDate, farmFullLuxury, farmFullOrchid) VALUES ('$startDate', '$endDate', '$luxury', '$orchid')"; //execute query $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // close connection 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.
•
•
•
•
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
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.
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:
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.
php Syntax (Toggle Plain Text)
if (farmHouse.startDay.value = $row[0]) { $startDay = $row[0]; } else { $startDay = $_POST['startDay']; }
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.
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
Then all I do is place the value in the insert statement
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']} -
{$row['City']} -
{$row['Name']}
</option>\n";
} else {
$pulldown1 .= "<option value=\"{$row['FacilitiesID']}\">
{$row['Street1']} -
{$row['City']} -
{$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!!!!
:}
:}
Thanks for your response JeniF.
Managed to sort my problem out with the following code:
Managed to sort my problem out with the following code:
php Syntax (Toggle Plain Text)
if ($_GET['startDay'] = $row[0]) { $day = $_GET['startDay']; if ($day < 10) { $startDay = "0".$day; } else { $startDay = $_GET['startDay']; } } else { $startDay = $_POST['startDay']; }
•
•
Join Date: Feb 2009
Posts: 1
Reputation:
Solved Threads: 0
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!
•
•
•
•
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!
This user has a spatula. We don't know why, but we are afraid.
![]() |
Similar Threads
- Populate One Drop Down List From Another (ASP.NET)
- drop down list selected index change doesn't work in mozilla (ASP.NET)
- drop down list question. (ASP.NET)
- Populating a Drop-down List (PHP)
- view employee info for the employee selected from the drop down list (PHP)
- Data Binding to a Drop Down List? (ASP.NET)
- Passing a drop down list item's value (HTML and CSS)
- Blank drop list (PHP)
Other Threads in the PHP Forum
- Previous Thread: MySQL - Some Records Not Deleting
- Next Thread: php httprequest class and session
Views: 3290 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dropdown dynamic echo email error file files folder form forms function functions google href htaccess html httppost image include insert integration ip java javascript joomla jquery limit link login loop mail md5 menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search secure select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video votedown web xml youtube





