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