将php var传递给jQuery函数

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(':'); 
                })

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)

I would have the php file return a json object that contains two parts, the html you want to echo and the title you want to use.

Then I would use jQuery's .ajax() function instead of .load() to get the return value from your php script in a javascript variable instead of dumping it directly as .load() does.

It would be cleaner to pass the var in a custom attribute (data-var), then fetch it width JQuery

$(some_element).attr("data-var");

I hate to mess my JS code with php.