The site is implemented on Smarty .
There was a desire to add a search on the site.
Faced a problem: I can’t understand how to pass from the searchproductAction , what it returns, it works correctly, but I don’t understand how to use its value. SearchController.php :
function indexAction($smarty){ $rsCategories = getAllMainCatsWithChildren(); $text = isset($text) ? $text : null; $smarty->assign('text', $text); $smarty->assign('pageTitle', 'Поиск'); $smarty->assign('rsCategories',$rsCategories); loadTemplate($smarty, 'header'); loadTemplate($smarty, 'search'); loadTemplate($smarty, 'footer'); } function searchproductAction(){ $search_box = $_POST['search_box']; $search_box = trim($search_box); $search_box = mysql_real_escape_string($search_box); $search_box = htmlspecialchars($search_box); if (empty($search_box)) { $resData['success'] = 0; $resData['message'] = 'Вы ничего не ввели в окно поиска'; echo json_encode($resData); return; } if (strlen($search_box) < 3) { $resData['success'] = 0; $resData['message'] = 'Слишком короткий поисковый запрос'; } else if (strlen($search_box) > 128) { $resData['success'] = 0; $resData['message'] = 'Слишком длинный поисковый запрос'; } else { $text = 0; $res = searchTextProduct($search_box); if (mysql_affected_rows() > 0) { $search_text = mysql_fetch_assoc($res); $num = mysql_num_rows($res); $resData['success'] = 1; $resData['message'] = "По запросу $search_box найдено совпадений: $num"; do { // Делаем запрос, получающий ссылки на продукты $res1=getLinkSearchProduct($search_text); if (mysql_affected_rows() > 0) { $product = mysql_fetch_assoc($res1); } $text = $text . '<p><a href="/product/'.$product['id'].'/">'.$product['name'].'</a></p>'; } while ($search_text = mysql_fetch_assoc($res)); return $text; } else { $resData['success'] = 0; $resData['message'] = 'По вашему запросу ничего не найдено'; } } } Search form:
<form method="post" action="/search/"> <input type="text" name="search_box" id="search_box" class='search_box' placeholder="Что искать?" /> <input type="submit" value="Поиск" class="search_button" onclick="searchProduct();"/><br /> </form> javascript:
function searchProduct() { var search_box = $('#search_box').val(); var postData = {search_box:search_box}; $.ajax({ type: 'POST', async: true, url: "/search/searchproduct/", data: postData, dataType: 'json', success: function (data) { if (data['success']) { alert(data['message']); document.location = '/search/'; } else { alert(data['message']); } } }); } search.tpl:
<div class="text-info"> {if isset($text)} По вашему искомому слову найдено: <br> {$text} {/if} </div> Everything should work as follows: the user enters a word, receives an alert with the number of products found, with the given word, then the shell ( search.tpl ) to the file, displays the link (s) and name (s) of the product (s)
$resdata['content']and output it$resdata['content']when you receive a response. without any redirections there. - teranmysql_affected_rowsand the outdatedmysqlextension 10 years ago? why checkissetin the template. templates are made to simplify the maximum layout. there is no need to write there. - teran