Results 1 to 5 of 5

Thread: getting selected value of a drop down from db

  1. #1
    Join Date
    Sep 2009
    Posts
    48
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default getting selected value of a drop down from db

    i would like my user to be able to edit a form

    how do i get the selected value of a drop down from db

    PHP Code:

    <select name='room'>

     <
    option value=''>-</option>
      <
    option value='Room A'>Room A</option>
       <
    option value='Room B'>Room B</option>
    </
    select

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    A lot depends on what database you are using.

    You would do database search on the value of the HTML field 'room'.

    If in your <form> you had 'action="POST" ' you would set that to a PHP variable like this.

    PHP Code:
    $room_type=$_POST['room']; 
    If in the database you had a field called 'roomtype' you would then do a database search for all instances where this field matched the variable $room_type.

    Sorry if this is a bit vague but you didn't give a lot of information in your question

  3. #3
    Join Date
    Sep 2009
    Posts
    48
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    I'm using mysql/php

    PHP Code:
    $result mysql_query("SELECT * FROM class WHERE log_id=$log_id");
    while(
    $row mysql_fetch_array($result))
    {
    $log_id=$row['log_id'];
    $room=$row['room'];
    }

    <
    form method='post' action='update.php'>

    <
    select name='room'>

     <
    option value=''>-</option>
      <
    option value='Room A'>Room A</option>
       <
    option value='Room B'>Room B</option>
    </
    select>  

    </
    form

  4. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    inside the file update.php you would have
    PHP Code:
    $room_type=$_POST['room']; 
    You would also include the code to open the database

    As I said before, I'll assume you have a field for the room type called 'roomtype' in Table 'rooms'.

    You would search the table like this

    PHP Code:
    $result mysql_query("SELECT * FROM rooms WHERE roomtype=$room_type"); 
    Hope this helps

  5. #5
    Join Date
    Sep 2009
    Posts
    48
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    thanks for the suggestion fa

    i finally got it...i made it simple..i don't want to use any extra table

    i don't know if this is the correct way but it works
    :-)

    PHP Code:

    <select name='room'>

        if(
    $room== 'Room A') {
      <
    option value='Room A' selected>Room A</option>
       <
    option value='Room B'>Room B</option>
    }

    else if (
    $room== 'Room B'
    {
      <
    option value='Room A'>Room A</option>
       <
    option value='Room B' selected>Room B</option>

    }

    else
    {
      <
    option value='Room A'>Room A</option>
       <
    option value='Room B'>Room B</option>
    }
    </
    select

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •