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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:39:30+00:00 2026-05-31T02:39:30+00:00

I have a few lines of jQuery in my web application. This code is

  • 0

I have a few lines of jQuery in my web application. This code is inline at the moment because it accepts a couple of PHP variables.

<script type="text/javascript">

$(document).ready(function(){

    $('.post<?php echo $post->id; ?>').click(function() {

        $.ajax({

            type: 'POST',
            url: 'http://domain.com/ajax/add_love',
            data: { 
                post_id: <?php echo $post->id; ?>,
                user_id: <?php echo $active_user->id; ?>,
                <?php echo $token; ?>: '<?php echo $hash; ?>'

            },
            dataType: 'json',
            success: function(response) {

                $('.post<?php echo $post->id; ?>').html(response.total_loves).toggleClass('loved');

            }

        });

        return false;

    });

});

</script>

I’m a big fan of best practices though, so I would like to move my jQuery into an external JS file.

How could I achieve such a feat?

Any tips? I’m still relatively new to jQuery and PHP.

Thanks!

🙂

  • 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-31T02:39:31+00:00Added an answer on May 31, 2026 at 2:39 am

    My solution combines several techniques, some of which are already mentioned within the answers to this question.

    Yes, separate PHP from JS

    First of all: yes, you should separate your JS from PHP. You can put it in a separate file, but you will need to make some changes into your current code. Do not make JS file dynamically generated – it actually kills the advantages of separating JS code into separate file (you cannot cache it etc.).

    Common variables as global variables / function arguments

    Next, store your common variables as global variables in the header of HTML file (you do not really have much choice, although there are other alternatives), preferably grouped in one object:

    var Globals = <?php echo json_encode(array(
        'active_user_id' => $active_user->id,
        'token' => $token,
        'hash' => $hash,
    )); ?>;
    

    Alternatively you can pass all of them as argument(s) to the function you will call, but I assumed you are using them also in other places in your script.

    Container-dependent data stored in data- attributes

    Then use data- attributes for storing real data associated with containers. You won’t need post1/post2/post3-like classes and separate event handlers for them:

    <div data-post-id="10">here is something</div>
    

    instead of eg.:

    <div class="post10">here is something</div>
    

    How to read globals and data- attributes from external JS file

    And then your JavaScript may look like:

    $(document).ready(function(){
        $('[data-post-id]').click(function() {
            var el = $(this);
            var data = { 
                'post_id': el.data('post-id'),
                'user_id': Globals.active_user_id
            };
            data[Globals.token] = Globals.hash;
            $.ajax({
                'type': 'POST',
                'url': 'http://domain.com/ajax/add_love',
                'data': data,
                'dataType': 'json',
                'success': function(response) {
                    el.html(response.total_loves).toggleClass('loved');
                }
            });
            return false;
        });
    });
    

    And this should be sufficient (although I did not test it).

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

Sidebar

Related Questions

I have a few lines of PowerShell code that I would like to use
I have a text file with a few 1000 lines of text in it.
I have few question in this regard When you create an internet page, does
I have a unique issue - I am designing a web application that creates
I have a web app which replaces few img's with other img's. For example:
I have following code on my site: backgroundImages[bg_img_path_b]=new Image(); backgroundImages[bg_img_path_b].src = bg_img_path_b; backgroundImages[bg_img_path_b].loaded=loading; //jQuery(backgroundImages[lastImage]).unbind('load.backgroundImages');
I have a few child templates which have extraheads which include the jquery script.
I have a jquery.ajax routine that calls a php script. The php script does
How do I create subdomains in ASP.NET? I have few links on my homepage(Home.aspx)
I have a few links that should all open in the same window or

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.