jquery - What is the new proper way to pass a variable between Ajax code and a PHP file?
one text
Solution:
Well $_POST will always be set, but might be empty. A better check would be
if ([] !== $_POST) { ...
Another trick you might consider uses the Null coalescing operator. The following to would result in better error checking. Please refer to the manual.
$uname = $_POST['uname'] ?? false;
$pwd = $_POST['pwd'] ??= false;
You will use $_POST in you day to day, and if you get error just from using it consider it a false positive. Moving you cursor to the underlined text in IntelliJ and pressing option + enter
will pull up a menu with possible fixes. Sometimes even auto fix the issues for you.. Note that even the auto fixes can be wrong. Use with caution.
As far as passing variables to JS I try to stay out of injection territory. Such being when you add js code from a php runtime and directly pass $variables
into that code. This is dirty, but will get you by while just learning. Taking principles from react programming, using asynchronous calls with Ajax and JSON encoding/decoding is typically the way to go. You want all the information needed on the initial request to be present in the initial payload. Thus, the Ajax-all-the-time method is no good. For that initial data payload just set a js const equal to a php json_encoded string.
While it's not relevant, it's worth noting in PHP using arrays will be much faster than objects for manipulating data. This seems to be uncommon knowledge.
Source