Header ads

Header ads
» » » » Bind jQuery ComboBox to MySql Database using PHP


In this post, we will show you how to connect our jQuery ComboBox, called jqxComboBox to MySql Database using PHP. We will obtain the data from MySql Database and especially the Northwind Database. The first thing we need to do is create the file we’ll connect with. We’ll call this file connect.php.
<?php
# FileName="connect.php"
$hostname = "localhost";
$database = "customers";
$username = "root";
$password = "";
?>
Now, we have our file to do the connection for us and we need to create the file that will run the query and bring the data so our ComboBox can be populated. We will call the file data.php.
<?php
#Include the connect.php file
include('connect.php');
#Connect to the database
//connection String
$connect = mysql_connect($hostname, $username, $password)
or die('Could not connect: ' . mysql_error());
//select database
mysql_select_db($database, $connect);
//Select The database
$bool = mysql_select_db($database, $connect);
if ($bool === False){
print "can't find $database";
}
// get data and store in a json array
$query = "SELECT * FROM customers";
$from = 0;
$to = 30;
$query .= " LIMIT ".$from.",".$to;
$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$customers[] = array(
'CompanyName' => $row['CompanyName'],
'ContactName' => $row['ContactName'],
'ContactTitle' => $row['ContactTitle'],
'Address' => $row['Address'],
'City' => $row['City']
);
}
echo json_encode($customers);
?>
The data is returned as JSON. This is it for the connection and data gathering. Let’s see how to add the data we just gathered into our jQuery ComboBox. Create the index.php file and add references to the following javascript and css files.
<link rel="stylesheet" href="../jqwidgets/styles/jqx.base.css" type="text/css">
<link rel="stylesheet" href="../jqwidgets/styles/jqx.classic.css" type="text/css">
<script type="text/javascript" src="../scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxbuttons.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxscrollbar.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxdata.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxlistbox.js"></script>
<script type="text/javascript" src="../jqwidgets/jqxcombobox.js"></script>
Create a div tag for the ComboBox.
<div id="jqxcombobox"></div>
Create your ComboBox and load the data. We define a source object for the ComboBox and bind that source to the data.php which returns the JSON data. We are also defining the ComboBox’s displayMember and valueMember properties. The displayMember specifies the name of an object property to display. The valueMember specifies the name of an object property to set as a ‘value’ of the list items.
<script type="text/javascript">
$(document).ready(function () {
// prepare the data
var source =
{
datatype: "json",
datafields: [
{ name: 'CompanyName' },
{ name: 'ContactName' },
{ name: 'ContactTitle' },
{ name: 'Address' },
{ name: 'City' },
],
url: 'data.php'
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#jqxcombobox").jqxComboBox(
{
source: dataAdapter,
theme: 'classic',
width: 200,
height: 25,
selectedIndex: 0,
displayMember: 'CompanyName',
valueMember: 'ContactName'
});
});
</script>

About Học viện đào tạo trực tuyến

Xinh chào bạn. Tôi là Đinh Anh Tuấn - Thạc sĩ CNTT. Email: dinhanhtuan68@gmail.com .
- Nhận đào tạo trực tuyến lập trình dành cho nhà quản lý, kế toán bằng Foxpro, Access 2010, Excel, Macro Excel, Macro Word, chứng chỉ MOS cao cấp, IC3, tiếng anh, phần mềm, phần cứng .
- Nhận thiết kế phần mềm quản lý, Web, Web ứng dụng, quản lý, bán hàng,... Nhận Thiết kế bài giảng điện tử, số hóa tài liệu...
HỌC VIỆN ĐÀO TẠO TRỰC TUYẾN:TẬN TÂM-CHẤT LƯỢNG.
«
Next
Bài đăng Mới hơn
»
Previous
Bài đăng Cũ hơn