Saturday, 16 September 2017

How to assign a returned Javascript function to a PHP variable and use in an SQL Query?



I use Javascript to get 'member_id' url parameter and need to then assign that value to a PHP variable that I use in an SQL Query. When I simply echo the PHP variable, everything looks good but if I try to include the PHP variable in the SQL query that I submit via POST, I get an error with the SQL Query.



    


document.write(getQueryVariable('member_id'));
";
?>


SQL Error: *You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'text/javascript'> document.write(getQueryVariable('member_id')); ')' at line 1*


Answer




This is because PHP is run on the server, and your JS executes on the client. Just as an aside, you can do things the other way, so in JS:






This is possible because the PHP executes on the server before the JS executes on the client. If however you want to send a JS value to a PHP script, then you will need to do it after the DOM has loaded, usually via an AJAX call.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...