[RESOLU] Afficher cp et ville

Eléphant du PHP | 194 Messages

18 août 2021, 09:40

Bonjour à tous,

Je travail sur un projet perso, j'ai un formulaire avec des champs classique du genre nom prénom .......
puis un champs cp (code postal) et city (ville) qui sont eux dans une table.
Je souhaiterai que en tapant que 6 tous les code postaux apparaissent dans le champs et que quand je clic sur le champ ville qu'il y ai que les villes correspondantes.
Mon prof me dit de voir coté jquery mais je ne connais pas.

Si quelqu'un pourrais m'aider ou m’orientè, sa serai cool.

Merci par avance et merci au forum.

@+ Filou

formulaire de la view

Code : Tout sélectionner

<?php ob_start() ?> <!-- Inscription collaborateur/trice --> <div class="middleSite mt-3"> <form action="<?=htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post"> <!-- bouton radio Civilite Nom et prenom --> <div class="container"> <div class="row d-flex justify-content-center align-items-center"> <div class="col-10 col-lg-4 d-flex mb-3 texteFond"> <?php foreach ($returnCivility as $key => $value) {?> <div class="form-check me-2"> <input class="form-check-input" type="radio" value="<?= $value->id ?>" name="civility" id="civility<?= $value->id ?>" <?= (!empty($civility) && $civility == "1") ? 'checked' : '' ?>> <label class="form-check-label" for="civility<?= $value->id ?>"><?= $value->genre ?></label> </div> <?php } ?> <div class="error"><br><?= htmlentities($error['civility'] ?? '', ENT_QUOTES, 'UTF-8')?></div> </div> <div class="col-10 col-lg-4"> <div class="form-floating mb-3"> <input type="text" class="form-control" minlenght ="2" maxlength="25" name="name" id="name" placeholder="Votre nom" pattern="<?=REGEX_NO_NUMBER?>" required value="<?= htmlentities($_POST['name'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['name'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="name">Nom *</label> </div> </div> <div class="col-10 col-lg-4 offset-lg-0"> <div class="form-floating mb-3"> <input type="text" class="form-control" minlenght ="2" maxlength="25" name="firstname" id="firstname" placeholder="Votre prénom" pattern="<?=REGEX_NO_NUMBER?>" required value="<?= htmlentities($_POST['firstname'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['firstname'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="firstname">Prénom *</label> </div> </div> </div> </div> <!-- Fin nom et prenom --> <!-- Debut adresse --> <div class="container"> <div class="row justify-content-center"> <div class="col-10 col-lg-4"> <div class="form-floating mb-3"> <input type="text" class="form-control" name="adress" id="adress" placeholder="Votre adresse" minlength="8" maxlength="100" required value="<?= htmlentities($_POST['adress'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['adress'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="adress">Adresse *</label> </div> </div> <div class="col-10 col-lg-4 offset-lg-0"> <div class="form-floating mb-3"> <input type="text" class="form-control" name="cp" id="cp" placeholder="Votre code postal" minlength="5" maxlength="5" required pattern="<?=REGEX_CP?>" value="<?= htmlentities($_POST['cp'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['cp'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="cp">Code postal *</label> </div> </div> <div class="col-10 col-lg-4"> <div class="form-floating mb-3"> <input type="text" class="form-control" name="city" id="city" placeholder="Votre ville" minlength="2" maxlength="50" required pattern="<?=REGEX_NO_NUMBER?>" value="<?= htmlentities($_POST['city'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['city'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="city">Ville *</label> </div> </div> </div> </div> <!-- Fin adresse --> <!-- Debut Date naissance + tel --> <div class="container"> <div class="row justify-content-center"> <div class="col-10 col-lg-4"> <div class="form-floating mb-3"> <input type="date" class="form-control" name="birthday" id="birthday" placeholder="Votre date de naissance" required pattern="<?=REGEX_DATE?>" value="<?=htmlentities($birthday ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['birthday'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="birthday">Naissance *</label> </div> </div> <div class="col-10 col-lg-4"> <div class="form-floating mb-3"> <input type="tel" class="form-control" name="telMobile" id="telMobile" placeholder="Votre telephone mobile" required pattern="<?=REGEX_PHONE_FRANCE10?>" value="<?= htmlentities($_POST['telMobile'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['telMobile'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="telMobile">Tel portable ou telephone joignable *</label> </div> </div> <div class="col-10 col-lg-4"> <div class="form-floating mb-3"> <input type="tel" class="form-control" name="telHome" id="telHome" placeholder="Votre telephone fixe" pattern="<?=REGEX_PHONE_FRANCE10?>" value="<?= htmlentities($_POST['telHome'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['telHome'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="telHome">Tel fixe</label> </div> </div> </div> </div> <!-- Fin date naissance + tel --> <!-- Debut mail --> <div class="container"> <div class="row justify-content-center align-items-center"> <div class="col-10 col-lg-4"> <div class="form-floating mb-3"> <input type="email" class="form-control" name="mail" id="mail" placeholder="Votre mail" required value="<?= htmlentities($_POST['mail'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['mail'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="mail">Mail *</label> </div> </div> <div class="col-10 col-lg-4 offset-lg-0"> <div class="form-floating mb-3"> <input type="email" class="form-control" name="mailConf" id="mailConf" placeholder="Confirmation du mail" required value="<?= htmlentities($_POST['mailConf'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['mailConf'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="mailConf">Confirmation du mail *</label> </div> </div> <div class="col-10 col-lg-4 offset-lg-0 mb-3"> <select name="id_roles" id="id_roles" class="form-control"> <option value="">Niveaux *</option> <?php foreach ($returnRoles as $key => $value) {?> <option value="<?= $value->id ?>" <?=$value==$level ? 'selected' : ''?> ><?= $value->level ?></option> <?php } ?> </select> <?= htmlentities($error['id_roles'] ?? '', ENT_QUOTES, 'UTF-8')?> </div> <div class="col-10 col-lg-12"> <textarea class="form-control mb-3" name="commentaire" id="commentaire" rows="2" placeholder="Commentaire"><?= htmlentities($_POST['commentaire'] ?? '', ENT_QUOTES, 'UTF-8')?></textarea> </div> <div class="col-10 col-lg-12"> <div class="form-floating mb-3"> <p class="texteFond">* Champs obligatoire</p> </div> </div> </div> </div> <!-- Fin mail --> <!-- Debut buttons --> <div class="container"> <div class="row d-flex justify-content-center"> <div class="col-10 d-flex col-lg-12 justify-content-end mb-5"> <button type="reset" class="btn btn-danger ">Effacer</button> <button type="submit" class="btn btn-success ms-2">Valider</button> </div> </div> </div> <!-- Fin buttons --> </form> <!-- Fin inscription collaborateur/trice --> </div> <?php $content = ob_get_clean(); $title = "Enregistrement d'un collaborateur"; $title1 = "Enregistrement d'un collaborateur"; include(dirname(__FILE__).'/../templates/template.php'); ?>

Eléphant du PHP | 194 Messages

18 août 2021, 21:21

J'ai modifier la partie de mon code front pour faire une boucle sur les villes

Code : Tout sélectionner

<div class="col-10 col-lg-4 offset-lg-0"> <div class="form-floating mb-3"> <input type="text" class="form-control" name="cp" id="cp" placeholder="Votre code postal" minlength="5" maxlength="5" required pattern="<?=REGEX_CP?>" value="<?= htmlentities($_POST['cp'] ?? '', ENT_QUOTES, 'UTF-8')?>"> <div class="error"><?= htmlentities($error['cp'] ?? '', ENT_QUOTES, 'UTF-8')?></div> <label for="cp">Code postal *</label> </div> </div> <div class="col-10 col-lg-4 offset-lg-0 mb-3"> <select name="ville" id="ville" class="form-control"> <option value="">Ville *</option> <?php foreach ($returnCity as $key => $value) {?> <option value="<?= $value->id ?>" <?=$value==$ville ? 'selected' : ''?> ><?= $value->ville ?></option> <?php } ?> </select> <?= htmlentities($error['ville'] ?? '', ENT_QUOTES, 'UTF-8')?> </div> </div> </div> <!-- Fin adresse -->
Je voudrai qu'en rentrant le code postal cela limite la liste des villes et qu'en fonction de la ville choisi je rentre id du cp et donc de la ville.
Ma table et faite comme ceci : id |cp | ville

Merci par avance pour votre aide.

@+ Filou

Avatar du membre
Administrateur PHPfrance
Administrateur PHPfrance | 9782 Messages

19 août 2021, 10:08

Quand tout le reste a échoué, lisez le mode d'emploi...

Eléphant du PHP | 194 Messages

19 août 2021, 14:42

Bonjour,

C'est exactement ça mais je ne veux pas passer par une api je veux rester totalement autonome.
mais c'est cela que je souhaite faire en tapant mon code postal de mon input afficher dans le select a coté les villes avec ce code postal.

@+ Filou

Avatar du membre
Administrateur PHPfrance
Administrateur PHPfrance | 9782 Messages

23 août 2021, 11:12

Oui mais justement c'est pour que tu t'en inspires.
Tu peux réutiliser ce qu'ils ont fait en javascript.
Et tu as juste à construire un petit script PHP qui prend en paramètre un code postal, fais une requête SQL sur ta bdd et renvoyer un JSON sous cette forme :
https://vicopo.selfbuild.fr/cherche/750
Quand tout le reste a échoué, lisez le mode d'emploi...

Eléphant du PHP | 194 Messages

23 août 2021, 19:03

Bonjour,
Je vais me jeter dedans pour voir, la je dois refaire tous mon MVC mon prof me dit on peut amélioré ......
Merci a vous je passe en resolu.
@+ Filou