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>
Related
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).
I am making a chrome extension on a localhost with an $.get request that executes a PHP script and echoes the output in a div. The PHP does execute and echo but inside the popup.html javascript returns the output as a comment.
Is it not possible to use php in any way in a chrome extension?
popup.html
<!doctype html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="jquery.min.js"></script>
<script src="toolbox.js"></script>
<head>
<body>
<div id="hello"></div>
</body>
Toolbox.js:
$(function(){
$.get('toolbox.php', function(data){
$("#hello").html(data);
});
});
toolbox.php:
<?php
echo 'hello';
?>
result:
<div id="hello"><!--?php
echo 'hello';
?--></div>
EDIT: I am running it in xampp the extension folder is in my htdocs folder and the php is inside the extension folder when i go to http://localhost/chrome%20extension/popup.html it does execute the php properly
That sounds like you are running this without a webserver and the php doesn't actually get executed - so you will need to setup one in order to make this work.
Once you've done this, have a look at $.load() which is made for what you are doing with $.get():
$(function(){
$("#hello").load('toolbox.html');
});
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]);
I'm trying to simply POST something using JQuery. when the element is clicked, I want the page redirect to the php file and echo out the POST value.
Html file
<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div onclick="posttest();" >hi there</div>
<script>
function posttest(){
var txt="hi";
$.post("testpost.php", {testVar: txt},function(){
window.location.href = "testpostphp.php";
});
}
</script>
</html>
PHP file
<?php
var_dump($POST["testVar"]);
?>
There is $_POST variables in PHP, not $POST.
Try this:
$.post("testpost.php", {testVar: txt}, function(response){
console.log(response);
});
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?