Voici une solution partielle pour vous commencer:
// bind to the keyup event of a text input
$("#textinput").keyup(function() {
// when the event is triggered, send the captured text to the server
// as the 'whichImage' param
$.getJSON('image.php', { whichImage: $(this).val()}, function(json) {
if(!json.error) {
// modify the src (and whatever other attributes) to
// whatever the response contains
// another approach would be to send back an entire image tag and insert it
// somewhere in the document
$("#theImage").attr('src', json.src);
$("#theImage").attr('width', json.width);
} else {
alert(json.error);
}
});
});
PHP:
<?php
if(isset($_GET['whichImage']) && !empty($_GET['whichImage'])) {
// supposing that function returns array('src' => 'foo.jpg', 'width' => '250px') etc..
$imgArray = getMatchingImage($_GET['whichImage']);
if(!empty($imgArray)) {
echo json_encode($imgArray);
} else {
echo json_encode(array('error' => 'no match!'));
}
exit;
}
?>
Ce serait une bonne idée d'ajouter un délai à envoyer le texte capturé au serveur, en fait la demande ne tirent pas constamment pendant que l'utilisateur tape. J'espère que c'était utile!
Je l'ai presque +1 fois attribué à "ajax-acally". Impressionnant. – JasCav
Je pensais que ce mot était plutôt cool. – nn2
C'est amusant à dire, bien que la plupart l'épellent "ajaxically". – webbiedave