简体   繁体   English

从php获取数据到JavaScript [验证电子邮件]

[英]Get data from php to JavaScript [validation email]

How can I get data from PHP to Javascript ? 如何从PHP到Javascript获取数据?
I hava an error message generated by a PHP script, and I want to get the message into JavaScript to display the results of email validation. 我有一个由PHP脚本生成的错误消息,我想将该消息导入JavaScript以显示电子邮件验证的结果。

I finished the error validation script for the case when the input form is empty, 输入表单为空时,我完成了错误验证脚本,
and now I am confused about how to get a success message if the input form is not empty and email validation is successful. 现在我很困惑如果输入表单不为空且电子邮件验证成功时如何获取成功消息。

Can anybody help me ? 有谁能够帮助我 ?

This is my PHP Code: 这是我的PHP代码:

{"error":1,"info":[
{"fieldId":"contact-form-name","message":"Please enter your name."},
{"fieldId":"contact-form-mail","message":"Please enter valid e-mail."},
{"fieldId":"contact-form-message","message":"Please enter your message."}]}

and this is my JavaScript code : 这是我的JavaScript代码:

;(function($,doc,win) {
"use strict";

var contactForm=function(object,option)
{
    /**********************************************************************/

    var $self=this;
    var $this=$(object);
    var $option=option;

    /**********************************************************************/

    this.build=function() 
    {
        $this.bind('submit',function(e) 
        {
            e.preventDefault();
            $self.submit();
        });
    };

    /**********************************************************************/

    this.submit=function()
    {
        this.blockForm(true);
        <!--$.post('plugin/contact-form/contact-form.php',$this.serialize(),this.processResponse,'json');-->            
        $.post('plugin/contact-form/contact-form.php',$this.serialize(),this.processResponse,'json');           
    };

    /**********************************************************************/

    this.processResponse=function(response)
    {
        $self.blockForm(false);
        $this.find('li').qtip('destroy');

        var error=false;

        if(typeof(response.info)!='undefined')
        {   
            if(response.info.length)
            {   
                for(var key in response.info)
                {
                    error=error || response.error;

                    $('#'+response.info[key].fieldId).parents('li:first').qtip(
                    {
                        show        :   
                        { 
                            target  :   $(this) 
                        },
                        style       :   
                        { 
                            classes :   (response.error==1 ? 'template-qtip template-qtip-error' : 'template-qtip template-qtip-success')
                        },
                        content     :   
                        { 
                            text    :   response.info[key].message 
                        },
                        position    :   
                        { 
                            my      :   'bottom center',
                            at      :   'top center' 
                        }
                    }).qtip('show');    
                }
            }
        }

        if(!error) 
        {
            $this.find('input[type="text"],textarea').val('').blur();
            window.setTimeout(function() 
            { 
                $('#contact-form-submit').qtip('destroy'); 
            },2000);
        }           
    };

    /**********************************************************************/

    this.blockForm=function(block)
    {
        if(block) $this.find('li').block({message:false,overlayCSS:{opacity:'0.3'}});
        else $this.find('li').unblock();            
    };

    /**********************************************************************/
}

/**************************************************************************/

$.fn.contactForm=function(option) 
{
    return this.each(function() 
    {
        var object=new contactForm(this,option);
        object.build();

        return(object);
    });
};

})(jQuery,document,window);

and this is my form : 这是我的表格:

<form name="contact-form" id="contact-form" method="post">

                    <ul class="template-reset-list template-clear-fix">

                        <li>
                            <label for="contact-form-name" class="template-infield">Name (required)</label>
                            <input type="text" name="contact-form-name" id="contact-form-name"/>
                        </li>
                        <li>
                            <label for="contact-form-mail" class="template-infield">E-mail (required)</label>
                            <input type="text" name="contact-form-mail" id="contact-form-mail"/>
                        </li>
                        <li>
                            <label for="contact-form-website" class="template-infield">Websiste</label>
                            <input type="text" name="contact-form-website" id="contact-form-website"/>
                        </li>
                        <li>
                            <label for="contact-form-subject" class="template-infield">Subject</label>
                            <input type="text" name="contact-form-subject" id="contact-form-subject"/>
                        </li>
                        <li>
                            <label for="contact-form-message" class="template-infield">Message (required)</label>
                            <textarea name="contact-form-message" id="contact-form-message"></textarea>
                        </li>
                        <li>
                            <label for="contact-form-submit" class="template-infield"></label>
                            <input type="submit" name="contact-form-submit" id="contact-form-submit" value="Submit"/>
                        </li>

                    </ul>

                </form>

I think you are trying to grab a copyrighted template through preview. 我认为您正在尝试通过预览获取受版权保护的模板。 Support a developer by purchasing template - you will not only get a correct templating files, php plugins, but also technical support from developer himself. 通过购买模板来支持开发人员-您不仅会获得正确的模板文件,php插件,而且还会得到开发人员本人的技术支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM