Boby Boby - 6 months ago 16
Javascript Question

Onchange didn't show or hide Div

I've a problem with my Script, please take a look.

My Javascript

<script type="text/javascript">
$(document).ready(function()
{
$("#datetimepicker_mask2").change(function()
{
var tanggalnya = $("#datetimepicker_mask2").val();
$.ajax({
type: "POST",
url: "http://localhost:84/helpdesk2/forgot_password/check_date/"+tanggalnya,
success: function(msgtgl){
if(msgtgl === 'benar' ){
console.log(msgtgl);
$('#form_email').show();
}
else
{
console.log(msgtgl);
$('#form_email').hide();;
}
}
});
return false;
});
});
</script>


My HTML

<div class="login-box-body">
<div id="form_email">
<form action="http://localhost:84/helpdesk2/forgot_password/check_date/" method="post">
<div class="form-group has-feedback">
<input class="form-control" id="email" placeholder="email@domain.com" name="email" />
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
</div>
<div id="hasiltanggal"></div>
</div>


my PHP

function check_date($tanggalnya)
{
extract(PopulateForm());
$nipnya = $this->session->userdata('nip');
$hasil = $this->db->query("SELECT BirthDate from Employee where Nip = '".$nipnya."'")->row();
if(indo_date($hasil->BirthDate)==indo_date($tanggalnya))
{
echo "benar";
}
else
{
echo "salah";
}
}


note:
populateform()
is one of my helper that handle the input.

I can't show my
form_email
. I wonder why, because in
console.log(msgtgl)
i receive
benar
. So, the
form_email
should be showed up, right?

Answer

I have had similar issues with comparing returned data - for some reason I had to apply trim() to it before comparing the value, try the following:

if(msgtgl.trim() === 'benar' ){...

or

 var test=msgtgl.trim();
if(test=== 'benar' ){...
Comments