I'm currently developing a Facebook game using html and Javascript. Right now I have two modals on a html page (ModalA and ModalB), I’m trying to attach both of them to one button, but only one of the modals should appear when the button is clicked. The modal that appears is decided by the result of a check on my database.
Whenever a user clicks on the button, I want to run a check on my database using a Javascript function that sends the user’s Facebook ID by AJAX to my database. This checks a certain value in the database and then sends back either “0” or “1” as the response to the AJAX call.
If the response sent back is “0” then I want modal A to appear on the webpage, however if the response is “1” then I want modal B to appear.
I know AJAX is asynchronous, so if I understand this correctly, I need to use something like e.preventDefault();
to make it wait for the response from the database?
Right now I can make a modal appear by making the button’s a href = the modal’s address, like this:
<td align="center"><a href="#openModalA"><img src="button.png" width="100" height="50"></a></td>
But this code is located directly in the source code of my page, I'm unsure how to set the html "a href" address dynamically using a Javascript function.
The addresses of my two modals are: #openModalA and #openModalB. (the code for these modals is located in the source code for the html page they appear on).
This is my html button:
<td align="center"><a href= ??? ><img src="button.png" width="100" height="50"></a></td>
And this is my current Javascript with AJAX:
function openModal() {
//getting user's Facebook ID
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
if (response.error) {
console.log('Error - ' + response.error.message);
}
else {
var userid = response.id;
e.preventDefault();
$.ajax({
url: 'scripts/checkmodal.php',
data: {'userid' : userid},
type: "POST",
success: function(response){
if(response=="0"){
window.location.href = $link.attr('#openModalA');
}
else{
window.location.href = $link.attr('#openModalB');
console.log(response);
}
}
});
}
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
}
I don't know how to link this Javascript to the html button's address, or if I'm doing something very fundamental wrong.
Any help with this would be really appreciated, thanks in advance!
UPDATE
This is the code I currently have:
Button in html:
<td align="center"><a id="yourAnchor" href><img src="button.png" width="100" height="50" onClick=openModal();></a></td>
Javascript:
function openModal() {
//getting user's Facebook ID
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
if (response.error) {
console.log('Error - ' + response.error.message);
}
else {
var userid = response.id;
var $link = $('#yourAnchor');
if($.ajax({
url: 'scripts/checkmodal.php',
data: {'userid' : userid},
type: "POST",
async: false
}).responseText == 0){
window.location.href = $link.attr('#openModalA');
}
else{
window.location.href = $link.attr('#openModalB');
console.log(response);
}
};
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
}