How to pass variable value from php to external JavaScript file? [duplicate] - javascript

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
I have a value sitting in my database which i want to use in an external JavaScript file. The JavaScript file is being referenced in the the html code using <script src="/thanks/thanks.js"></script>
Can anyone please help to figure out how i can either pass the value from php tp JavaScript or how i can access the database from the JavaScript?

The simple way is to use the <script> tag and echo the value you want to.
<?php
echo "<script>var parse = $value;</script>";
?>
Now you can use your value in javascript (parse variable). But if you want to have it detailed...
here is the duplicate you didnt search for: How to pass variables and data from PHP to JavaScript?

Related

Is there any better way to pass javascript variable as php variable? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass JavaScript variables to PHP?
(16 answers)
Closed last month.
I am playing with PHP and Javascript. Also I am trying to find all possible ways to pass javascript variable into php variable in the same php file. So far, I have found two possible ways. One is well known to everybody which is ajax (i,e calling another php file). But it could not solve my problem easily. Another solution that I have figured out is utilizing COOKIE/SESSION. Here is my simple code example: It will alert 5 to 10. Javascript variable value 5 is passing inside php variable =>
<div id="one">5</div>
<script>
var vall = document.getElementById("one").innerHTML;
document.cookie = "val="+vall;
alert('<?php for($i=$_COOKIE["val"];$i<=10;$i++){echo $i;} ?>');
</script>
Do you have any other alternate or better solution?

get data from mysql using php and javascript [duplicate]

This question already has answers here:
Using Jquery Ajax to retrieve data from Mysql
(5 answers)
Updating a MySql database using PHP via an onClick javascript function
(2 answers)
How to update a web page without reloading the web page using AJAX?
(4 answers)
Closed 5 years ago.
I am new to javascript. I have one label, What I am doing is to get id using javascript which I am saving in a variable.
What I want is to send that variable to php page, where it will compare that id with mysql database id and returns it's data(some text) to javascript, it will then set it to label.
I would have used text file which is easiest way but some says this isn't a secure way to do this.
So how can I achieve this ?
Any help is appreciated, Thanks in advance.

Fetching data from a json file using variables javascript [duplicate]

This question already has answers here:
How do I access a JSON object using a javascript variable
(4 answers)
Closed 8 years ago.
My question is pretty simple. I have a json file stored and I fetch it using a simple xml request and store it for later use in the localstorage. Now What I need to do is that when the user selects something from the select tag, I fetch the value of the tag and use it get the data from json.
For ex:
database.Class.Teachers."The value from the select tag".age
Is this possible with javascript or similar workaround?
Ok i got the answer. Thanks.
link for the working code i found.
How do I access a JSON object using a javascript variable

read local text file in javascript [duplicate]

This question already has answers here:
How do I read in a local text file with javascript?
(2 answers)
Closed 9 years ago.
I have a local test.txt file that I need to read into variable in JavaScript and send that variable to google.visualization.arrayToDataTable() to create a chart. Text in test.txt file is in right format (I tried to copy it directly into function and it works). But I don't know how to read that file into variable.
Reading local fine js is not permitted how ever you can use jQuery script function to load the data

Pass an array from .jsp file to .js file [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to transfer java array to javaScript array using jsp?
After googling I am still not able to pass array from .jsp file to js file.
Can somebody help me out?
In my .jsp file i have an array and I wan to call a function in .js file which accepts this array. How to call this function?
By passing an array from .jsp to js file I guess you mean your Javascript script needs to "call" a script return by a JSP file. And that script contains a function that returns an array?
If so have you looked at returning JSON from your JSP?
For simple arrays, you can convert the array to a comma-delimited string using an implode function, pass the string to javascript, and then use split(str) in Javascript to get an array back. More complicated arrays may cause issues with this.

Categories

Resources