I'm at a loss here guys and I need your help.
I have a website where you can submit story urls to a database. When you click the submit button, a jquery post request is set calls my php which then returns a json object and inside there is a boolean to indicate if it was successful or not.
Then depending on the success of the php, the submit button uses some fancy css transforms to indicate to the user the status of their submission.
I would make a jfiddle for you guys to see but I'm not sure how to emulate the php sending back the request since this website is only on my local machine.
This is the jquery:
function applySubmitFeedback(activatedClass) {
$('#submit').click(function() {
var self = this;
$(this).addClass(activatedClass);
setTimeout(function() {
$(self).removeClass(activatedClass);
}, 1000);
});
}
$(document).ready(function() {
$('form').submit(function(event) {
event.preventDefault();
var $form = $(this),
term = $form.find("input[name='url']").val(),
url = $form.attr("action");
$.post( url, { story_url:term }, function(data) {
if (!data.success) {
console.log("failed");
if (typeof data.message == 'json' && data.message.hasOwnProperty("url"))
console.log(data.message.url);
else
console.log(data.message)
applySubmitFeedback('btn-error3d');
else {
console.log("success");
applySubmitFeedback('btn-success3d');
}
console.log(data);
}, "json");
});
});
And this is the fancy css:
.btn-8g {
background: #fff;
color: #999;
}
.btn-8g:active {
background: #fff;
}
.btn-8g:after,
.btn-8g:before {
text-transform: uppercase;
width: 100%;
height: 100%;
position: absolute;
left: 0;
line-height: 70px;
}
.btn-8g:after {
top: -98%; /* should be -100% but there's a gap in Chrome Version 34.0.1847.131 */
background: #7aca7c;
color: #358337;
content: 'It worked!';
-webkit-transform-origin: 0% 100%;
-webkit-transform: rotateX(90deg);
-moz-transform-origin: 0% 100%;
-moz-transform: rotateX(90deg);
-ms-transform-origin: 0% 100%;
-ms-transform: rotateX(90deg);
transform-origin: 0% 100%;
transform: rotateX(90deg);
}
.btn-8g:before {
top: 100%;
background: #e96a6a;
color: #a33a3a;
content: 'Error!';
font-weight: 700;
font-family: 'Lato', Calibri, Arial, sans-serif;
-webkit-transform-origin: 0% 0%;
-webkit-transform: rotateX(-90deg);
-moz-transform-origin: 0% 0%;
-moz-transform: rotateX(-90deg);
-ms-transform-origin: 0% 0%;
-ms-transform: rotateX(-90deg);
transform-origin: 0% 0%;
transform: rotateX(-90deg);
}
.btn-8g.btn-success3d {
background: #aaa;
-webkit-transform-origin: 50% 100%;
-webkit-transform: rotateX(-90deg) translateY(100%);
-moz-transform-origin: 50% 100%;
-moz-transform: rotateX(-90deg) translateY(100%);
-ms-transform-origin: 50% 100%;
-ms-transform: rotateX(-90deg) translateY(100%);
transform-origin: 50% 100%;
transform: rotateX(-90deg) translateY(100%);
}
.btn-8g.btn-error3d {
background: #aaa;
-webkit-transform-origin: 50% 0%;
-webkit-transform: rotateX(90deg) translateY(-100%);
-moz-transform-origin: 50% 0%;
-moz-transform: rotateX(90deg) translateY(-100%);
-ms-transform-origin: 50% 0%;
-ms-transform: rotateX(90deg) translateY(-100%);
transform-origin: 50% 0%;
transform: rotateX(90deg) translateY(-100%);
}
My problem is that the css seems to get stuck when I click the submit button the first time and doesn't activate until subsequent clicks. In addition, if the I send valid data first then on the second click the "It works" css fires.
However if I then send bad data, the "It works" css fires once more and then the "Error" data fires. After that no matter what I do it only fires the "Error" css even though I can verify in the console logs, and the chrome js debugger that it enters the else condition of the 'if(!data.success)' block, meaning it's supposed to fire the 'applySubmitFeedback('btn-success3d');' but it still fires as if it was an error.
It feels like there is a caching problem at work here or functions don't work in jquery like I thought they did. Please help me.