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 8582783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:22:46+00:00 2026-06-11T21:22:46+00:00

So I’ve been digging online and found that a jQuery AJAX solution will be

  • 0

So I’ve been digging online and found that a jQuery AJAX solution will be best for what I want to do.

I’ve found jquery.ajax() and jquery.post() being mentioned the most.

However the info I’ve found is extremely comprehensive, and honestly, I’m a bit lost!

I need to create a simple button, that will act as a toggle switch. It needs to POST data to a PHP script that will process it and return true, or false. The button should display set, or unset accordingly.

Here’s the snag, the result is dependent also on a database lookup returning true or false, and I need to get information from the $_SESSION securely. Since javascript is client side, i’m not keen on outputting this data into something the user can access. My question being, can I access the session from within the “called” PHP file, and it still work correctly (like an include?).

Here’s the best basic code that I’ve found, however not secure and may be incorrect..

$("#mydiv").click(function(){

var button = $(this) ;

if (!button.hasClass('active')) {
    $.post("ajax.php", { user: "2", action: "start" },
        function(data){
           button.addClass('active');
           button.html("End") ;
        }
    );

}
else {
    $.post("ajax.php", { user: "2", action: "stop" },
        function(data){
           button.removeClass('active');
           button.html("Start") ;
        }
    );
}


});
  • 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-06-11T21:22:47+00:00Added an answer on June 11, 2026 at 9:22 pm

    The session variables will be available in the php scripts you call. Note that the $.post()-function is asynchronous and has a callback parameter, in your case called data. This callback variable will store whatever you echo in the php script that was called by $.post().

    Now, you can validate stuff and do whatever you like (btw, validations should always happen in your php files, never in javascript, for security reasons!).

    One idea would be to echo out "true" or "false" in your php files after validation and then exiting the php script, such as:

    <?php
        //validate here and store success in boolean $success
        if($success==true){ echo "true"; exit; }
        else{ echo "false"; exit; }
    ?>
    

    Then, your javascript callback parameter data will have the value of either "true" or "false". You can simply access this variable like any other variable in javascript, and do stuff depending on its value.

    One solution might be:

    $("#mydiv").click(function(){
    
        var button = $(this) ;
    
        if (!button.hasClass('active')) {
            $.post("ajax.php", { user: "2", action: "start" },
                function(data){
                    if(data=="true"){
                        button.addClass('active');
                        button.html("End");
                    }
                }
            );
        }
        else {
            $.post("ajax.php", { user: "2", action: "stop" },
               function(data){
                    if(data=="true"){
                        button.removeClass('active');
                        button.html("Start");
                    }
               }
            );
        }
    
    
    });
    

    That should do the trick. If you need to return several data points, e.g. an object with attributes, check JSON.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
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've got a string that has curly quotes in it. I'd like to replace

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.