Either move the code within the second document.ready block to the first one (which will make the a
variable accessible by your .tipsy()
call) or make the a
variable a global.
<script type="text/javascript">
$(document).ready(function () {
var a ="Login";
$("#login_form").submit(function () {
var formdata = $("#login_form").serializeArray();
$.ajax({
url: "ajax_login.php",
type: "post",
dataType: "json",
data: formdata,
success: function (data) {
if (data.livre === 'complete') {
a ="success";
} else
a = "Error";
}
});
return false;
});
$('.login_fields input[rel=tipsy]').tipsy({gravity: 'w', trigger: 'manual', fallback: a });
});
</script>
OR
<script type="text/javascript">
var a = "Login";
$(document).ready(function () {
$("#login_form").submit(function () {
var formdata = $("#login_form").serializeArray();
$.ajax({
url: "ajax_login.php",
type: "post",
dataType: "json",
data: formdata,
success: function (data) {
if (data.livre === 'complete') {
a ="success";
} else
a = "Error";
}
});
return false;
});
});
</script>
<script type='text/javascript'>
$(document).ready(function () {
$('.login_fields input[rel=tipsy]').tipsy({gravity: 'w', trigger: 'manual', fallback: a }); // a is not defined
});
</script>
Notice I removed the var
declaration from the success callback on your AJAX call.
UPDATE
To pass the updated value of a
to tipsy you will need to run the .tipsy()
call in your callback function. You could also run it as you are now and update the tipsy plugin in your callback function (however I am not familiar with the plugin and am not aware of how to do this):
<script type="text/javascript">
$(document).ready(function () {
var a ="Login";
$("#login_form").submit(function () {
var formdata = $("#login_form").serializeArray();
$.ajax({
url: "ajax_login.php",
type: "post",
dataType: "json",
data: formdata,
success: function (data) {
if (data.livre === 'complete') {
a ="success";
} else {
a = "Error";
}
$('.login_fields input[rel=tipsy]').tipsy({gravity: 'w', trigger: 'manual', fallback: a });
}
});
return false;
});
});
</script>