Depuis quelques jours je cherche a réaliser un formulaire, avec la possibilité de remplier des champs en automatique avec deux champ remplie.
mon but est le suivant :
Je voudrais remplir le pays, puis le code postal, et là il me donne un choix de ville que je choisie puis il remplir la région puis le département en auto.
Voilà le code que j’utilise :
AutoCompletion.php :
Code : Tout sélectionner
<?php
require_once('./AutoCompletionCPVille.class.php');
//Initialisation de la liste
$list = array();
//Connexion MySQL
try
{
$db = new PDO('mysql:host=123.123.123.123;dbname=pays', 'root', 'root', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
}
catch (Exception $ex)
{
echo $ex->getMessage();
}
//Construction de la requete
$strQuery = "SELECT CP CodePostal, VILLE Ville FROM pays WHERE ";
if (isset($_POST["codePostal"]))
{
$strQuery .= "CP LIKE :codePostal ";
}
else
{
$strQuery .= "VILLE LIKE :ville ";
}
$strQuery .= "AND CODEPAYS = 'FR' ";
//Limite
if (isset($_POST["maxRows"]))
{
$strQuery .= "LIMIT 0, :maxRows";
}
$query = $db->prepare($strQuery);
if (isset($_POST["codePostal"]))
{
$value = $_POST["codePostal"]."%";
$query->bindParam(":codePostal", $value, PDO::PARAM_STR);
}
else
{
$value = $_POST["ville"]."%";
$query->bindParam(":ville", $value, PDO::PARAM_STR);
}
//Limite
if (isset($_POST["maxRows"]))
{
$valueRows = intval($_POST["maxRows"]);
$query->bindParam(":maxRows", $valueRows, PDO::PARAM_INT);
}
$query->execute();
$list = $query->fetchAll(PDO::FETCH_CLASS, "AutoCompletionCPVille");;
echo json_encode($list);
?>
Code : Tout sélectionner
<?php
class AutoCompletionCPVille {
public $CodePostal;
public $Ville;
}
?>
Code : Tout sélectionner
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title>AutoCompletion</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="https://ajax.microsoft.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
<script type="text/javascript">
var cache = {};
$(function ()
{
$("#cp, #ville, #region").autocomplete({
source: function (request, response)
{
//Si la réponse est dans le cache
if (('FR' + '-' + request.term) in cache)
{
response($.map(cache['FR' + '-' + request.term], function (item)
{
return {
label: item.CodePostal + ", " + item.Ville,
value: function ()
{
if ($(this).attr('id') == 'cp')
{
$('#ville').val(item.Ville);
return item.CodePostal;
}
else
{
$('#cp').val(item.CodePostal);
return item.Ville;
}
}
}
}));
}
//Sinon -> Requete Ajax
else
{
var objData = {};
if ($(this.element).attr('id') == 'cp')
{
objData = { codePostal: request.term, pays: 'FR', maxRows: 10 };
}
else
{
objData = { ville: request.term, pays: 'FR', maxRows: 10 };
}
$.ajax({
url: "AutoCompletion.php",
dataType: "json",
data: objData,
type: 'POST',
success: function (data)
{
//Ajout de reponse dans le cache
cache[('FR' + '-' + request.term)] = data;
response($.map(data, function (item)
{
return {
label: item.CodePostal + ", " + item.Ville,
value: function ()
{
if ($(this).attr('id') == 'cp')
{
$('#ville').val(item.Ville);
return item.CodePostal;
}
else
{
$('#cp').val(item.CodePostal);
return item.Ville;
}
}
}
}));
}
});
}
},
minLength: 3,
delay: 600
});
});
</script>
<link rel="Stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css" />
</head>
<body>
<form action="#">
Pays :<input type="text" id="ps" size="6"/>
Code postal :<input type="text" id="cp" size="6"/>
Ville : <input type="text" id="ville" />
Région : <input type="text" id="rg" />
Département : <input type="text" id="dp" />
</form>
</body>
</html>
Code : Tout sélectionner
-- phpMyAdmin SQL Dump
-- version 3.3.7
-- http://www.phpmyadmin.net
--
-- Serveur: localhost
-- Généré le : Mer 09 Mars 2011 à 16:24
-- Version du serveur: 5.1.51
-- Version de PHP: 5.2.14
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Base de données: `batifac`
--
-- --------------------------------------------------------
--
-- Structure de la table `pays`
--
CREATE TABLE IF NOT EXISTS `pays` (
`CODEPAYS` char(2) NOT NULL,
`CP` varchar(10) NOT NULL,
`VILLE` varchar(180) NOT NULL,
KEY `CODEPAYS` (`CODEPAYS`),
KEY `CP` (`CP`),
KEY `VILLE` (`VILLE`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Contenu de la table `pays`
--
INSERT INTO `pays` (`CODEPAYS`, `CP`, `VILLE`) VALUES
('FR', '24000', 'Périgueux'),
('FR', '24001', 'Périgueux'),
('FR', '24002', 'Périgueux'),
('FR', '24003', 'Périgueux'),
('FR', '24004', 'Périgueux'),
('FR', '24005', 'Périgueux'),
('FR', '24007', 'Périgueux'),
('FR', '24009', 'Périgueux'),
('FR', '24010', 'Périgueux'),
('FR', '24011', 'Périgueux'),
('FR', '24012', 'Périgueux'),
('FR', '24013', 'Périgueux'),
('FR', '24014', 'Périgueux'),
('FR', '24015', 'Périgueux'),
('FR', '24016', 'Périgueux'),
('FR', '24017', 'Périgueux'),
('FR', '24019', 'Périgueux'),
('FR', '24020', 'Périgueux'),
('FR', '24022', 'Périgueux'),
('FR', '24024', 'Périgueux'),
('FR', '24029', 'Périgueux'),
('FR', '24050', 'Périgueux'),
('FR', '24051', 'Périgueux'),
('FR', '24052', 'Périgueux'),
('FR', '24055', 'Périgueux'),
('FR', '24059', 'Périgueux'),
('FR', '24060', 'Périgueux'),
('FR', '24100', 'Creysse'),
('FR', '24100', 'Bergerac'),
('FR', '24100', 'Lembras'),
('FR', '24100', 'Saint-Laurent-des-Vignes'),
('FR', '24101', 'Bergerac'),
('FR', '24102', 'Bergerac'),
('FR', '24104', 'Bergerac'),
('FR', '24105', 'Bergerac'),
('FR', '24106', 'Bergerac'),
('FR', '24107', 'Bergerac'),
('FR', '24108', 'Bergerac'),
('FR', '24109', 'Bergerac'),
('FR', '24110', 'Saint-Astier'),
('FR', '24110', 'Montrem'),
('FR', '24110', 'Léguillac-de-l''Auche'),
('FR', '24110', 'Grignols'),
('FR', '24110', 'Manzac-sur-Vern'),
('FR', '24110', 'Saint-Aquilin'),
('FR', '24110', 'Bourrou'),
('FR', '24110', 'Saint-Léon-sur-l''Isle'),
('FR', '24111', 'Bergerac'),
('FR', '24112', 'Bergerac'),
('FR', '24113', 'Bergerac'),
('FR', '24114', 'Bergerac'),
('FR', '24120', 'La Feuillade'),
('FR', '24120', 'La Dornac'),
('FR', '24120', 'Terrasson-Lavilledieu'),
('FR', '24120', 'La Cassagne'),
('FR', '24120', 'Chavagnac'),
('FR', '24120', 'Villac'),
('FR', '24120', 'Grèzes'),
('FR', '24120', 'Châtres'),
('FR', '24120', 'Beauregard-de-Terrasson'),
('FR', '24120', 'Coly'),
('FR', '24120', 'Pazayac'),
('FR', '24121', 'Terrasson-Lavilledieu'),
('FR', '24122', 'Terrasson-Lavilledieu'),
('FR', '24130', 'Saint-Pierre-d''Eyraud'),
('FR', '24130', 'Ginestet'),
('FR', '24130', 'Le Fleix'),
('FR', '24130', 'Saint-Georges-Blancaneix'),
('FR', '24130', 'La Force'),
('FR', '24130', 'Monfaucon'),
('FR', '24130', 'Lunas'),
('FR', '24130', 'Prigonrieux'),
('FR', '24130', 'Bosset'),
('FR', '24130', 'Laveyssière'),
('FR', '24130', 'Fraisse'),
('FR', '24140', 'Saint-Hilaire-d''Estissac'),
('FR', '24140', 'Beauregard-et-Bassac'),
('FR', '24140', 'Jaure'),
('FR', '24140', 'Beleymas'),
('FR', '24140', 'Saint-Julien-de-Crempse'),
('FR', '24140', 'Saint-Martin-des-Combes'),
('FR', '24140', 'Clermont-de-Beauregard'),
('FR', '24140', 'Villamblard'),
('FR', '24140', 'Maurens'),
('FR', '24140', 'Saint-Jean-d''Eyraud'),
('FR', '24140', 'Saint-Jean-d''Estissac'),
('FR', '24140', 'Saint-Georges-de-Montclard'),
('FR', '24140', 'Douville'),
('FR', '24140', 'Montagnac-la-Crempse'),
('FR', '24140', 'Campsegret'),
('FR', '24140', 'Queyssac'),
('FR', '24150', 'Saint-Capraise-de-Lalinde'),
('FR', '24150', 'Badefols-sur-Dordogne'),
('FR', '24150', 'Bourniquel'),
('FR', '24150', 'Lanquais'),
('FR', '24150', 'Baneuil'),
('FR', '24150', 'Varennes'),
...
Merci de votre aide,