Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using PHP variables with JavaScript
You can use PHP variables with JavaScript the same way you do with HTML.You can add the variable to the JavaScript code. For example, the JavaScript example in the preceding section can include a PHP variable, as follows:

$string = “This page last updated: “;
echo “<script language=\”JavaScript\”>
+ document.lastModified + ‘<br>’)
// -->

The JavaScript language itself uses variables. You can set a JavaScript variable to the value of a PHP variable to use in your JavaScript code. For example, the JavaScript could be sent as follows .

$string = “This page last updated: “;
echo “<script language=\”JavaScript\”>
var message = \”$string\”;
document.write( message
+ document.lastModified + ‘<br>’)
// -->

Because JavaScript code is not executed until it reaches the browser on the client side, passing values from JavaScript variables to PHP variables can’t take place in the current page. The JavaScript value must be passed on to the next PHP script before PHP can receive it. JavaScript can pass the values on so that PHP can use them by adding them to the URL, by storing them in a cookie that PHP can read, or by sending them as a form element.

Possibly Related Threads...
Thread Author Replies Views Last Post
  What is called as negative infinity in JavaScript? sulbha 5 55 05-15-2017, 07:06 PM
Last Post: sin123
  How Does PHP Updates by Your Web Host Impact your WordPress Sites Garrysmith 1 26 05-07-2017, 11:40 PM
Last Post: tonnygrand
  How to get difference between two or more arrays in PHP? tonnygrand 1 19 04-19-2017, 07:41 PM
Last Post: Davidwatson
  What is PHP Frameworks ? parisahu13 10 69 03-16-2017, 12:56 AM
Last Post: RGBpro
  How to submit PHP contact form to an email tonnygrand 2 34 02-16-2017, 12:21 AM
Last Post: raynowenhall
  How to Open Popup using javascript tonnygrand 2 74 01-17-2017, 01:33 AM
  Live Chat API SDK for PHP Websites hudsonlane 1 260 10-24-2016, 06:57 AM
Last Post: Aryansao
  Client-side JavaScript being.chinmay 0 51 09-19-2016, 09:34 PM
Last Post: being.chinmay
  What is the difference between require() and include() in PHP? Brianvoges 4 116 09-12-2016, 05:13 PM
Last Post: Training Surat
  Will php7 be the last version of php tonnygrand 1 41 08-30-2016, 11:38 PM
Last Post: webkhazana

Forum Jump:

Users browsing this thread: 1 Guest(s)