I have this jquery code for hiding Ad box after clicking on link in:
$('a.close').click(function () {
var parent = $(this).parent().parent();
var adid = $(parent).attr('id');
$.ajax({
type: "POST",
url: "./setcookie.php",
data: "adid=" + adid,
cache: false,
dataType: 'json',
success: function (cook) {
if (cook.set === 'success') {
parent.fadeOut(1000);
}
},
error: function () {
$(".guestwarn").html("<font color='red'>There was an error submitting the form. Please try again.</font>").fadeIn(1000);
}
});
return false;
});
So I would like by means of cookies close box. My html(php) is:
<?php if(isset($_COOKIE[ "msg1"])){?>
<div id="msg1" class="msgbox">
<div class="guestwarn">
<a href="#" class="close">X</a>
Something goes here...
</div>
</div>
<?php } ?>
So my working setcookie.php is:
$adcookies = array();
$value = $_POST['adid'];
if ($value) {
setcookie($value, $value, time() + 3600);
if (isset($_COOKIE[$value])) {
$adcookies['set'] = "success";
} else {
$adcookies['set'] = "error";
}
echo json_encode($adcookies);
}
Why adbox (.msgbox) hides after two clicks? What is wrong with the codes? Thank You
I would bet on the cookies. If you set a cookie it will be 'visible' to you after you refresh the page (in your case second AJAX request), so that's why you have to click twice. You can solve it using sessions instead of cookies.