提交到PHP之前检查表单 [英] Validate form before submitting to php

查看:136
本文介绍了提交到PHP之前检查表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我实现一个登录页面,一切都是从PHP端工作正常,但现在我会实现使用jQuery多了一个功能:我想检查登录的域是空的,如果不超过打印东西页面,否则继续使用PHP,那怎么办?

下面是我的HTML code部分:

 <形式ID =登录表单行动=登录-exec.phpNAME =登录表单的方法=后级=UI体UI体,一UI的角落的所有数据AJAX =假>
    <字段集>
        < D​​IV数据角色=fieldcontain>
            <标签类=UI输入文本为=登录>登录:LT; /标签>
            <输入名称=登陆类型=文字级=文本框ID =密码/>
        < / DIV>
        < D​​IV数据角色=fieldcontain>
            <标签类=UI输入文本为=密码>密码:LT; /标签>
            <输入名称=密码类型=密码级=文本框ID =密码/>
        < / DIV>
        <输入类型=提交名称=提交值=登陆/>
    < /字段集>
< /形式GT;
 

解决方案

绑定将事件提交表单(注:一个提交按钮而不是点击)。这就是你的code去,检查的形式,并在必要时,被张贴停止。

  $('#登录表单)。在('提交',函数(E){
    如果($('#登录)。VAL()==''|| $('#密码)。VAL()==''){
        即preventDefault();
        //警报('填写在这两个领域');
        //您可以使用警报或对话,但我会去在页面上留言
        $('#ERRORMSG)文本(在这两个领域填写)显示()。
    }其他{
        //什么也不做,让窗体发布
    }
});
 

如果您preFER显示一个消息不是使用一个对话框,地方上的页面,preferably附近的提交按钮添加ERRORMSG DIV

 &其中,P n =ERRORMSG的风格=显示:无;>< / P>
 

Hello I'm implementing a login page everything is working fine from the PHP side but now I would implement one more function using jQuery: I would like to check if the fields of the login are empty and if not than print something on the page, otherwise continue with php, how to do that?

Here is part of my HTML code:

<form id="loginForm" action="login-exec.php" name="loginForm" method="post" class="ui-body ui-body-a ui-corner-all" data-ajax="false">
    <fieldset>
        <div data-role="fieldcontain">
            <label class="ui-input-text" for="login">Login:</label>
            <input name="login" type="text" class="textfield" id="login" />
        </div>                                  
        <div data-role="fieldcontain">                                      
            <label class="ui-input-text" for="password">Password:</label>
            <input name="password" type="password" class="textfield" id="password"/> 
        </div>
        <input type="submit" name="Submit" value="Login" />
    </fieldset>
</form>     

解决方案

Bind an event to the submission of the form (Note: NOT the click of a submit button). That's where your code goes that checks the form, and if necessary, stops it from being posted.

$('#loginForm').on('submit', function(e){
    if ($('#login').val()=='' || $('#password').val()==''){
        e.preventDefault();
        // alert('Fill in both fields');
        // You can use an alert or a dialog, but I'd go for a message on the page
        $('#errormsg').text('Fill in both fields').show();
    }else{
        // do nothing and let the form post
    }
});

If you prefer to show a message than use a dialog, somewhere on the page, preferably near the Submit button add the errormsg div

<p id="errormsg" style="display:none;"></p>

这篇关于提交到PHP之前检查表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆