我是 symfony 的初学者,有一个问题。
我正在尝试从 thenewbostom 进行电子邮件验证。
在指南中,他说您需要将 php 文件放入 .post 函数中。使用普通的 PHP,我可以轻松完成此操作,但使用不同的映射结构,我不知道。
有人能给我正确的方向吗?
我的twig(仅包含跨度的电子邮件输入)
<input id="autocomplete" type="text" name="email" class="autocomplete validate">
<span id="email_feedback"></span>
我的js(我在twig文件和post函数中链接的)
function validate_email (email) {
$.post(/*php file is supoosed to be here*/,{ email:email}, function (data) {
$('#email_feedback').text(data);
});
}
$('#autocomplete').focusin(function () {
if ($('#autocomplete').val() === '') {
$('#email_feedback').text('Type een geldige email');
} else {
validate_email($('#autocomplete').val());
}
}).blur(function () {
$('#email_feedback').text('');
}).keyup(function () {
validate_email($('#autocomplete').val());
});
最后一个我的 php 文件
/**
* @Route("/")
*/
public function indexAction()
{
if (isset($_POST['email'])) {
$email = $_POST['email'];
echo $email;
}
return $this->render('BontekoeCinemaBundle:Default:index.html.twig');
}
最佳答案
来自 JQuery API 文档:jQuery.post( url [, data ] [, success ] [, dataType ] )
,所以基本上你想写
function validate_email (email) {
$.post('http://myhost.com/myRoute1', { email:email}, function(data) {
$('#email_feedback').text(data);
});
}
然后你需要实现路由
/**
* @Route("/")
*/
public function indexAction()
{
if (isset($_POST['email'])) {
$email = $_POST['email'];
echo $email;
}
return $this->render('BontekoeCinemaBundle:Default:index.html.twig');
}
/**
* @Route("/myRoute1")
*/
public function autoCompleteEmailAction()
{
// get parameter(s)...
// search in database...
// return result(s)...
}
关于javascript - 使用 symfony 的 .post 函数中的 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39855044/