4 pièce(s) jointe(s)
Afficher une réponse Ajax dans un champ html input
Salut
Je voudrais afficher le BIC d'un IBAN dans un champ input.
J'arrive à obtenir la réponse ajax(j'ai testé avec firebug) qui affiche le BIC mais quand je clique sur le champ bic, le BIC ne s'affiche pas dans ce champ.
je ne sais pas là où le problème se pose.
Mon code curl.php pour afficher le bic:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?php
//Affichage des erreurs PHP
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
include_once('simple_html_dom.php');
/* Utilisation de Curl */
//On stock la valeur des variables POST
$iban=!empty($_POST['iban']) ? $_POST['iban'] : null;
//$iban = 'BE30293034556711';
$url='https://www.ibancalculator.com/validate/'.$iban.'/';
$data=array(
'tx_valIBAN_pi1[iban]' => $iban) ;
$fields_string = http_build_query($data);
//Initialisation de curl dans $curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //Page sur laquelle envoyer les POST autrement dit la page vers laquelle pointe le formulaire
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string); //On envoie les valeurs
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Required for HTTP error codes to be reported via our call to curl_error($ch)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch); //Recuparation de la page
curl_close($ch);
$html = file_get_html($url);
$pattern='[A-Z]{6,6}[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3,3}){0,1}'; //regex pour BIC EU
preg_match_all('/'.$pattern.'/', $html->plaintext, $out);
echo json_encode(array('bic'=>$out[0][0]));
// clean up memory
$html->clear();
unset($html);
?> |
Mon code html:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<form action="../app/user/test" method="post">
<div class="form-group row">
<label class="col-form-label col-lg-2">IBAN</label>
<div class="col-lg-10">
<input type="text" name="iban" id="iban" class="form-control" required>
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-lg-2">BIC</label>
<div class="col-lg-10">
<input type="text" name="bic" id="bic" class="form-control" required>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn bg-info">Test</button>
</div>
</form>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$("#iban").on("blur",function(){
//on récupére l'IBAN
var iban = $(this).val();
getBIC(iban);
});
function getBIC(iban){
//On prépare l'Ajax
$.ajax({
type: "POST",
url:'curl.php',
data: { iban:iban }, //On lui donne le nom des données à récupéré
dataType: "json", //on indique le type de fichier que retourne notre url:'curl.php'
success: function(result) {
if(typeof(result)!="undefined" && result!=null){
if(result) {
console.log(result);
$("#bic").val(result.bic);
}else{
/* alert("Verifie la console") ;*/
}
} else{
/* alert ("Erreur");*/
}
},
error: function (err) {
console.log("Erreur Ajax : ",err);
/* alert("Erreur Ajax ! Regardez la console pour plus dinformations...");*/
},
});
}
</script>
</body>
</html> |
Lorsque je teste l'iban FR1420041010050500013M02606, voici les erreurs que j'obtiens
Pièce jointe 576968Pièce jointe 576970Pièce jointe 576969Pièce jointe 576971
J'ai besoin d'aide svp
Cordialement
Joey