Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

WallyJ's avatar

Javascript within a Blade View, Passing Variables between PHP and Javascript

So, PHP, server side, Javascript, client side... But when using something like Puppeteer (Javascript) for data mining, I need to pull data from the DB first, send variables from PHP to Javascript (Puppeteer, to mine the correct data), then the results from the mining (Javascript variables) need to be added/appended to the database to particular records, not just edited for screen view (client side).

I want to do this correctly with Laravel using models, routes, views, etc. But the passing back and forth may take multiple views, but maybe not. I also want to keep the Puppeteer Javascript in a separate file and call it when needed in the process.

Just looking for some logical suggestions of file structures and where to place each functional part of my code.

I have looked into using a utility like :

Transform PHP Vars to JavaScript https://github.com/laracasts/PHP-Vars-To-Js-Transformer

to help with the variable interchange, but I shy away from utility dependency, but I'm trying to use what works.

0 likes
6 replies
WallyJ's avatar

I appreciate the suggestion. However, most of what I read in tutorials and other posts about PHP, Javascript, and AJAX, is based on form submissions. I don't need to submit a form. I need to run a DB lookup, pass the info to Puppeteer Javascript, which will run, return results, put those in the DB in the correct records, and then notify me that it is finished, (possibly showing me the new records recently added via the datamining process). Maybe I need to look more into examples that don't require form submissions when using AJAX

Cronix's avatar

You can use ajax for anything. In essence, you send a request and get a response, just like you do normally, except it's behind the scenes and not a new page request. If you look at the ajax/form examples (yes, most widely used scenario) it doesn't matter that it's a form. The important thing is it is collecting data (could be from a form, could be from anything) and sending it to a url, and receiving a response. What data you are sending is up to you.

WallyJ's avatar

@Cronix , Always great and explanatory responses. This is the heart of learning! Thanks for that explanation. I will work on converting the form examples to pull the data from my variables rather than form fields, if I'm understanding you correctly. :)

Cronix's avatar

Yes, most ajax libraries have a data portion, where you just send key/value pairs as json in the request (just like a form does. Form fields have names (the key) and a value being submitted). You don't even actually have to send data. You can just hit the url and receive a response. Maybe you just want to check if any sales have been made in the last 5 minutes, etc. That wouldn't require sending data. Just send the request to the url, the route goes to a controller, the controller queries the db for sales made in last 5 minutes and returns the results as json, where you can do what you want with them.

WallyJ's avatar

Cool. To modify your example a bit, my app would have an existing list of products without prices in the DB (id, productname, price1, price2, price3).

The app queries the DB for a list of products, runs Puppeteer (Javascript) to get prices from 3 different websites (added to Javascript array via push method as part of a loop for each product/3 websites), then adds the prices to the DB with the appropriate products.

Please or to participate in this conversation.