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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:33:14+00:00 2026-05-22T16:33:14+00:00

My php generates some type from the DB and it is passed to a

  • 0

My php generates some type from the DB and it is passed to a Smarty Variable $X.

With the help of Jquery, I want to be able to click a button, and the content of one of my div will be be replace with $X.

$(document).ready(function(){
  $("button").click(function(){
    $("#div1").html($X);
   });
});

This piece of Jquery script is included in an external js file.

  • 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-22T16:33:15+00:00Added an answer on May 22, 2026 at 4:33 pm

    If you have a template file that is parsed where you can output PHP->Smarty assigned variables, something you could do is create a global JS variable in the template, and then use that global variable within your JS as normal.

    Such as:

    Template file

    <script type="text/javascript">
    var MyGlobalVar = "{$MyGlobalVar}";
    </script>
    

    Global.js file

    $(document).ready(function(){
        $("button").click(function(){
            if (MyGlobalVar != '') {
                $("#div1").html(MyGlobalVar);
            } else {
                alert('Error! Error! Abort!');
            }
        });
    });
    

    Note, you could output the Global.js file with it being parsed by Smarty (although… this is probably not a great idea) and inject your PHP->Smarty variables this way. This would treat the Global.js included file as a Smarty template.

    To do so, you would need to use {literal}, probably name the file with a .php file ending (so it was PHP-parseable), and add a PHP header() call so PHP outputs the file contents to the browser as a Javascript content-type.

    Global.js

    <?php
    
    header("content-type: text/javascript");
    
    ?>
    var MyGlobalVar = "{$MyGlobalVar}";
    {literal}
    $(document).ready(function(){
        $("button").click(function(){
            if (MyGlobalVar != '') {
                $("#div1").html(MyGlobalVar);
            } else {
                alert('Error! Error! Abort!');
            }
        });
    });
    {/literal}
    

    Additionally, on the PHP side, you might want to consider adding slashes to your variable, especially if the JS variable will contain html or other bits of text that will use single/double quotes.

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

Sidebar

Related Questions

Heres a simple php script that generates some XML: <?php // create doctype $dom
I have some PHP code that generates dynamic tables of data on the fly.
I have some PHP code that generates a calendar and then outputs html to
I'm reading in a plist from a web server, generated with some php. When
I use FPDF to generate some PDF from a PHP. It worked great for
Having some trouble sending properly formatted HTML e-mail from a PHP script. I am
Hello I have a jQuery function that will send to a php file some
Going from .Net to PHP because I want to explore the possibilities of the
I'm trying to export some tables generated with PHP from MySQL data. I'm trying
I'm trying to write an excel file from php, and I need that some

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.