passing a variable to a JSON object

Problems with the Windows version of XAMPP, questions, comments, and anything related.

passing a variable to a JSON object

Postby MikeSiencyn » 28. May 2021 17:19

Hi folks
I want to pass the value of a php variable to an existing JSON object.
I have tried the code:
<script type="text/javascript">
var length='<?php echo $length; ?>';
jsonobject.import({
locationId: 'abcdefghij’,
height: 'length',
onResponse: function(type, body) {
console.log('Type', type);
console.log('Body', body);
}
});
</script>
The process works if I merely type a particular value e.g. 1.7 i.e. height: '1.7',
but I need to transfer the value of the php variable $length not type a particular value.
I know I’m probably being a bit of a dingus but could someone show me how to do this
please?
Many thanks
Mike
MikeSiencyn
 
Posts: 24
Joined: 02. February 2021 15:41
XAMPP version: 3.2.4
Operating System: Windows10

Re: passing a variable to a JSON object

Postby Nobbie » 28. May 2021 19:26

First of all, you have to understand how processing and data flow is working.

Most important is the understanding, that PHP is running far earlier than JavaScript, these are running in a totally different scope and there is no connection between these. That is what you MUST understand. Normally, a webserver is a remote PC somewhere in the world, but not your desktop PC running your browser. Think of Google, think of this forum - these things are running on remote computers, not on your computer.

PHP is executed by the webserver, actually Apache sends it to the PHP interpreter (which must be installed on the webserver). After PHP has finished, Apache sends the resulting output to your browser (via TCPIP). So, when your browser receives the HTML file which contains the JavaScript commands, PHP has already finished and there is no PHP anymore. Not on your PC. All PHP coding like "echo $variable" is replaced by the output of the statement already on the webserver.

Last not least, your browser receives the resulting HTML and JavaScript, which does not contain any PHP source. And then your browser executes the JavaScript.

And now think about, how data flow is running in your application and how you can have values from PHP variables in your JavaScript. I dont say, that doesnt work. But surely it works differently as you may believe. I actually dont know what you are trying to do, you did not give us a proper description, but as already said, you MUST have an idea, how PHP is working and how JavaScript is working. And why it may be difficult to have PHP variables in your JavaScript Code. I hope you get it properly, if not, do not hesitate to ask. It is most important that you get full knowledge about the realisation of PHP and JavaScript. These do not run at the same time and not in the same scope.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: passing a variable to a JSON object

Postby MikeSiencyn » 28. May 2021 19:48

Thanks, Nobbie
That explanation was really clear; I was definitely not clear in my mind about where various processes happen.
What you said gave me an idea of what to try. Basically I needed to start and end my php code at different points and write the code accordingly.
Thanks again
All the best
Mike
MikeSiencyn
 
Posts: 24
Joined: 02. February 2021 15:41
XAMPP version: 3.2.4
Operating System: Windows10


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 108 guests