【问题标题】:PHP file in .post function with symfony带有 symfony 的 .post 函数中的 PHP 文件
【发布时间】:2016-10-04 14:33:56
【问题描述】:

我是 symfony 的初学者,有一个问题。

我正在尝试从 thenewbostom 进行电子邮件验证。

在指南中他说您需要将 php 文件放在 .post 函数中。使用普通的 PHP 我可以很容易地做到这一点,但使用不同的地图结构,我不知道。

谁能给我一个正确的方向?

我的树枝(只有带有跨度的电子邮件输入)

<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');
}

【问题讨论】:

    标签: javascript php jquery symfony


    【解决方案1】:

    来自 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)...
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 2012-02-11
      相关资源
      最近更新 更多