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
ἔρως
ἔρως4w ago
that's impossible to answer with more context the possibilities are varied what EXACTLY do you want to do?
lanszelot
lanszelot4w 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
ἔρως
ἔρως4w ago
you mean, like a fetch request?
lanszelot
lanszelot4w ago
if it is the right word. Sorry I do not know the names, I can only use those fetch request in php?
ἔρως
ἔρως4w 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
lanszelot4w ago
my code is more than 400 lines php cannot have event listener example so js can do that
ἔρως
ἔρως4w ago
but why do you have to pass data from php to an event listener?
lanszelot
lanszelot4w 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
ἔρως
ἔρως4w ago
sounds like you're trying to re-implement the <details> tag
lanszelot
lanszelot4w ago
no no detail tag I do not understand what you mean
ἔρως
ἔρως4w ago
so, you want a list, that when you click it shows extra content that sounds a lot like the <details> tag
lanszelot
lanszelot4w 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
ἔρως
ἔρως4w ago
like the user list here on discord?
lanszelot
lanszelot4w ago
is there user list? I do not know where is user list I am not familiar with discord
ἔρως
ἔρως4w ago
on the right side
lanszelot
lanszelot4w ago
I am writing now on the right side I am from pc
lanszelot
lanszelot4w 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
lanszelot4w 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
ἔρως
ἔρως4w ago
but that page opens the names on other pages is that what you want?
lanszelot
lanszelot4w ago
"but mine not drop you other page next to the names show the details html grid"
ἔρως
ἔρως4w ago
what kind of information?
lanszelot
lanszelot4w 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
ἔρως
ἔρως4w ago
what data?
lanszelot
lanszelot4w ago
are you joking? what is data? Name, Id, ...so on I wrote datas in sql like: Name, Id, Age, Job, Country, Hair, Details
ἔρως
ἔρως4w ago
yeah, im totally joking. i have nothing better to do -.-
Jochem
Jochem4w 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
lanszelot4w 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
Jochem4w 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
lanszelot4w ago
"You'll want to make a rest api endpoint and then use fetch in Javascript to call that api." - no
Jochem
Jochem4w ago
... Okay?
lanszelot
lanszelot4w ago
I am not using api and no fetch in JS
Jochem
Jochem4w ago
Ok then use the other suggestion
lanszelot
lanszelot4w 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
Jochem4w 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
lanszelot4w 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
Jochem4w ago
you shouldn't be using var anymore
lanszelot
lanszelot4w ago
but this way cannot pass what I would like to var is ok if you careful
Jochem
Jochem4w ago
if you want to use multiple line variables, you can use backticks instead of single quotes
lanszelot
lanszelot4w ago
not multiple lines variables 1 variable
Jochem
Jochem4w 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
lanszelot4w ago
no one read this even no one will use it
Jochem
Jochem4w ago
They could, which is why I added that note
lanszelot
lanszelot4w 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.