How to maintain a PHP object through successives AJAX calls - javascript

I am programming a php page for controlling a serial port using some in-screen-buttons. When I press a button, this sends some data through the serial port and it shows its response. All this is working OK.
Now, I need to listen continuously the serial port in order to receive data that I haven't ask for. I'm trying to do it with a timer in javascript, but every time I execute the PHP code through AJAX, serial port is restarted and its buffer is flushed. I need to pass the object that controls the serial port to the javascript code before finish the execution of the PHP code. And when pressing a button, I need to pass this object to the new PHP code.
How can I do this?
Thanks a lot

First create a global js variable in your code.
When you first make ajax call then store response in your js variable.
so everytime you need to pass that global js variable through ajax.
You can make window level js global variable using
window.myVariable="";
make sure to define this variable before sending any js request

Check out: http://php.net/manual/en/language.oop5.serialization.php
You should pass the serialized object or, to keep the Serial Object alive between sessions, store it in a text file. On the AJAX end, store it as a Global [WINDOW] variable. Such as
window.serialphpobject = data;
then on the next AJAX call use this data
{object: window.serialphpobject}
All for example obviously, without seeing your code I cannot fit it exactly.

I need to pass the object that controls the serial port to the javascript code before finish the execution of the PHP code. And when pressing a button, I need to pass this object to the new PHP code.
This solution would be vulnerable. You should keep the information in a PHP session.
http://php.net/manual/en/session.examples.basic.php

I've found that this is imposible to be done: when the PHP code finish its execution, the serial port is closed automatically, so I lost all the data received untill I open the port again.

Related

How can I exchange data between nodejs and puppeteer?

I made a nodejs application that starts from index.js.
Then, index.js launches puppeteer and injects bot.js on a headless-api page by addscripttag function.
I made index.js sets a cookie for conveying initial values before injecting javascript, but I need more common way to exchange data.
I thought two ways; the first is using cookie, and the second is networking via socket connection.
Is there other way for send and receive data between index.js(node) and puppeteer(headless chrome)?
First, puppeteer IS nodejs side application, so they have a single environment and you don't need to "send" anything. Just pass data around as you'd do in any other JS code. I assume you want to transfer data between page and nodejs then.
To pass data from nodejs to page use page.evaluate. You can call any code in page context, ranging from simply setting some variables to directly calling whatever functions with necessary arguments.
To initiate transfer from page side to nodejs, first register a nodejs-side callback function with page.exposeFunction and then call it from page code and it will be executed in nodejs context. Just like in previous case, everything else depends on code of that function. It can be as simple as storing whatever argument you pass to it in some variable or directly perform with data pretty much whatever you want.

Is it possible to use a javascript variable inside of a javascript variable that contains php?

I'm trying to use a JS variable inside of php code that executes a function. Based on the selected radio button, Javascript gets a string of code from a data attribute of the radio button (trimcode) and I need to set this as the key in my PHP function where one of the variables requires an array key.
The result will be, whenever a radio button is selected, the PHP function inside this function will execute using the 'code' variable and thus return it's return value as the inside content of the "byo" class div via javascript innerhtml. However, I can't figure out how to use a javascript variable inside of PHP code that is inside another javascript variable. Can someone point me in the right direction?
<script type='text/javascript'>
$(document).ready(function(){
var code;
var colorsavailable;
$("input:radio[name=trimlevel]").click(function() {
code = $(this).data("trimcode");
colorsavailable = ("<?=byoTrimImageColors($modelMap["+code+"],$hexcolors)?>");
$('#byo').html(colorsavailable);
});
});
});
</script>
I think you're confusing the notions of server-side code and client-side code.
The PHP code runs once, in its entirety, when the page is requested. Then, after it's executed, the resulting page is sent to the browser where the JavaScript code runs. And that line of PHP code isn't going to execute for each click event. It's going to execute once, and only once, and emit only one result to the page.
Given what it looks like you're trying to do, you probably have two options:
Include the functionality you're invoking in JavaScript code and just invoke it there instead of in PHP code. Whatever that array is, whatever that function is, implement those in JavaScript. Then there's no need to involve PHP at all.
If the functionality needs to be in server-side code, then you're going to need to use AJAX to invoke it. Essentially you'd create a separate PHP "page" (which emits JSON data, not an actual page) to accept the value, invoke the functionality, and emit the result. The JavaScript code would make an AJAX request to that resource and use the result.
Client-side, that AJAX call might look something like this:
code = $(this).data("trimcode");
$.post('someNewPage.php', { code : code }, function (result) {
$('#byo').html(result);
});
Server-side (and I'm mostly guessing here), someNewPage.php might have something like:
$code = $_POST['code'];
// you'll want to sanitize the $code value here, since it's user input
$result = byoTrimImageColors($modelMap[$code],$hexcolors);
echo json_encode($result);
Your idea is not possible since for PHP code to execute it requires a page refresh. The best solution would be using Ajax to send the data to PHP asynchronously and changing your HTML, using JS, with the data that comes back from PHP.
To learn how Ajax/PHP works visit this site: http://www.w3schools.com/php/php_ajax_php.asp.
It is not possible to call a PHP function from Javascript. PHP is a pre-processor and ALL PHP code will finish executing before the page loads. Once the page loads, the browser will load your Javascript files and JS will take it from there.
In order to "call a PHP function" again after the page has loaded, you will need to make a new request to the page. This is typically handled in Javascript via AJAX. Using ajax, Javascript can send a new request to the server, PHP will read it and send a response back to Javascript.
If you really need that PHP function to execute on the Javascript level synchronously, then consider porting that function to Javascript.
Right now what you are doing is constructing a string that resembles some PHP code. It looks like you are doing that part just fine.
However, you cannot execute PHP code on the client side. There is no interpreter available on the client (the browser) that knows what to do with your PHP code.
What I would recommend doing is if you need to run some PHP code, create a controller method or some script that you can execute on the server, make an AJAX request to that (passing the JS code variable), have the PHP method return the value of your byoTrimImageColors method, and then handle that data however you please.

PHP/AJAX Requests: session_start(): Cannot send sesssion cache limiter - headers already sent

I have a problem which I can not find a solution to:
After a mouse keypress (onclick="") in my web application, I call a PHP function via AJAX request. This is nothing unusual. However, the PHP function needs to access some session variables. As I call the PHP function via HTML5 onclick and AJAX request, the webpage is already rendered, which probably means the headers have already been sent.
Probably, as a result of that, I get the following error:
session_start(): Cannot send session cache limiter - headers already sent
The key information here is that my PHP function called through AJAX Request does not output anything to the browser before calling session_start().
Is there any solution to this, please? Originally, I thought, AJAX function calls are independent from rendering the webpage and I could basically call session_start() at any time via AJAX. But now, it looks to me, that I can not call session_start() from my PHP function if called with AJAX request, just because all the AJAX calls are performed after rendering the webpage and thus after sending the headers?
Client Side: HTML5, JavaScript (no jquery)
Server Side: PHP, MySQL
If you have any idea for a workaround, that would help. Because of security reasons, I do not want to pass needed parameters as POST arguments. Because of speed reasons, I do not want to access them from my MySQL database. I would really like to know, if there is a way to access session variables through AJAX calls.
What I would like to know, if it is possible to replace AJAX calls with local PHP function calls. Then, I could take advantage of PHP global variables.
Thank you.

Sending data to external js file without it being visible to user (CodeIgniter)

I want to send a variable from my controller to a view which then uses that variable as a parameter for a Javascript function found in an external file. I know that, for example, I could pass the variable from the controller to the view using an array, and then use:
var quiz_key = <?php echo $quiz_key; ?>;
to pass the php variable to a javascript variable, which I could then use as a parameter for the function. However, in doing this, the variable becomes visible if a user was to View Source and see the html. Is there a way of accomplish this to where the variable would not be visible in this context?
Thanks in advance! I'm still getting the hang of this so any help is appreciated!
No. As javascript is not a server side language, but it runs on the client's browser, it cannot be completely hidden from him. You can hide this piece of code, obfuscate it, minimize it, but it can be found eventually.
You can set up a node.js server on the remote machine, connect to it, send it some variables, then process them and return to the client the result, but that's much more complex sollution.
I'll Vote for using Ajax. nodejs is good but if you are in a hurry you should use Ajax.

Pass PHP class instance through AJAX?

I recently wrote a PHP wrapper class for a new API we are using and have been asked to setup a demo which makes use of it. Certain features can be called directly from PHP, however things such as performing actions on button clicks requires I make use of JS/AJAX.
As I already have an instance of the object in my main PHP file, can I pass this as a parameter to JS and then pass it through Ajax to my handler or is it necessary to establish two separate instances?
The closest thing to doing what you want that I can think of would be copying the PHP object to a JavaScript variable via json_encode and then passing that variable back to your PHP code during the AJAX event via the data parameter. PHP code doesn't persist in the way that you seem to be describing - once a page has been requested by a browser, your PHP code for that page is done, there are no variables persisted by the server after that point.

Categories

Resources