Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Passing php variables to javascript

Featured Replies

So i need to get the value of a php variable inside a javascript function that updates a div with content when called. Now i know u can just pass the variable as a parameter in the function but i thought as a global solution i would do this

 

 <?php if($pid){?>
 <script>
 var pid = <?php echo $pid;?>;
 </script>
 <?php }else{?>
 <script>
 var pid = '';
 </script>
 <?php }?>

 

and then i can use the value of pid in my ajax request within a seperate external javascript file. Is this an ok way to do this?

Not really sure why you have duplicated the script or indeed the JS code... You would be better using

<script>
var pid = <?php echo $pid ? $pid : "''"; ?>
</script>

 

That said, personally I would put any js variables into an array in php, then use json_encode() and set one variable to have all the array values in, such as

<?php

$vars = array(
'pid' => $pid
);

?>
<script>
var v = <?php echo json_encode($vars); ?>;
</script>

 

Then to call the variable for $pid in JS, you could just use

v.pid

This is a more convoluted method for the single variable, but if you're passing multiple, this becomes far more convenient and means you don't have to assign loads of variables in JS

Edited by Jay Gilford

  • Author

Not really sure why you have duplicated the script or indeed the JS code... You would be better using

<script>
var pid = <?php echo $pid ? $pid : "''"; ?>
</script>

 

That said, personally I would put any js variables into an array in php, then use json_encode() and set one variable to have all the array values in, such as

<?php

$vars = array(
'pid' => $pid
);

?>
<script>
var v = <?php echo json_encode($vars); ?>
</script>

 

Then to call the variable for $pid in JS, you could just use

v.pid

This is a more convoluted method for the single variable, but if you're passing multiple, this becomes far more convenient and means you don't have to assign loads of variables in JS

 

thank you your way does make more since :) ill try that

  • 2 weeks later...

Remember that PHP is a server-side scripting language, and Javascript resides on a client's browser. So, you could actually... Write directly any java-script you wish from your PHP script.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.