<HTML>
<script language="JavaScript">
<script language="php">
</script>
</script>
</HTML>
<HTML>
<script language="php">
</script>
<script language="JavaScript">
</script>
</HTML>
I want to insert PHP and javascript code in HTML code like above.
Can I do this work??
It doesn't work like that, you can have them in the same file per se, just not like you have.
PHP is executed on the server and the result is sent to the client, whereas the JS code is executed by the client's browser.
<?php
//php code in here is evaluated and the result sent to the client
$somevar = 1234;
?>
<HTML>
<script language="JavaScript">
//javascript in here is evaluated by the client
//you could insert PHP values here to be used in JS if you want
//make sure you escape them though...
var some_js_var = <?php echo $somevar; ?>
//the JS var above would contain the value of php variable $somevar
</script>
</HTML>
<HTML>
<script language="JavaScript">
<?php
// Your PHP code here that outputs javascript
?>
</script>
</HTML>
<HTML>
<?php
// Your PHP code here that outputs text/html code
?>
<script language="JavaScript">
</script>
</HTML>
But of course, as others pointed out, browser will not see your PHP code. It will be processed by the server and browser will see only the javascript/html.
sure, you can even make php generate javascript. php file is processed to html before sending it to client, and client will see nothing except html with javascript.
You can also insert your code like this, don't give error
<HTML>
<script language="JavaScript">
<script language="php">
echo "alert('javascript alert')";
</script>
</script>
</HTML>
<HTML>
<script language="php">
echo "php code runned";
</script>
<script language="JavaScript">
</script>
</HTML>
If you want php to be executed inside javascript, then you have to use AJAX.
AJAX is a javascript code that allows you to call the server, thus executing php code and returning the result to you, at any time you wish (not only at the creation time of the page, but at the time the javascript code is called).
https://www.w3schools.com/xml/ajax_intro.asp
with jquery is even easier:
https://api.jquery.com/jquery.ajax/
You can put PHP code in the middle of a fichero.js???
Many do not know, but not in principle. If the server to interpret a php file needs to see the php extension, note that you can not set or if the file is php html extension, therefore you're not a js file extension. What you can do is something like the php type="javaScript"> '; echo 'write ("'. $ name. '");'; echo ''; } ?> For example.
Related
The Idea is:
<script type="text/javascript">
var supervar = 'u can reach me anywhere on the page';
</script>
[...some PHP and HTML...]
<script type="text/javascript">
$('#output').html(supervar);
</script>
so basicly i need it because I use php includes and need to send the exact location of my PHP file to jQ.
Your supervar variable is already in scope inside jQuery, because it is declared above the jQuery script. The question is how to assign a variable from PHP and pass it over to supervar. Inside your .php file, you can simply escape into php when needed within the <script> element.
For example, if you want to assign is the public path of your PHP script, you can do something like:
<script type="text/javascript">
var supervar = <?php echo $_SERVER['PHP_SELF']; ?>;
</script>
Your server will execute the php script before serving the following output to the client:
<script type="text/javascript">
var supervar = '/your-app/your-file.php';
</script>
I have a file php javascript
in news.js
news_id = 1;
document.write('<div id="news-id"></div>');
function output(strHtml) {
document.getElementById('news-id').innerHTML = strHtml;
}
in news.php is using
<?php
$news_id =
?>
news_id
<?php
$str = '<p>This is id: </p>'.$news_id;
?>
output(<?php echo json_encode($str); ?>);
And index.html i call
<script src="news.js" type="text/javascript"></script>
<script src="news.php" type="text/javascript"></script>
When i run index.html is error, how to get news_id from js to using in php
Well, as wrong as the original question is, there is a way to get the a JS variable into a PHP file.
In the HTML:
<script src="news.js"></script>
<script>
document.write('<scr'+'ipt type="text/javascript" src="news.php?myvar='+myvar+'" ></scr'+'ipt>');
</script>
In news.js:
var myvar = "HELLO";
In news.php:
alert("<?php echo $_GET["myvar"]?>");
Again, I highly discourage this approach... but it works.
You can't call news.php like this. It should be included in the html file like this:
include("news.php");
and you should change the extension of your html file to .php
To use PHP as javascript source <script src="news.php"
you need explicit declare content-type in .php
<?php header("Content-type: text/javascript"); //at very first line no white-space before ?>
//this is javascript content..
Important!
first, PHP execute on web server and send response to client(browser).
then javascript execute later in web browser.
To use js value inside php function you need AJAX GET/POST.
I am using an external javascript file to call a function and it will not. i get function not defined in firebug too.
the name of the external js file is getpic.js
in the html, i put this in the header:
<script src="getpic.js" type="text/javascript">
</script>
php:
echo "<button id='sldkfj' onclick='hg();'>sdlkfj</button>";
js:
function hg()
{
alert("hello")
}
the file system is basically in one folder for wamp
this is all of getpic.js
function hg()
{
alert("hello")
}
for the php part
<html>
<head>
<script src="getpic.js" type="text/javascript">
</script>
</head>
<body>
<?php
echo "<button id='sldkfj' onclick='hg();'>sdlkfj</button>";
?>
EDIT-----
i also keep getting this in firebug:
Reload the page to get source for: http://localhost/iframe/getpic.js
Thanks
Edit:
add the code segment to the html page as a
<script type="text/javascript">
function hg()
{
alert("hello");
}
</script>
if still it doesnt work there should be something wrong with the browser.
(disabled java script) try a different browser
if it works,
obviously there's an error in linking the file.
on firebug go to the script panel and see whether it is loaded or not. (you can also use net panel as well)
try linking
<script src="/getpic.js" type="text/javascript">
if you are at the localhost(www) directory or the absolute path
<script src="/mytest/getpic.js" type="text/javascript">
add ; at the end of the alert() command
function hg()
{
alert("hello");
}
Try taking the script tag out of the head element and put it in the body. I've had this problem before and that's what fixed it for me.
I've tried (but its not working):
<?php
$htmlString= 'testing';
?>
<html>
<body>
<script type="text/javascript">
var htmlString=<?php echo $htmlString; ?>;
alert(htmlString);
</script>
</body>
</html>
Here is the tutorial that I've used for that purpose:
Try this:
<?php $htmlString= 'testing'; ?>
<html>
<body>
<script type="text/javascript">
// notice the quotes around the ?php tag
var htmlString="<?php echo $htmlString; ?>";
alert(htmlString);
</script>
</body>
</html>
When you run into problems like this one, a good idea is to check your browser for JavaScript errors. Different browsers have different ways of showing this, but look for a javascript console or something like that. Also, check the source of your page as viewed by the browser.
Sometimes beginners are confused about the quotes in the string: In the PHP part, you assigned 'testing' to $htmlString. This puts a string value inside that variable, but the value does not have the quotes in it: They are just for the interpreter, so he knows: oh, now comes a string literal.
You're missing quotes around your string:
...
var htmlString="<?php echo $htmlString; ?>";
...
All the explanations above doesn't work if you work with .js files. If you want to parse PHP into .js files, you have to make changes on your server by modfiying the .htaccess in which the .js files reside using the following commands:
<FilesMatch "\.(js)$">
AddHandler application/x-httpd-php .js
</FilesMatch>
Then, a file test.js files containing the following code will execute .JS on client side with the parsed PHP on server-side:
<html>
<head>
<script>
function myFunction(){
alert("Hello World!");
}
</script>
</head>
<body>
<button onclick="myFunction()"><?php echo "My button";?></button>
</body>
</html>
The only proper way to put server side data into generated javascript code:
<?php $jsString= 'testing'; ?>
<html>
<body>
<script type="text/javascript">
var jsStringFromPhp=<?php echo json_encode($jsString); ?>;
alert(jsStringFromPhp);
</script>
</body>
</html>
With simple quotes the content of your variable is not escaped against HTML and javascript, so it is vulnerable by XSS attacks...
For similar reasons I recommend to use document.createTextNode() instead of setting the innerHTML. Ofc. it is slower, but more secure...
you need quotes around the string in javascript
var htmlString="<?php echo $htmlString; ?>";
As others have pointed out you need the quotes, but I just want to point out that there's a shorthand method of writing this same line of code
var htmlString="<?=$htmlString?>";
See you can leave out the "php echo" stuff and replace it with a simple "=".
Let's see both the options:
1.) Use PHP inside Javascript
<script>
<?php $temp = 'hello';?>
console.log('<?php echo $temp; ?>');
</script>
Note: File name should be in .php only.
2.) Use Javascript variable inside PHP
<script>
var res = "success";
</script>
<?php
echo "<script>document.writeln(res);</script>";
?>
i need to have some php code inside javascript
<script ...>
<?php
echo " ... ";
?>
</script>
but this doesnt work. how can u implement php inside javascript that is in a own file javascript.php?
That doesn't do what you probably think it does. It'll work, but the PHP gets run once, when the page is loaded, not every time the JavaScript function is called.
Just for clarification, this is what will happen
index.php
<script type="text/javascript">
<?php echo "alert('hello!');"; ?>
</script>
output html in browser
<script type="text/javascript">
alert('hello!');
</script>
If that is what you want to do, then you can output all the javascript you like. What you cannot do is execute PHP code in the user's browser.
your can use php to dynamically generate javascript code, but you cannot execute php client side. If you need to execute php you will need to postback or use AJAX
There seems to be a good bit of misunderstanding of the question... Here is what you want to do to generate JS from PHP on the server:
file javascript.js.php
<?php
header('Content-Type: text/javascript');
?>
// javascript code here
function PrintTime()
{
alert("The time is " + <?php echo json_encode(time()); ?>);
}
Now, include it on the HTML page using normal script tags:
<script type="text/javascript" src="/url/to/javascript.js.php"></script>
The server will process the PHP file, and return javascript from it.
You can't run PHP inside a javascript file. Primarily because PHP runs server side and is processed before the client is sent any actual http info. JavaScript is processed by the browser on the client side and is sent as text.
It looks like you want to pass some kind of dynamic info to the JavaScript. You can do this by passing a variable like this:
<?php $variable="its me"; ?>
<script>
alert('<?php print($variable)?>')
</script>
The output passed to the client is:
<script>
alert('its me')
</script>
What are you trying to accomplish and maybe we can help you come up with a better solution?