gpt4 book ai didi

javascript - 如何在服务器响应后在 Symfony2 中显示警报或信息消息而不重新加载页面

转载 作者:行者123 更新时间:2023-12-01 02:19:59 24 4
gpt4 key购买 nike

我正在使用 Symfony2 为配镜师构建一个管理应用程序。当管理员将新客户添加到数据库时,我的 Controller 会检查客户名称是否重复。我想显示一个弹出对话框,询问用户是否想要添加新客户。我怎样才能实现这个?我应该使用 Ajax 吗?以下是我在本例中使用的 Controller 的示例代码:

public function nouveauAction(Request $request)
{
$form = $this->createFormBuilder()
->add('nom','text')
->add('tel','text', array('label' => 'Nº de téléphone', 'data' => '06'))
->add('email','email', array('label' => 'E-mail', 'required' => false))
->add('date','date', array('label' => 'Date d\'ajout', 'data' => new \DateTime()))
->add('ajouter','submit')
->getForm()
;

$form->handleRequest($request);

if ($form->isValid()){

$client = new Client();
$client->setNomClient($form["nom"]->getData());
$client->setTelClient($form["tel"]->getData());
$client->setEmailClient($form["email"]->getData());
$client->setDateEditionClient($form["date"]->getData());
//just for now (Later we'll retrieve the username from the session)
$em = $this->getDoctrine()->getEntityManager();
$user = (new Utilisateur)->rechercherParPseudo($em, 'admin');
$client->setIdUtilisateur($user);

$em = $this->getDoctrine()->getEntityManager();

if($client->existe($em))
{
//I need a popup message here : The customer you are trying to add already exists""
}

else
{
$request = $this->container->get('request');
if($client->existeNomDouble($em)) //If the customer name is duplicate
{
//I need a popup message here with Yes/No buttons...
}
else
{
//Writing to the database:
$em = $this->getDoctrine()->getEntityManager();
$client->ajouterClient($em);

//A notification to fade in here : "Customer successfully added"
}


}

}

return $this->render('ClientBundle:Client:nouveau.html.twig', array(
'formAjouter' => $form->createView(),
));
}

最佳答案

试试这个:

Controller 端:

 $this->get('session')->getFlashBag()->add(
'notice',
'Customer Added!'
);

View 侧面( Twig ):

{% for flashMessage in app.session.flashbag.get('notice') %}

<div class="alert alert-success">
{{ flashMessage }}
</div>

{% endfor %}

关于javascript - 如何在服务器响应后在 Symfony2 中显示警报或信息消息而不重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25628545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com