Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6615305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:26:44+00:00 2026-05-25T20:26:44+00:00

OK, I’m getting the results of a PHP form from JSON to do a

  • 0

OK, I’m getting the results of a PHP form from JSON to do a login validation. I want to check to see if their account is activated, which I do just fine. If it’s not I show a jQuery error but I want the ability to let them resend the activation email. I can pass the username password to the function displaying the error with JSON, but how do I then pass that data to a new function to process the new email? Here is what I have so far:

// LOGIN Validation

$(function(){
  $("#jq_login").submit(function(e){
     e.preventDefault();  

     $.post("widgets/login_process.php", $("#jq_login").serialize(),
       function(data){    
        if(data.all_check == 'invalid'){
          $('div.message_error').hide();
          $('div.message_success').hide();
          $('div.message_error').fadeIn();
          $('div.message_error').html(
            "<div>UserId and/or password incorrect. Try again.</div>"
          );

        } elseif(data.user_check == 'invalid'){
          $('div.message_error').hide();
          $('div.message_success').hide();
          $('div.message_error').fadeIn();
          $('div.message_error').html(
            "<div>UserId and/or password incorrect. Try again.</div>"
          );

        } elseif (data.activated_check == 'invalid'){
          $('div.message_error').hide();
          $('div.message_success').hide();
          $('div.message_error').fadeIn();
          $('div.message_error').html(
            "<div>Your account has not been activated. Please check your "  + 
            "email and follow the link to activate your account. Or click " +
            "<a href='#' id='resend'>here</a> to re-send the link.</div>"
          );

        } else {
          $('div.message_error').hide();
          $('div.message_success').fadeIn();
          $('div.message_success').html(
            "<div'>You are now logged in. Thank you </div>"
          );

          window.location.replace("producer.php");
          return false;
        }
      }, "json");
    });
  });             

  $(function(){
    $("#resend").live('click', function(event){
      event.preventDefault();
      alert(data.username);

      var data = 'username=' + data.username + 'password=' + data.password;

      $.ajax
    });                  
  });

I’m new so I don’t understand all the ins and outs of passing data back and forth.

thank you.

craig

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:26:45+00:00Added an answer on May 25, 2026 at 8:26 pm

    With Ajax there’s not really “passing data back and forth,” but rather just passing callbacks. That’s what you’re doing when you put function() { ... } as a function parameter–you’re creating a callback.

    I think the best course of action is to refactor this into several stand-alone functions. A good best practice is to make each function do one thing only, rather than defining functions within functions within functions.

    Once refactored, it becomes more clear how we can “reuse” the username and password for the resend-activation link.

    (function() { // to keep these functions out of the global scope(†)
      // this will be called when the login form is submitted
      function handleLogin(evt) {
        evt.preventDefault();
    
        // same as your code except that instead of creating a function here
        // we instead pass `handleLoginResponse`, which is a function we'll
        // define later
        $.post( 'widgets/login_process.php',
          $(this).serialize(), // <-- In events, `this` refers to the element that 
          handleLoginResponse, //     fired the event--in this case the form, so we
          'json'               //     don't need its id, we can just give `this`
        );                     //     to jQuery.
      }
    
      // This is the function we gave to $.post() above, and it'll be called when
      // the response is received.
      function handleLoginResponse(data) {
        // Here we decide what message to show based on the response, just like
        // in your code, but we call a function (showError or showSuccess) to
        // avoid repeating ourselves.
        if(data.all_check == 'invalid') {
          showError("UserId and/or password incorrect. Try again.");
    
        } else if(data.user_check == 'invalid') {
          showError("UserId and/or password incorrect. Try again.");
    
        } else if(data.activated_check == 'invalid') {
          showError("Your account has not been activated. Please check your " + 
                    "email and follow the link to activate your account. Or " +
                    "click <a href='#' id='resend'>here</a> to re-send the link."
          );
    
        } else {
          showSuccess("You are now logged in. Thank you.");
          redirectToLoggedInPage();
        }
      }
    
      // the function that shows error messages
      function showError(message) {
        $('.message_success').hide();
        $('.message_error').hide().            // jQuery chaining keeps things tidy
          html('<div>' + message + '</div>').
          fadeIn();
      }
    
      // the function that shows success messages
      function showSuccess(message) {
        $('div.message_error').hide();
        $('div.message_success').fadeIn().
          .html('<div>' + message '</div>');
      }
    
      // this method is called when the "resend" link is clicked
      function handleResendClicked(evt) {
        evt.preventDefault();
    
        // send another Ajax request to the script that handles resending, using
        // the form values as parameters
        $.get( './resend_activation.php',
          $('#jq_login').serialize(),
          handleResendResponse // again we've defined this function elsewhere
        );
      }
    
      // called when the Ajax request above gets a response
      function handleResendResponse(data) {
        // of course you can do whatever you want with `data` here
        alert('resend request responded with: ' + data);
      }
    
      // called from handleLoginResponse when the login is successful
      function redirectToLoggedInPage() {
        window.location = './producer.php';
      }
    
      // finally, our document.ready call
      $(function() {
        // pass the names of the functions defined above to the jQuery
        // event handlers
        $("#jq_login").submit(handleLogin);
        $("#resend").live('click', handleResendClicked);
      });
    }());
    

    Of course, you won’t always code like this–sometimes it really is best to just define an anonymous function() { ... } on the spot–but when things are getting nested three-levels deep this is a good way to untangle things and tends to make the way forward more clear.

    (†) Anonymous closures for limiting scope

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.