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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:45:00+00:00 2026-06-01T01:45:00+00:00

How do I pass a variable in a php file that is loaded into

  • 0

How do I pass a variable in a php file that is loaded into a page (DOM) to a jQuery function??

Iv’e tried various method’s while searching online but I haven’t figured out how to use them correctly.

I need the var navHeaderTitle to be passes to the jQuery load() callback function so it sets the HTML tag, #navHeaderTitle, to the variable called in the php file.

Thnx for you help.

php:

<?php
$con = mysql_connect("localhost","user","pw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM some_list");

$navHeaderTitle = "MY NEW TITLE";//<--I NEED 2 INJECT THIS!!

while($row = mysql_fetch_array($result))
{
echo "<div id='navItem' title='$navHeaderTitle'>";
echo "<h1>" . $row['label'] . "</h1>";
echo "<h2>" . $row['title'] . "</h2>";
echo "<p>" . $row['description'] . "</p>";
echo "</div>";
}
mysql_close($con);
?>

JavaScript in the HTML Head:

<script type="text/javascript">

    var navHeaderTitle = '';

    $(document).ready(  
        function() {
            $("#navContent").load('http://url/my_list.php', function() {
            $('#navHeaderTitle').text($(html).find('div#navItem').attr('title'));//<--GET THE VAR FROM LOADED PHP FILE!!
            }); 
        });
</script>
<body>
<div id="navPanel">

            <div id="navHeader">
                <img src="images/ic_return.png" style="float: left;"/>
                <img  id="listSortBtn" src="images/ic_list_sort.png" style="float: right;"/>
                <h4 id="navHeaderTitle"></h4>//THIS IS WHAT NEEDS THE VAR DATA!!
            </div>

            <div id="navScrollContainer" class="navContentPosition">
                <div id="navContent">HTML CONTENT from PHP GETS DUMPED IN HERE</div>
            </div>

        </div>
</body>

Ive tried using this but not sure how to:

$.get('scripts/my_list.php', {}, function(data){ 
                   data = split(':'); 
                })
  • 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-01T01:45:01+00:00Added an answer on June 1, 2026 at 1:45 am

    replace echo("$navHeaderTitle"); with

    echo("<script> var navHeaderTitle = $navHeaderTitle </script>");
    

    and remove var navHeaderTitle = ''; from the <head> script..

    that will setup a JS variable like you’re using, but you have to do that before the code in the <head> loads…

    EDIT

    ok don’t echo("$navHeaderTitle"); you can put it into the HTML like:

    echo "<div id='navItem' title='$navHeaderTitle'>";
    

    then in the JS you can do:

    <script type="text/javascript">
        var navHeaderTitle = '';
    
        $(document).ready(  
            function() {
                $("#navContent").load('http://url/my_list.php', function(response) {
                    $('#navHeaderTitle').text($(response).attr('title'));
                }); 
            });
    </script>
    

    here’s a jsfiddle demo: http://jsfiddle.net/JKirchartz/hdBzF/ (it’s using fiddle’s /echo/html/ so the load has some extra stuff to emulate the ajax)

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

Sidebar

Related Questions

Is it possible to pass a variable to a linked .js file? I tried
I have a variable inside my php file, which I want to pass to
So how can I pass a variable into a script that is not connected
how to pass variable from zf to javascript/jquery? thanks
I want to pass a variable to he buttonEvent method in the selector. [button
I want to pass a variable as text or somehow to make above function
I need to pass a variable to a javascript function,but I got a little
I have a page that includes/embeds a file that contains a number of functions.
How can I pass multiple variables between PHP files using jquery with json? Ex:
I've been working on a small page in PHP, one that doesn't need the

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.