Login Register

Combobox and MySql

Hi,
I want to make a combobox which display the contenent from MYSQL I was searching for goods examples but I can't found anything, any one can help me.

My approach is using php but can't found samples...

Camilo

Combobox and MySql

Look at http://dojotoolkit.org/book/dojo-book-0-9/part-2-dijit/form-validation-s...

You have to use 2 dijit widgets : a comboBox and a store. Pass your php to your store :

Your php have to return datas in a special json format.

{
identifier:"abbreviation",
items: [
        {name:"Alabama", label:"Alabama",abbreviation:"AL"},
        {name:"Alaska", label:"Alaska",abbreviation:"AK"},
        {name:"American Samoa", label:"American Samoa",abbreviation:"AS"},
        {name:"Arizona", label:"Arizona",abbreviation:"AZ"},
        ...

Thanks, it works

Thanks, it works i post my code for others

<?php
$username = "root";
$password = "123456";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
// you're going to do lots more here soon
$selected = mysql_select_db("baseancienne",$dbh)
or die("Could not select first_test");
$res = '{identifier:"abbreviation",';

$res = $res.'items: [';
$i = 0;

$result = mysql_query("SELECT * FROM CLIENTS ORDER BY ID_CLIENT");
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$res = $res.'{name:"'.$row{'NOM1'}.'", label:"'.$row{'ID_CLIENT'}.'",abbreviation:"'.$row{'ID_CLIENT'}.'"},';
}
$res = $res.']}';
mysql_close($dbh);
print $res;
?>

_________________________________________________________________________________________________________

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Simple ComboBox</title>
    <style type="text/css">
        @import "http://o.aolcdn.com/dojo/1.0.0/dijit/themes/tundra/tundra.css";
        @import "http://o.aolcdn.com/dojo/1.0.0/dojo/resources/dojo.css"
    </style>
    <script type="text/javascript" src="http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js"
        djConfig="parseOnLoad: true">
</script>
    <script type="text/javascript">
       dojo.require("dojo.parser");
       dojo.require("dijit.form.ComboBox");
       dojo.require("dojo.data.ItemFileReadStore");
       function setVal2(value) {
           console.debug("Selected "+value);
       }
   </script>
</head>
<body class="tundra">
     <div dojoType="dojo.data.ItemFileReadStore" jsId="stateStore"
              url="myPhpFileName.php">
</div>
             
     <input dojoType="dijit.form.ComboBox"
                store="stateStore"
                value="California"
                searchAttr="name"
                name="state2"
                onChange="setVal2" />

       
</body>

Thanks!

Thanks for posting your code on how to do this, it helps so much with having dojo widgets interact with php and mysql!

Internet Explorer

Watch out, while the code above works, IE 6 and 7 don't seem to like the trailing , on the JSON for some reason. Firefox handles it fine. Its a simple fix to his above php script after the while loop and before $res = $res.']}'; add this line:

$res = substr($res,0,strlen($res)-1);

that should basically cut off the last , and IE should work properly.