Hi i need the help of how to access the PHP files values from the External JavaScript. I have read we should keep Header("content-type: application/x-javascript"); this line in PHP. I have done everything, if any one can explain this with Example code and please explain how to use the value in External JavaScript

Thanks in advance

could you explain it better? i didn't got it...
do you want to access some PHP $variables inside the .js file?

How about including a PHP file which contains the javascript you wish to be able to access the PHP variables. Then in this included file pass the PHP variables to the script. You can not alter a PHP variable from javascript, but this should suffice. Here's an example:

index.php:

<?
//Set some vars that JS can access
$something = "Hello Javascript!";
$daniweb = "awesome";
$scriptby = "PhpMyCoder";
?>
<html>
<head>
<title>PHP Vars in JS Example</title>
<?php require_once("js.php"); ?>
</head>
<body onload="phpVars();">
</body>
</html>

js.php:

<?php
//Add the javascript vars with the script tag
echo "<script type='text/javascript'>\n" +
        "something = " . $something . ";\n" +
        "daniweb = " . $daniweb . ";\n" +
        "scriptby = " . $scriptby . ";\n" +
        "</script>\n";

//Now either attach an external JS file
// echo "<script type='text/javascript' src='external.js'></script>";
//Or add the Javascript to this file
echo "<script type='text/javascript'>\n" +
        " function phpVars() { alert(something + ' Daniweb is ' + daniweb + '! Script coded by ' + scriptby + '.') }\n" +
       "</script>\n";
?>

Hi,

My advice is to put the code in the javascript file into a funtion and then call the function from your php file with your parameters.

Include the javascript file somewhere, best is in the head element:

<script language="javascript" src="myscript.js"></script>

And call your javascript function, lets say at the end of php file:

<script language="javascript">
myfunction(<? echo $var1.', '.$var2.',  '.$var3; ?>)
</script>

-----------------------------------------------------------------------------------
Bodi Andras
http://www.nowdev.ro

printf(" Hai Friend Furutani,Flash Creations & bodiandras thank U for Ur Reply \n");

printf("Actaully I need to acces the PHP variable from the External javascript without using any post or Get method in PHP \n");

printf(" FlahCreation, can U explain about the External javascript part from ur , becoz that may useful to me \n");

printf("bodiandras i have tried ur suggestions, do u have any other idea plz \n ");

printf("Furutani u aslo help me out ");

Dear SJai... I guess you need to find out another solution...

You said that you need to load some GET element inside an external js, like this: <script src="myjs.js?var=<?=$_GET?>"></script>?

I guess it's not possible at all...

But if someone find how to do this, I would glad to know it too.

Hi i need the help of how to access the PHP files values from the External JavaScript. I have read we should keep Header("content-type: application/x-javascript"); this line in PHP. I have done everything, if any one can explain this with Example code and please explain how to use the value in External JavaScript

Thanks in advance

<?php
if(mysql_num_rows($result)>0)
     {

header( 'Location: [url]http://www.google.com/talk/service/badge/Show?tk=z01q6amlqsm3bm8e92tdj8s0qserjve55bmmd7jruu3r7hm983lhsiuifsiu768u4lll2o4o0tsq0qvtr7d7ss7210303hh2t7f5sqvv09iijkto6mhichv6vltobiik6ma073hv5q0sh77le2pj36i67armp9ce34g8b48hs[/url]') ;

       }



     else
     {
        $sql = "INSERT INTO user_livechat (username,email) VALUES
                ('$name','$email')";
                 $wpdb->query($sql);

                header( 'Location: [url]http://www.google.com/talk/service/badge/Show?tk=z01q6amlqsm3bm8e92tdj8s0qserjve55bmmd7jruu3r7hm983lhsiuifsiu768u4lll2o4o0tsq0qvtr7d7ss7210303hh2t7f5sqvv09iijkto6mhichv6vltobiik6ma073hv5q0sh77le2pj36i67armp9ce34g8b48hs[/url]') ;



    }
     }
?>
Member Avatar for diafol

IMO: FlashCreations has answered your query.

You could make your js file a php file (a bit extreme) where all the js contained therein is echoed as text, and the

<script src="my.js"></script>

is replaced by

<?php include('my.js.php');?>

Just a thought. I wouldn't do this myself, but if you're not satisfied with FlashCreations and bodiandras, you've got few other avenues to explore.

Dear furutani i will try some other way, if i find it's my pleasure to remind U,

Friend ganesh_tavva you only mentioned the php file code. But i need to access the PHP values inside the EXTERNAL JAVASCRIPT,

ardav i couldn't understand ur concept, anyhave i will try ur idea and
THANKS FOR ALL THE PEOPLE WHO ARE ALL GIVEN THE REPONSE T O MY DOUBTS

Sure no problem. The solution I provided doesn't require $_GET or $_POST , nor does bodiandras'. Regarding mine, all you have to do is echo the variables from PHP into a script tag. Before you echo the </head> tag, simply output a script tag. Inside it declare some javascript variables and set their values to the values of the PHP variables like so:

<!doctype>
<html>
 <head>
  <?php
    $shared = "hey javascript!"; //A variable to be shared
    //Here we are writing some JavaScript that declares the variables from PHP
    echo "<script type='text/javascript'>\n";
    echo "var shared = '".$shared."'"; //Here I wrote javascript code to set the javascript variable shared equal to the PHP variable
    echo "</script>";
  ?>
 </head>
 <body onload="alert(shared);">
 </body>
</html>

Try this code. It should work. The only thing to note is PHP is server-side so it runs before the javascript which is run on the user's browser. Because of this, PHP can pass its values to JavaScript, but JavaScript cannot change the value in PHP. This is not because of a restriction, but because of the fact that the PHP will have completed execution once the JavaScript begins to run.

IMO: FlashCreations has answered your query.

Thanks ardav! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.