ludvik120 ludvik120 - 20 days ago 5
PHP Question

How to pass checbox value via 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);
})
}
});

Answer

I would suggest using this:

jQuery

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

PHP

if((isset($zapametat))&&($zapametat==true)){
Comments