PHP for Beginners course: ➤ https://davehollingworth.net/phpy
PHP MVC course: ► https://davehollingworth.net/phpmvcy
CodeIgniter 4 course: ► https://davehollingworth.net/codeigni...
If you have a variable in PHP, and you need to use its value in JavaScript in the browser, how do you get the value of this variable from PHP to JavaScript?
In this video we'll look at various solutions to achieve this, from writing out the value into the JavaScript or DOM in the original request, making sure we escape it, to using a cookie, and using Ajax. We'll also look at their advantages and disadvantages.
Which method you choose depends on how much data there is, and how complex your page is: whether it's a simple PHP script or a more complex web applications.
Code shown in the video:
https://gist.github.com/daveh/56fd2fa...
00:00 Intro
00:17 The problem
01:21 Simple echo
03:11 Escaping
05:00 JSON encoding
06:10 Hidden field
07:58 Meta tag
09:04 Data attribute
10:30 Cookie
13:20 Ajax request
16:14 Summary