0

I want to put javascript code to php variable, after that save it to mysql database and after that echo it on page. I have problem with save javascript code to php variable and echo it on page.

I tried to save javascript code to php variable this way but got errors:

$parameter = "<script language='JavaScript'>document.write(geoip_country_name());</script>";

For echo this javascript code from database I was thinking to use that way:

$parameterecho = htmlentities($parameter);
echo $parameterecho; 

Is there any better solution to echo code to page and how to save code to php variable?

zokopog
  • 67
  • 1
  • 7
  • So its not possible to dynamically insert javascript to page? – zokopog Dec 27 '12 at 12:20
  • 1
    @zokopog what errors are you getting?? – Sibu Dec 27 '12 at 12:21
  • @RohitKumarChoudhary yes you can do that, if it's from the backend of the application. When in a plain PHP-webpage you can't. OT: What errors are you getting? This doesn't seem that strange to me. – CE_REAL Dec 27 '12 at 12:21
  • 1
    Notice: Trying to get property of non-object in AND Fatal error: Call to a member function fetch_assoc() on a non-object in – zokopog Dec 27 '12 at 12:22
  • @CE_REAL if using ajax then its posible – Rohit Choudhary Dec 27 '12 at 12:22
  • If you get such specific errors, use the search!! http://stackoverflow.com/search?q=Call+to+a+member+function+fetch_assoc%28%29+on+a+non-object – deceze Dec 27 '12 at 12:24
  • @zokopog the error that you are getting has nothing to do with assigning javascript to php variable, i guess its your php error. can you show more code of yours – Sibu Dec 27 '12 at 12:24
  • 1
    http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – Michael Berkowski Dec 27 '12 at 12:26
  • @RohitKumarChoudhary why are you first saying otherwise then? – CE_REAL Dec 27 '12 at 12:38

3 Answers3

2

I think you can achieve what you want with AJAX. Here a mockup using jQuery:

$.post(
        save_parameter.php,
        {
            parameter : geoip_country_name()
        });

file save_parameter.php
// save parameter to DB

$.get(
        get_parameter.php,
        function(parameter) {
            alert(parameter);
        }, 'html');

file get_parameter.php
// get $parameter from DB
// echo  $parameter
// exit;

UPDATE:

You can load/run javascript dynamically with jQuery like this:

$.getScript("have_javascript_code.php");

FINALLY:

But seems that you are talking about save/use more than javascript values. There is no problem if you really want to do that. Although I recommend you re-think your approach and take advantage of the previous snippets.

save code:

$parameter   = "<script language='JavaScript'>document.write(geoip_country_name());</script>";
$code        = htmlspecialchars($parameter, ENT_QUOTES);
// insert to DB

ouptut code:

// get $code from DB
$code = htmlspecialchars_decode($code, ENT_QUOTES);
echo <<<EOF
<script type="text/javascript">
$code
</script>
EOF;
Igor Parra
  • 9,600
  • 10
  • 66
  • 93
1

Javascript and PHP ARE NOT THE SAME. You're thinking, duh! obviously but I mean Javascript is a "client side" technology while PHP is a "server side" technology.

Your Javascript can read from PHP variables only because first the .php page is processed on the server and then output returned. Hence any variables will be output into the JS.

However, the reverse is NOT true. Your Javascript does not have access to any PHP information as it resides on the server. The only way you can make JS variables accessible to PHP is to make POST or GET back to the server (call the .php again) with the required variables+values.

code_fish
  • 3,132
  • 5
  • 44
  • 84
sandip
  • 3,161
  • 5
  • 27
  • 52
1

PHP runs on server so that code will be executed on server and when the page is loaded first the entire PHP will be alrady executed and your $parameter will be undefined since javascript is runs on client side it will start after page is loaded or after php is executed more likely.

Sivagopal Manpragada
  • 1,282
  • 12
  • 32