Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP触发jquery验证插件错误_Php_Jquery_Forms_Validation - Fatal编程技术网

使用PHP触发jquery验证插件错误

使用PHP触发jquery验证插件错误,php,jquery,forms,validation,Php,Jquery,Forms,Validation,我希望登录页面上的服务器端PHP验证在客户端jquery验证插件验证中触发错误。对于非登录表单(构造为使用远程文件作为操作),我只需回显消息,然后根据返回的值触发客户端验证错误/成功消息。我以前在这里收到过一些建议,但它们不起作用-一些语法错误(或我的无知)下面是一些伪代码: <?php $username = "foo"; $password = "bar"; if ($_POST['username'] != $username || $_POST['password'] != $pa

我希望登录页面上的服务器端PHP验证在客户端jquery验证插件验证中触发错误。对于非登录表单(构造为使用远程文件作为操作),我只需回显消息,然后根据返回的值触发客户端验证错误/成功消息。我以前在这里收到过一些建议,但它们不起作用-一些语法错误(或我的无知)下面是一些伪代码:

<?php
$username = "foo";
$password = "bar";
if ($_POST['username'] != $username || $_POST['password'] != $password) {
#would like to trigger the #errormsg here
?>
<!DOCTYPE>
<html>
<head></head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" >
</body>
</html>
<?php
}
else {
?>
<!DOCTYPE>
<html>
<head></head>
<body>Logged in user sees this content</body>
</html>
<?php
}
?>
目前,如果用户输入了错误的un/pwd,它只会重置表单,我想告诉他们输入的信息是错误的。我知道PHP可以输出JS—尝试在PHP中做一些类似的事情—但是无论我放在哪里,我都会得到一些语法错误,因为这些部分是如何分解的

<?php
$username = "foo";
$password = "bar";
if ($_POST['username'] != $username || $_POST['password'] != $password) {
<script>
   $(document).ready(function() {
      $("#Error").html('ERROR MSG HERE').show();
   });
</script>
?>


谢谢!

您只需要将PHP输出移动到正确的部分。在您没有doctype或
标记之前,您不能将
元素放入HTML页面。它必须进入
部分

<?php
$username = "foo";
$password = "bar";

if(!empty($_POST) && $_POST['username'] == $username && $_POST['password'] == $password) {

#display 'LOGGED IN' page here

} else {

   if(!empty($_POST) && ($_POST['username'] != $username || $_POST['password'] != $password)) {
    //Put the javascript error alert here
   }

  //Put the log in form here
}

我认为您应该使用Ajax表单提交和服务器端数据验证。
这是正确的方法,请参见下面的示例

HTML文件

$(函数(){
$('#登录表单')。提交(函数(){
$form=$(此项);
$message=$form.find('.message');
$.post($form.attr('action'),$form.serialize(),函数(数据){
if(data.error){
$message.html(data.message).removeClass('success').addClass('error');
}else if(data.success){
$message.html(data.message).removeClass('error').addClass('success');
}
}“json”);
返回false;
});
});

PHP文件


据我所知,这是正确的方法。

你在哪里尝试回显JS?我在上面的代码中没有看到这一部分。编辑了答案-我只是在PHP中插入了底部部分,我想在这里触发#errormsg。如果我尝试在顶部插入并回显,它只会在页面加载时打印出来。thx这里一定是我的设置有问题-当我尝试在您建议的位置添加警报时-它加载到页面加载中。我需要JS函数仅在登录尝试不正确时启动…您是否检查登录尝试是否不正确?您应该执行
打印($\u POST);
并检查值。您收到该警报的事实表明,post值与您定义的值不匹配。检查它,un foo pwd bar-我有打印,它只是在页面加载时打印出来…不知何故,我搞砸了!如果我输入了正确的用户名和密码,我会按照您的预期登录。您保留JS吗有错误登录的警告吗?哦,好的,非常简单的修复方法。只需将您的第一个
if
块更改为此。
if(!empty($\u POST)&($\u POST['username']!=$username |$\u POST['password']!=$password)){
您在页面加载时收到警告,因为当有人第一次访问时,没有$\u POST['username']。因此,警报会触发,因为NULL!=$username。太棒了!我确实在其他区域运行AJAX表单提交,这只是一个简单的登录,没有数据库检查等。我正在努力保持这一简单和可管理性,我只想在从同一页面登录后向客户端显示他们的项目。我会尝试使用您的建议重新编写,谢谢!
<?php
$username = "foo";
$password = "bar";

if(!empty($_POST) && $_POST['username'] == $username && $_POST['password'] == $password) {

#display 'LOGGED IN' page here

} else {

   if(!empty($_POST) && ($_POST['username'] != $username || $_POST['password'] != $password)) {
    //Put the javascript error alert here
   }

  //Put the log in form here
}
<html>
<head>
    <script type="text/javscript" src="latest-jquery.js"></script>
    <script type="text/javscript">

        $(function(){

            $('#login-form').submit(function(){

                $form = $(this);
                $message = $form.find('.message');

                $.post($form.attr('action'), $form.serialize(), function(data){

                    if( data.error ) {

                        $message.html( data.message ).removeClass('success').addClass('error');

                    } else if( data.success ) {

                        $message.html( data.message ).removeClass('error').addClass('success');

                    }

                }, 'json');

                return false;
            });

        });

    </script>
</head>
<body>
    <form id="login-form" action="PATH_TO_PHP_FILE" method="post">
        <div class="message"></div>
        <input type="text" name="username" />
        <input type="password" name="password" />
        <input type="submit" value="Login Now !" />
    </form>
</body>
<?php
$username = 'foo';
$password = 'bar';

$response = array('error' => TRUE, 'message' => "Invalid Credentials");

if ( count($_POST['username']) && isset($_POST['username']) && isset($_POST['password']) && $_POST['username'] == $username && $_POST['password'] == $password  ) {

    $response = array('error' => FALSE, 'success' => TRUE, 'message' => "Bingo you have done it !");

}

print json_encode($response); //PHP function to convert array to JSON
exit; //To resolve IE Bug
?>