winsalva

winsalva

Hello, i want to know how can i possibly access/get Javascript values defined in assets/js/app.js into my Elixir code?

Example, in assets/js/app.js file..

var x = "hello"

What i want to happen or trying to accomplish is access this variable x’s value and pass it as parameter to my Elixir code defined in lib/app/test.ex.

Example Elixir code would be,

def my_function(x_value_here) do
	. . .
end

Any suggestion would be really appreciated. Thanks in advance :folded_hands:

Showing Posts 1 to 7

soup

soup

You might need to give more specific information on your goal, and whether youre using liveview or normal views and when you want to access the value.

winsalva

winsalva OP

Hello sir, thank you for the response.

I am just using ordinary view.

What i’m currently doing here is i want to add a qr code scanner on my personal website (for test purposes only) and im using a Javascript library.

Now the Javascript code that reads and decodes the qr code was defined in assets/js/app.js file. I would want to access this result in my Elixir code so i can use it to scan for records in my database.

soup

soup

You wont be able to directly access the javascript values in Elixir, you need to send them over some bridge. There are a few ways you might approach this. Your QR library will have some way to run a javascript function when a QR code is recognised and this function will all most certainly receive the data that was embedded in the QR code.

In this callback you might:

a) Make a HTTP request in JS to a controller-action that returns data from your records. Your JS should then process the results and update the page for the user. Most commonly this data is returned as JSON.

Things you may want to look into: json, rest api and see Using the Fetch API - Web APIs | MDN which has some example JS code, as well as searching for phoenix rest api.

b) Redirect to a URL with your QR result embedded in the URL. So if your QR code decodes to dogs, you might redirect to https://myapp.com/search?q=dogs to search for dogs, or if it decoded to a record id 100, you might redirect to https://myapp.com/blog/100. This can be as simple as

window.location.href = `https://myapp.com/${qrdata}`;

c) Use the QR data to set values in a regular HTML form and submit it. This an be useful if you have an existing form on the page and the QR code is a “shortcut” to some results from that form.

Good luck. Try making all 3 options.

winsalva

winsalva OP

Thank you so much sir. I really appreciated your efforts! This are really very helpful to me :heart:

LostKobrakai

LostKobrakai

I’d also add using channels and/or liveview to the list of options you could use to send the value to the server for processing.

winsalva

winsalva OP

Noted sir. Thank you :heart:

dev234

dev234

Would you mind explaining this further please? I have the same issue as the OP and am still new to elixir/phoenix. I’m using Surface UI if it helps to mention that. Thank you

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews