How to input a php variable in a html file - javascript

I would like to export a variable from a php file in to a html file.
The php file is called example.php and looks like this:
<?php
$array= ['one','two'];
?>
The html file is called example2.html and want the $array variable to be the input of var list, like this:
<html>
<head>
<script type="text/javascript">
var list = $array
echo list;
</script>
</head>
</html>
I am new on this field and donĀ“t know where to start from, thanks for your help.

You could try json_encode() or implode() for printing your array from to javascript (html), i have shown you two solution below
<html>
<head>
<script type="text/javascript">
var list = <?="['".implode("','",$array)."']";?>;
//or
var list2 = <?=json_encode($array);?>;
</script>
</head>
</html>
you've used echo in your javascript code that is a syntax error, in javascript you can use alert() or console.log for testing your varriable, for example in top code you can try this: alert(list[0]);

Related

Word Press PHP ACF Snippets in JS functions

I'm currently adding in Advanced Custom Fields for my site but when I try and add an ACF snippet to any of the functions the content doesn't show in the browser and all my js code in the script tags stop working and displaying in the browser. what am I doing wrong?
the js code is in script tags in the front-page.php file
function changeTestimonial1(){
document.getElementById("client-name").innerHTML ="<?php the_field('client_name1'); ?>";
}
I would suggest constructing your html/php/javascript as such:
<?php
$somevar = 1234;
?>
<HTML>
<script language="JavaScript">
// JS will contain value of php variable $somevar, but make sure you escape the value.
var some_js_var = <?php echo $somevar; ?>
</script>
</HTML>

Add JavaScript in head

I understand that good practice is to add the .js files in head of HTML.
My problem is that one of my .js files (or code+script) also has a beginning function, and it looks something like this (and all this is now in head):
<?php
// Countdown timer
function timer($date, $id){ ?>
<script>
(function()
{
// random stuff
}
</script>
// end function timer
<?php } ?>
How should I include/require or <script> src=" x "</script> this kind of file in the head of my HTML? I have hard time figuring out how to add this file in the proper way because of the beginning function.
First of all, replace this:
<script> src=" x "</script>
with this:
<script src=" x "></script>
Now if I understand it correctly you want to include a .php file into the HTML header of your page, and this .php file should include a js script.
Something like:
index.html
<head>
<title>My Weird App</title>
<?php include "myphpfile.php" ?>
</head>
myphpfile.php
<?php
some_func() { ?>
<script src="myjsfile.js"></script>
<?php } ?>
myjsfile.js
function some_other_function() {
//do stuff
}
If you want to execute some javascript code when your php function gets called. This would do, but if you're hoping to be able to use some value returned from php on javascript, or the other way around, then it won't work as you expected. The preferred method of client/server communication is AJAX.
If you want to include the .js file based on a condition, this is done better by:
myphpfile.php
<?php
some_func(some_params) {
if(some_cond) {
return "<script src="myjsfile.php"></script>\n"
} else {
return "";
}
}
?>
then on index.html
<head>
<title>My not so weird app</title>
<?php
include 'myphpfile.php';
echo some_func(some_arguments);
?>
</head>
Well, I hope that helps, if I didn't get it, please comment, (or make your question clearer).

How to call custom.js file in for loop and i want pass some argument with this js

How to call customs.js file in for loop and i want pass some argument with this js,And how to retrieve this var in JavaScript file.
for($i=0;$i<5;$i++)
{
<script type="text/JavaScript" src="http://localhost/customs.js?var=test'">
}
A better way to do this would be to include your custom.js in the html of the webpage and wrap it's code in a function to which you can pass a variable, like so:
<html>
<head>
<script type="text/JavaScript" src="http://localhost/customs.js"></script>
</head>
<body>
<!-- Your HTML -->
<script>
<?php
for($i=0; $i<5; $i++){
echo "custom(".$myvar.");";
}
?>
</script>
</body>
</html>
Then in your custom.js file you can do this:
function custom(input){
//Do something
}
I should note that you should be careful inserting unsanitized PHP into html directly. For more info, see: How to prevent XSS with HTML/PHP?

Run php script within javascript <script> in .php file?

I'm trying to run a php script within a javascript <script> in a .php file. Is this possible?
<script type="text/javascript">
$("#id").example("
<?php print('Hello World'); ?>
");
</script>
No, that's not possible. JavaScript is (unless you use node.JS) running in you WebBrowser. php get's started by your apache webserver when the webserver gets an http request for example to http://localhost/test.php. So these two scripts run in two completely different scopes. If you want to start a php file (called REST-Service) you need to perform an ajax-request:
JQuery style: (I don't like it but anyway...)
$.ajax({url:"myTestFile.php",success:function(result){
$("#id").html(result);
}});
Best regards
Dustin
This examples shows you how you can pass $variable from php to Javascript
<?php $x = 'hello world'; ?>
<script type="text/javascript">
var str = '<?php print("Hello World"); ?>' ;
$("#id").html(str);
</script>
Instead of print, you can use echo and it works:
<script type="text/javascript">
$("#id").example("
<?php echo 'Hello World'; ?>
");
</script>
It Will help you.You can add php code in single quote. Try this code.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
</head>
<body>
<div id="id">
</div>
<script type="text/javascript">
$("#id").text('<?="Hello World"?>');
</script>
</body>
</html>

How to import javascript variable from .js file to html document

I am trying to print out a variable from a default.js file. That variable counts up to a number, which I want to print on the html page.
So as it is now, I have:
We found <script type="text/javascript">
var $tempCountPlaces;
document.write($tempCountPlaces); </script> places for you!
<div id="places_list">
<script type="text/javascript">
var places_file = '<?php echo site_url().'/places.html'; ?>';
var region_id = <?php echo $region; ?>;
var event_type_id = <?php echo $eventtype; ?>;
var seats = '<?php echo $seats; ?>';
</script>
As you can see, I can send files from PHP to Javascript, and then use these variables in the default.js file. But how do I import a variable from default.js to the html page, and then print the variable, as shown above?
The var $tempCountPlaces; IS defined in the default.js file, and it is not a local variable within a function.
So basicly i want to pass the variable from default.js $tempCountPlaces into my HTML file using the script tag.
You might want to consider approaching the problem like this:
HTML file contains:
We found <span id="numOfPlaces"></span> places for you!
JavaScript file contains:
var tempCountPlaces = 10;//example value fetched from your PHP service
document.getElementById("numOfPlaces").innerText = tempCountPlaces;
//of if you choose to use jQuery: $("#numOfPlaces").text(tempCountPlaces);
Your resulting interface would end up looking like this:
We found 10 places for you!
I use to ask the same question, and yes ajax and axios can help, but if you really need to pass any var from directly php to javascript, or from the script in your html yo you doc.js you have to do this:
<head>
<script type="text/javascript">
var places_file = '<?php echo site_url().'/places.html'; ?>';
</script>
<script src="js/youDoc.js" type="text/javascript"></script>
</head>
As you can see, you have only to declare your vars first and then add your js doc.

Categories

Resources