Load database value to a text box

Wednesday, November 4, 2015

I’m developing a website using PHP. I have a mySQL database called “db” and a table called “places”. It is as follows

-----------------------------
Id | name
-----------------------------
25|AA
26|BB
27|CC
28|BB
29|AA
30|AA

I want to enter the name on my web interface, and it should return the relevant smallest value of ID and put that value into a text box without refreshing the page.
For example, if I enter “BB” it should return “26” in the text box.
This is my code so far. It’s not working for me. Can anyone please help me to find the problem here?

Index.php
---------------



<html>
<head>
<meta type="description" content="Map"/>
http://jquery-1.11.0.min.js
http://script.js
</head>
<body>

<form>
Name<input type="text" id="name" name="name"><input type="button" id="submit" value="select"><br><br>
Id<input type="text" name="min" id="min" value="">

</form>
</body>
</html>




Script.js
-----------------



$(document).ready(function(){
$('#submit').click(function (e){
e.preventDefault();
var $name=$('#name').val();
$.ajax({
type: "POST",
dataType: "json",
data: {"name": $name},
url: 'select.php',
success: function(data) {
$('#min').val(data.r_id);
}
});
});
})




Select.php
---------------------



<?php

$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server..
$db = mysql_select_db("db", $connection); // Selecting Database

if($_POST)
{
$name=$_POST['name'];
$query1=mysql_query("select MIN(id) as id1 from places where mname='$name'");
while($query2=mysql_fetch_array($query1))
{
$id=$query2['id1'];
}
$response=array(
'r_id'=>$id,
);
}
print json_encode($response);

?>




Any help would be appreciated. Thanks in advance.

0 comments:

Post a Comment