dongshan3759 2016-11-13 18:43
浏览 72
已采纳

如何通过AJAX,PHP传递复选框值?

I have a simple login form with checbox remember me. What i want to achieve is when user checked checbox than create cookies else start session but it still start session even I checked checkbox. Can anyone help ?

This is my html:

<form action="PHP/login.php" method="post" class="loginForm">

                <div class="form_vkope">
                    <input type="text" name="prihlNick" placeholder="Nick" class="prihlNick" />
                    <img src="Obrazky/ios7-person.png" alt="Ikonka postavy">                    
                </div>

                <div class="form_vkope">
                    <input type="password" name="prihlHeslo" placeholder="Heslo" class="prihlHeslo" />
                    <img src="Obrazky/locked.png" alt="Ikonka zámok">                   
                </div>


                <div class="obal_submitov">
                    <input type="checkbox" name="zapametat" class="zamapetat" /><label for="zapametat"><span></span>Remember me</label>
                    <input type="submit" name="prihlasit" value="Login" class="prihlasit" />                    
                </div>
            </form>

part of my PHP:

$zapametat = $_POST['zapametat']; //checkbox
 if ($dbNick == $Snick AND $dbHeslo == $Sheslo) {
        if ($ban != 1) {
            if (isset($zapametat)) {
                setcookie('id',$dbId,time()+86400, '/');
                setcookie('nick',$dbNick,time()+86400, '/');
                echo "cookie";
            }else{
                $_SESSION['id'] = $dbId;
                $_SESSION['nick'] = $dbNick;
                echo "session";
            }
        }else{
            echo "Tvoj účet bol zablokovaný";
        }
    }else{
        echo "Heslo alebo meno sa nezhoduje";
    }
}

And my jQuery:

$('.prihlasit').click(function() {
    var prihlNick = $('.prihlNick').val();
    var prihlHeslo = $('.prihlHeslo').val();
    var prihlasit = $('.loginForm .prihlasit');
    var checbox = $('.zapametat'); //checkbox
    var data = 'prihlNick='+prihlNick+'&prihlHeslo='+prihlHeslo+'&zapametat='+checbox+'&prihlasit='+prihlasit;
    if (prihlNick == '' || prihlHeslo == '') {
        $('.loginForm :input').addClass('inputError');
    }else{
        $('.loginForm :input').removeClass('inputError');
        $.ajax({
            url: 'PHP/login.php',
            type: 'POST',
            data: data ,
        })
        .done(function(data) {
            $('.vypis_chyba').html(data);
            console.log(data);
        })
    }
});
  • 写回答

1条回答 默认 最新

  • douniao8687 2016-11-13 18:53
    关注

    I would suggest using this:

    jQuery

    var checbox = $('.zapametat').prop('checked'); //checkbox (jQuery 1.6+)
    

    PHP

    if((isset($zapametat))&&($zapametat==true)){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化