pass PHP to JS - string

Hello, How can I pass string from PHP to JavaScrtipt? string like this : "1 - one 2 - two 3 - three" 4 - éáúő 5 - $%*&" Simple string, with line breaks. in sql database I have descriptions this descriptions I need to use in JS
43 Replies
ἔρως
ἔρως6mo ago
that's impossible to answer with more context the possibilities are varied what EXACTLY do you want to do?
lanszelot
lanszelotOP6mo ago
there are things what php cannot do, so I have to do with js so I have to pass datas to js from sql
ἔρως
ἔρως6mo ago
you mean, like a fetch request?
lanszelot
lanszelotOP6mo ago
if it is the right word. Sorry I do not know the names, I can only use those fetch request in php?
ἔρως
ἔρως6mo ago
im trying to understand absolutely exactly why you have to put data in javascript, from php if you can show code, that helps a lot too
lanszelot
lanszelotOP6mo ago
my code is more than 400 lines php cannot have event listener example so js can do that
ἔρως
ἔρως6mo ago
but why do you have to pass data from php to an event listener?
lanszelot
lanszelotOP6mo ago
onclick example I have name li tags from sql database, when I click one of them, those name datas have to show on the page
ἔρως
ἔρως6mo ago
sounds like you're trying to re-implement the <details> tag
lanszelot
lanszelotOP6mo ago
no no detail tag I do not understand what you mean
ἔρως
ἔρως6mo ago
so, you want a list, that when you click it shows extra content that sounds a lot like the <details> tag
lanszelot
lanszelotOP6mo ago
no, that is details tag sql database have lot lot tables 1 table [ name and id] 2nd [different datas] 3rd....so on in the datas there is details, like name and id, but that one have details with string like what I type at the first one li tag have names, all names from sql datas if I click on of the name , that specific name datas have to show it on the page
ἔρως
ἔρως6mo ago
like the user list here on discord?
lanszelot
lanszelotOP6mo ago
is there user list? I do not know where is user list I am not familiar with discord
ἔρως
ἔρως6mo ago
on the right side
lanszelot
lanszelotOP6mo ago
I am writing now on the right side I am from pc
lanszelot
lanszelotOP6mo ago
BabyCenter
Most popular baby names of 2024
Our list of the most popular baby names of the year is different from the others. We update it in real time, using data BabyCenter parents share with us as…
lanszelot
lanszelotOP6mo ago
you click on the name and show details. but mine not drop you other page next to the names show the details html grid
ἔρως
ἔρως6mo ago
but that page opens the names on other pages is that what you want?
lanszelot
lanszelotOP6mo ago
"but mine not drop you other page next to the names show the details html grid"
ἔρως
ἔρως6mo ago
what kind of information?
lanszelot
lanszelotOP6mo ago
like VS Code left side list right side datas when you click left side a file name, right side you can see the file but mine is not files only datas
ἔρως
ἔρως6mo ago
what data?
lanszelot
lanszelotOP6mo ago
are you joking? what is data? Name, Id, ...so on I wrote datas in sql like: Name, Id, Age, Job, Country, Hair, Details
ἔρως
ἔρως6mo ago
yeah, im totally joking. i have nothing better to do -.-
Jochem
Jochem6mo ago
Please stay civil, Epic is just trying to help. He's asking reasonable questions to try to determine what the best solution is
lanszelot
lanszelotOP6mo ago
He ask me same question again and again. I wrote many times. Pleas look back. And I am not write anything wrong. Please write to me what wrong did I wrote "sql database have lot lot tables 1 table [ name and id] 2nd [different datas] 3rd....so on in the datas there is details" 17:19
Jochem
Jochem6mo ago
You'll want to make a rest api endpoint and then use fetch in Javascript to call that api. Either that, or embed the data in data attributes on the LIs on the initial page load. It depends on how much data you have, and how often someone that loads your page will access one or more detail views of your data That's why Epic kept asking what data, because you just gave a vague, incomplete description, which isn't enough to make the determination So if you're asking a question about a topic that you don't know anything about, someone keeps asking you to clarify, and you don't know why they keep asking, try asking them politely what information they need rather than just going "are you joking". It's rude and only going to escalate things and make people less likely to want to help you
lanszelot
lanszelotOP6mo ago
"You'll want to make a rest api endpoint and then use fetch in Javascript to call that api." - no
Jochem
Jochem6mo ago
... Okay?
lanszelot
lanszelotOP6mo ago
I am not using api and no fetch in JS
Jochem
Jochem6mo ago
Ok then use the other suggestion
lanszelot
lanszelotOP6mo ago
I do not understand This is php how to pass php string to js no fetch, no api you wrote api and fetch , not me you just confusing me I do not understand
Jochem
Jochem6mo ago
you're asking how to pass data between two programming languages. That's a problem that is usually solved by using an API (Application Programming Interface).
lanszelot
lanszelotOP6mo ago
echo "<script>var hova = '$elem';</script>";
echo "<script>var hova = '$elem';</script>";
this is how I pass simple string no api, no fetch
Jochem
Jochem6mo ago
you shouldn't be using var anymore
lanszelot
lanszelotOP6mo ago
but this way cannot pass what I would like to var is ok if you careful
Jochem
Jochem6mo ago
if you want to use multiple line variables, you can use backticks instead of single quotes
lanszelot
lanszelotOP6mo ago
not multiple lines variables 1 variable
Jochem
Jochem6mo ago
echo "<script>var hova = `$elem`;</script>";
echo "<script>var hova = `$elem`;</script>";
will work if there are newlines in the variable for anyone else reading this in the future though, that is a terrible practice especially if it's more than a couple of variables
lanszelot
lanszelotOP6mo ago
no one read this even no one will use it
Jochem
Jochem6mo ago
They could, which is why I added that note
lanszelot
lanszelotOP6mo ago
oh, I see. you mean here. I thought my page Thank you so much for help. I will solve the problem to change description format. I think this is the only way to me.
Want results from more Discord servers?
Add your server