MySQL Database Drop Down Listing




Project Structure


index.php

<!DOCTYPE html>
<html>
<head>
<title>MySQL Database Drop Down Listing</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
<script type="text/javascript" src="bootstrap/js/bootstrap.js"></script>

</head>
<body>
<div class="col-md-4 col-md-offset-4">
<form>
<div class="form-group">
<label for="servername">Server Name</label>
<input type="text" class="form-control" id="servername" placeholder="Type Server Name">
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="password" class="form-control" id="username" placeholder="Username">
</div>
<div class="form-group">
<label for="userpassword">Password</label>
<input type="password" class="form-control" id="userpassword" placeholder="Password">
</div>
<div class="form-group">
<label for="databasename">Database Name List</label>
<select class="form-control" id="databasename">
<option value="">Select Dataase Name</option>
</select>
</div>
</form>
</div>
<script type="text/javascript">
$(document).ready(function(e){
$('#databasename').on('focus',function(e){
var parse,len,opt='',db;
var getServer = $('#servername').val();
var getUser = $('#username').val();
var getPass = $('#userpassword').val();

var phpJson = {
'getServer':getServer,
'getUser':getUser,
'getPass':getPass
};
$.ajax({
method:'POST',
url:'dbconn.php',
data:phpJson,
success:function(resp){
parse = jQuery.parseJSON( resp )
len = parse.jsonReturn.length;
for(i=0;i<len;i++){
db = parse.jsonReturn[i].Database;
opt+='<option value="'+db+'">'+db+'</option>';
}
$('#databasename').append(opt);
}
});
})
});
</script>
</body>
</html>
------------------------------------------
dbconn.php
<?php 
$getServer = $_POST['getServer'];
$getUser = $_POST['getUser'];
$getPass = $_POST['getPass'];
$dbcnx = mysql_connect ($getServer, $getUser, $getPass); 
$result = @mysql_query('SHOW DATABASES'); 
$arrData = array();
while ($row = mysql_fetch_array($result)) { 
$arrData[] = $row;
}

echo json_encode(array('jsonReturn'=>$arrData));
?>


Demo 


Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment