Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using PHP variables with JavaScript
#1
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:

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

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 .


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

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.
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  How to Open Popup using javascript tonnygrand 2 45 01-17-2017, 01:33 AM
Last Post: ITSGOLIVE
  Client-side JavaScript being.chinmay 0 38 09-19-2016, 09:34 PM
Last Post: being.chinmay
  Naming Your Variables being.chinmay 1 44 08-04-2016, 05:52 PM
Last Post: khushbu123
  What are signed variables? Clarke 2 66 05-18-2016, 08:52 PM
Last Post: Martinricky
  What is the default value of the local variables? being.chinmay 0 96 04-01-2016, 11:12 PM
Last Post: being.chinmay
  what is bind() method in javascript? Brianvoges 3 170 03-14-2016, 12:11 AM
Last Post: har.sinha
  How can JavaScript be used to personalize or tailor a Web site to fit individual user being.chinmay 0 77 02-08-2016, 10:27 PM
Last Post: being.chinmay
  What can javascript programs do? being.chinmay 0 54 02-08-2016, 10:13 PM
Last Post: being.chinmay

Forum Jump:


Users browsing this thread: 1 Guest(s)