[英]Single Ajax in symfony2
i have to integrate ajax in my proyect, something like the seeker of google . 我必须将ajax集成到我的proyect中,类似于google的搜索者。 i did something like this:
我做了这样的事情:
pedido.html.twig pedido.html.twig
/**... html code...
...
...
...**/
$(document).ready(function(){
var consulta;
$("#busqueda").focus();
$("#busqueda").keyup(function(e){
//obtenemos el texto introducido en el campo de búsqueda
consulta = $("#busqueda").val();
//hace la búsqueda
$.ajax({
type: "POST",
url: "{{ path('buscar_productos') }}",
data: "b="+consulta,
dataType: "html",
error: function(){
alert("Error petición ajax");
},
success: function(data){
$("#busqueda").empty();
$("#busqueda").append(data);
}
});
});
});
PD: "#busqueda" is my input. PD:“ #busqueda”是我的输入。
TablasController.php TablasController.php
public function buscarProductos(){
$em = $this->getDoctrine()->getManager();
$productos = $em->getRepository('ProyectoAdminBundle:Catalogo')->findAll;
return $this->render('AtajoBundle:Ajax:buscarProductos.html.twig', array('productos' => $productos));
}
buscarProductos.html.twig buscarProductos.html.twig
<?php
$buscar = $_POST['b'];
if(!empty($buscar)) {
buscar($buscar);
}
function buscar($b) {
$con = mysql_connect('localhost','root', 'root');
mysql_select_db('base_de_datos', $con);
$sql = mysql_query("SELECT * FROM anuncios WHERE nombre LIKE '%".$b."%'",$con);
$contar = mysql_num_rows($sql);
if($contar == 0){
echo "No se han encontrado resultados para '<b>".$b."</b>'.";
}else{
while($row=mysql_fetch_array($sql)){
$nombre = $row['nombre'];
$id = $row['id'];
echo $id." - ".$nombre."<br /><br />";
}
}
}
?>
My real problem is that when I call AJAX, this is going to look for the information to " buscarProductos.html.twig " throughout the controller. 我的真正问题是,当我调用AJAX时,它将在整个控制器中查找“ buscarProductos.html.twig”的信息。 The problem is that I can do it with php , (is more , in the example I did in php but I put .html.twig ) and the driver actually performs the query and passes it to the template.
问题是我可以用php来做到(在我在php中做的例子中更多,但我放了.html.twig),驱动程序实际上执行了查询并将其传递给模板。 I wonder if " SearchProducts " I have to do in twig and how, or if you have to do in php and as I do.
我想知道是否“ SearchProducts”是我必须在树枝上做的以及如何做,还是必须在php中做以及像我一样呢。 Forgive me if I do not understand is that I am Argentine and I can hardly communicate with you .
如果我不明白,请原谅我是阿根廷人,我几乎无法与您交流。 Thank you
谢谢
First of all, do not use mysql_connect
: it's deprecated; 首先, 不使用
mysql_connect
:这是已经过时; you should always use Doctrine. 您应该始终使用教义。
The AJAX call should call the url /products
which should be the GET
action buscarProductosAction
; AJAX调用应该调用url
/products
,它应该是GET
操作buscarProductosAction
; you do not need the buscarProductos.html.twig
view because you can do something like that inside the action: 您不需要
buscarProductos.html.twig
视图,因为您可以在操作中执行以下操作:
<?php
// Namespace definition...
use Symfony\Component\HttpFoundation\JsonResponse;
// Controller class definition...
public function buscarProductosAction(){
$em = $this->getDoctrine()->getManager();
$productos = $em->getRepository('ProyectoAdminBundle:Catalogo')->findAll;
return new JsonResponse(array('productos' => $productos));
}
Check out the official documentation about the JsonResponse class. 查看有关JsonResponse类的官方文档。
PS: Using php code inside a Twig file is totally wrong; PS:在Twig文件中使用php代码是完全错误的; you should read and learn the official documentation about templating .
您应该阅读并学习有关模板的官方文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.