/    Sign up×
Community /Pin to ProfileBookmark

How to hide a div after a jquery/ajax request to a php file?

Hi,

I’m creating a login form and what I am trying to do is that when the form was successfull, it will hide. If there were errors, it should not hide.

login_proc.php

“`
if(mysqli_num_rows($result) > 0) { // When this if statement is getting called, it should hide a certain div
$class = “success”;
$title = “Success!”;
$msg = “Correct Password!”;
} else {
$class = “danger”;
$title = “Error!”;
$msg = “Incorrect Password!”;
}
“`

I can use this to hide the form but I have no idea how to achieve what Im trying: login-form.style.display = “none”;

This is my jquery/ajax code:

“`
$(‘.btn-login’).click(function (e) {
e.preventDefault();
var mail = $(‘#mail’).val();
var password = $(‘#password’).val();
loader.style.display = “block”;
setTimeout(function() {
$.ajax
({
type: “POST”,
url: “bin/login_proc.php”,
data: { “mail”: mail, “password”: password },
success: function (data) {
$(‘.result’).html(data);
$(‘#logform’)[0].reset();
loader.style.display = “none”;
// If login was succesful, hide the div… But how?
}
});
}, 1000);
});
“`

Any help would be appreciated

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 18.2020 — Transmit a status to the client and hide the form based on it:
if(mysqli_num_rows($result) > 0) { // When this if statement is getting called, it should hide a certain div
$class = "success";
$title = "Success!";
$msg = "Correct Password!";
$error = false;
} else {
$class = "danger";
$title = "Error!";
$msg = "Incorrect Password!";
$error = true;
}

and later on include it in the result being echoed:
echo json_encode(['msg'=>$msg, 'error'=>$error]);
Then you can hide the form based on it:
$.ajax
({
type: "POST",
url: "bin/login_proc.php",
data: { "mail": mail, "password": password },
dataType: "json", // <-- add this
success: function (data) {
$('.result').html(data.msg); // <-- change this
$('#logform')[0].reset();
loader.style.display = "none";
// If login was succesful, hide the div... But how?
// add this:
if (!data.error) {
$('#logform').hide();
}
});

(not tested)
×

Success!

Help @uwrp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...