Autocompletion avec redirection

Par nicolastaf, il y a 8 ans


Bonsoir a tous,

je tourne en rond pour la redirection de l'autocompletion, j'ai ma requete pour récupérer ma table ou je souhaite faire correspondre la recherche et mon js pour le front.

Si vous pouviez m'aider se serait COOL...

ma requete:

$pdo = connect(); $keyword = '%'.$_POST['keyword'].'%'; $sql = " SELECT posts.id as idPost, posts.manufacturer_code, posts.title, posts.content, posts.writer, posts.image, posts.date_post, posts.posted, posts.menus_id, posts.delivery,posts.price,posts.brand,posts.deadlines,posts.link_shop,posts.availability,posts.guarantee FROM posts WHERE title LIKE (:keyword) ORDER BY idPost ASC LIMIT 0, 10"; $query = $pdo->prepare($sql); $query->bindParam(':keyword', $keyword, PDO::PARAM_STR); $query->execute(); $list = $query->fetchAll(); foreach ($list as $rs) { // put in bold the written text $title = str_replace(htmlspecialchars($_POST['keyword']), '<b>'.$_POST['keyword'].'</b>', $rs['title']); // add new option echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['title']).'\')">'.$title.'</li>'; }

mon js:

function autocomplet() { var min_length = 2; // min caracters to display the autocomplete var keyword = $('#autocomplete_input').val(); if (keyword.length >= min_length) { $.ajax({ url: 'content/ajax/autocomplet.php', type: 'POST', data: {keyword:keyword}, success:function(data){ $('#country_list_id').show(); $('#country_list_id').html(data); } }); } else { $('#country_list_id').hide(); } } // set_item : this function will be executed when we select an item function set_item(item) { var idPost = $(this).attr('id'); console.log(id); // change input value $('#autocomplete_input').val(item); window.location = "index.php?controller=blog&page=blog&id="+idPost; // hide proposition list $('#country_list_id').hide(); };

et mon form:

<form method="post"> <div class="input-field"> <i class="material-icons prefix">search</i> <label for="autocomplete-input">Rechercher un article...</label> <input type="text" id="autocomplete_input" class="autocomplete" onkeyup="autocomplet()"> <ul id="country_list_id" class="autocomplete-content dropdown-content"></ul> </div> </form>

Mon autocompletion fonctionne j'ai bien le title qui remonte dans mon input, pour la redirection je seche et coté sécurité y t-il un risque qvec se code ???

Merci d'avance

La solution a mon PB c'est de supprimer la function :

function set_item(item) { var idPost = $(this).attr('id'); console.log(id); // change input value $('#autocomplete_input').val(item); window.location = "index.php?controller=blog&page=blog&id="+idPost; // hide proposition list $('#country_list_id').hide(); };

et dans ma function PHP :

foreach ($list as $rs) { // put in bold the written text $title = str_replace(htmlspecialchars($_POST['keyword']), '<b>'.$_POST['keyword'].'</b>', $rs['title']); // add new option echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['title']).'\')">'.$title.'</li>'; }

Remplacer par

foreach ($list as $rs) { // put in bold the written text $title = str_replace(htmlspecialchars($_POST['keyword']), '<b>'.$_POST['keyword'].'</b>', $rs['title']); // add new option echo '<li><a href="index.php?controller=blog&page=post&id='.$rs['idPost'].'">'.$title.'</a></li>'; }

Pour ceux que cela peut interesser...

Aucune réponse