0

I need to get selected option value: Here is my code:

<?php
    $conn = mysql_connect("localhost","root","");
    if(!$conn)
    {
        die('Connection refused!'.mysql_error());
    }
    $dbs = mysql_query("SHOW DATABASES");
    echo "<select name=\"dbs_present\">";
    $i = 1;
    while($res = mysql_fetch_assoc($dbs))
    {
        echo "<option value=\"$i\">".$res['Database']."</option>";
        $i++;
    }
    echo "</select>";
?>

I can be able to display databases present in mysql in a dropdown using the above code but i unable to get the option which is selected in the dropdown.. How can i get the selected option so that i can display tables present inside that selected database?!

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
champ
  • 1
  • 1
  • Isn't it in `$_POST['dbs_present']` or `$_GET['dbs_present']`? – Barmar Feb 22 '14 at 10:11
  • you need a form and submit to another page, from there you can read by using $_POST['dbs_present']; – user1844933 Feb 22 '14 at 10:11
  • PHP runs on the server side and the user interacts with the page in his browser, on the client. You have to send the data back to your server. That's what forms are for. Read about them in the PHP manual: http://php.net/manual/en/language.variables.external.php. – Felix Kling Feb 22 '14 at 10:12
  • To do this dynamically (without submitting a form) you should either use javascript or jquery. Please refer to [this answer](http://stackoverflow.com/a/2780584/824495) if you are allowed to use JQUery. – mehmetseckin Feb 22 '14 at 10:13

4 Answers4

0
u can use this the execution time be less

echo "<option value='".$i."'>".$res['Database']."</option>";
vignesh pbs
  • 418
  • 2
  • 5
  • 16
0

use this..

$selectOption = $_POST['dbs_present'];
Sachin
  • 44
  • 11
0

try with this if you submit on the same page.

<?php
    $conn = mysql_connect("localhost","root","");
    if(!$conn)
    {
        die('Connection refused!'.mysql_error());
    }
    $dbs = mysql_query("SHOW DATABASES");
    echo '<select name="dbs_present">';
    $i = 1;
    while($res = mysql_fetch_assoc($dbs))
    {
        $selected_text = "";
        if(isset($_REQUEST['dbs_present']) && $_REQUEST['dbs_present']==$i)
        {
            $selected_text = " selected ";
        }  
        echo '<option value="'.$i.'" '.$selected_text.'>'.$res['Database'].'</option>';
        $i++;
    }
    echo "</select>";
?>
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

To do this dynamically (without submitting a form), you should either use JavaScript or jQUery. Please refer to this answer if you are allowed to use jQUery.

Using JQuery :

Get the currently selected item's value :

$('#dbs_present').val();

Using JavaScript :

var value = document.getElementById("dbs_present").value;

To tell your server this value, you should either use an ajax call, or submit the form.

Community
  • 1
  • 1
mehmetseckin
  • 3,061
  • 1
  • 31
  • 43
  • You're welcome, please upvote if you think this answer is useful, and accept it if this is the solution to your problem. – mehmetseckin Feb 22 '14 at 21:58