Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created May 31, 2023 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/8756b4db352bb545694ea487115d644b to your computer and use it in GitHub Desktop.
Save code-boxx/8756b4db352bb545694ea487115d644b to your computer and use it in GitHub Desktop.
Pass PHP Variables To Javascript

PASS PHP VARIABLES TO JAVASCRIPT

https://code-boxx.com/pass-php-variables-arrays-to-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!-- (A) PHP VARIABLE -->
<?php $phpvar = "Foo"; ?>
<!-- (B) USE PHP TO ECHO JAVASCRIPT CODE -->
<script>
<?php echo "var jsvar = '$phpvar';"; ?>
console.log(jsvar);
</script>
<!-- (A) PHP VARIABLE -->
<?php $phpvar = "Foo"; ?>
<!-- (B) PHP SHORT ECHO TAG TO ECHO VARIABLE -->
<script>
var jsvar = "<?=$phpvar?>";
console.log(jsvar);
</script>
<!DOCTYPE html>
<html>
<head>
<title>PHP Var Into Form</title>
</head>
<body>
<!-- (A) PHP VARIABLE -->
<?php $phpvar = "Foo"; ?>
<!-- (B) INSERT INTO HTML FORM -->
<form onsubmit="return demo()">
<input type="text" id="myTxt" value="<?=$phpvar?>">
<input type="submit" value="Go">
</form>
<!-- (C) JS GET FIELD VALUE -->
<script>
function demo () {
// (C1) GET FIELD VALUE
var jsvar = document.getElementById("myTxt").value;
console.log(jsvar);
// (C2) PREVENT FORM SUBMIT
return false;
}
</script>
</body>
</html>
<?php
// (A) PHP CONVERT ARRAY TO STRING
$phpvar = json_encode(["Apple", "Banana", "Cherry"]);
?>
<!-- (B) JAVASCRIPT CONVERT STRING TO ARRAY -->
<script>
var jsvar = JSON.parse('<?=$phpvar?>');
console.log(jsvar);
</script>
<!DOCTYPE html>
<html>
<head>
<title>Simple AJAX</title>
</head>
<body>
<!-- (A) AJAX FETCH DATA FROM SERVER -->
<script>
function doAJAX () {
fetch("4b-ajax.php")
.then(res => res.text())
.then(data => console.log(data));
}
</script>
<!-- (B) GO! -->
<input type="button" value="AJAX Fetch" onclick="doAJAX()">
</body>
</html>
<?php
$phpvar = "Foo";
echo $phpvar;
<!DOCTYPE html>
<html>
<head>
<title>Simple AJAX JSON</title>
</head>
<body>
<!-- (A) AJAX FETCH DATA FROM SERVER -->
<script>
function doAJAX () {
fetch("5b-ajax.php")
.then(res => res.json())
.then(data => console.log(data));
}
</script>
<!-- (B) GO! -->
<input type="button" value="AJAX Fetch" onclick="doAJAX()">
</body>
</html>
<?php
echo json_encode(["Apple", "Banana", "Cherry"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment