php - Javascript HTML button click not working -


I have created a login system using HTML, Javascript, Ajax and PHP.

I am trying to implement the logout function, what I have here:

First there is a div in my HTML, which is not logged on to the site Welcomes anyone:

  & lt; Div id = "welcome" style = "postian: absolute; top: 0; float: true;" & Gt; Welcome to SIK! & Lt; / Div & gt;   

I have a JavaScript function, which is called when a user is logged in, or modifies the div in a specific user to say hi, and shows a logout button .

  function show_logged_in (success) {var is_logged_in = success; Var datastring = {"reg": invalid, "login": invalid, "is_logged_in": is_logged_in}; $ .jax ({type: "post", url: "php / class.ajax.php", data: datastring, datatype: 'jesnon', success: function (user name) {warning ("user shown") ; User = ('# welcome'). Html ('& lt; p & gt; Hi' + user + ' 

& lt; button id = "logout_but" Type = "button" & gt; logout & lt; / button & gt; & lt; / p & gt; ');}, error: function () {warning (error in "show_logged_in"}}}}); }

And then when the user clicks logout, then my main JS file contains a function:

  $ ("# logout_but") . Ui.logout);   

Here my main JS file is completely:

  $ (document) .ready (function () {// ------ - - Login / Register Gui -------------- $ ('# load-new') Hide (); $ ('# reg') Hide (); $ ( '# Login'). Hide (); // GUI class var ui = New GUI (); // If login_but is clicked click do ui.login function $ ('# login_but'). (Ui.login ); // If clicked on reg_but, click the ui.regin_ajax function $ ("#.") Then click the login_sumbit button (ui.register); // ui.register function $ ('# reg_but'). Login_submit ") click (ui. Login_ajax); $ (" # reg_submit ") click Click (ui.register_ajax); $ ("# logout_but"). Click (ui.logout);});   

So you can now view call orders, and there is also a click function called for the logout button:

  this.logout = function ( ) {Warning ("Clicked"); Var datastring = {"is_logged_out": invalid}; $ .jax ({type: "post", url: "php / class.logout.php", data: data string, datatype: 'JSON', success: function (success) {warning ("user is logged out"); $ ('# Welcome'). Html (Welcome to SIK & lt; / p & gt; ');}, Error: Action () {Warning (' Error in logging out ');}}}} < / Code>  

As you can see, at the top of the previous work, I have warned that the function has actually been posted to me.

My problem then Is there When I do not have any logout button, I do not see the warning, so the problem is on the click.

Any help with this situation will be great.

That's because you create a logout button dynamically and then click the handler to attach itself to the element in its main JS file As a result, the handler must not have #logout_but element And so nothing is attached.

To fix this, you can use a delegate instead of one handler:

Change it: < / P>

  $ ("#logout_but"). Click (ui.logout); With   

  $ ("# welcome"). ("Click", "#logout_but", ui.logout);    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -