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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:59:57+00:00 2026-06-05T06:59:57+00:00

I have a php application that makes use of a Listener class, which basically

  • 0

I have a php application that makes use of a Listener class, which basically just sets up an ajax request with jquery (example below). But for some reason, echoing the javascript just seems inelegant. Is it better practice to build a singleton class for the javascript to be passed to (which could introduce coupling) or to just echo the script like I’m doing now?

Here’s a code snippet of what I’m doing.

<?php

        $script = "
                    <script>
                    $(document).ready(function(){
                        $('".$trigger."').".$action."(function() {
                        ".$js_variables."
                            var ajax_load = '';
                            var loadUrl = '".$this->controller_path."';
                            var action = $(this).attr('id');

                            $('".$this->parent."')
                            .hide(2)
                            .html(ajax_load)  
                            .load(loadUrl, {action: action, ".$post_mapper."})
                            .fadeIn(800);
                        });
                    });
                </script>
        ";

      echo $script;

?>

Edit: Using a singleton class would also allow me to use $(document).ready() or the shortcut version $(function(){}) only once instead of every single time I add a listener. But I’m not sure if this is worth the extra time and effort… Any ideas?

  • 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-05T06:59:58+00:00Added an answer on June 5, 2026 at 6:59 am

    mm it’s all preference… I generally would do it like this so my text editor would be able to determine what’s javascript and what’s PHP so it wouldn’t make my eyes bleed trying to look at non-code-colored text.

    <script>
        $(document).ready(function(){
            $('<?php echo $trigger ?>').<? echo $action ?>(function() {
                <?php echo $js_variables ?>
                var ajax_load = '';
                var loadUrl = '<?php echo $this->controller_path ?>';
                var action = $(this).attr('id');
    
                $('<?php echo $this->parent ?>')
                    .hide(2)
                    .html(ajax_load)  
                    .load(loadUrl, {action: action, <?php echo $post_mapper ?>})
                    .fadeIn(800);
                });
        });
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP application that makes extensive use of Javascript on the client
I have written a web app in PHP which makes use of Ajax requests
I have a Zend Framework application that is making use of jQuery UI. In
I have a php application that is installed on several servers and all of
I have a PHP application that sends email using the pear Mail function. Unfortunately
I have a PHP application that is built around the MVC architecture without any
I have a php web application that uses big cookies to store a lot
We have a PHP/MYSQL application that collects user input, including special characters like ø,ü,ñ,
I have written a PHP application that uses Objects heavily. Added, deleting, updating etc..
I have a PHP based application that I need to work on both a

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.