This is a theoretical question.
My question is whether a jQuery function or script can be written inside a PHP function.
E.g.
<?php function phpfunc(){
$a=10; ?>
<script>var a ='<?php $a ?>'</SCRIPT> <?php } ?>
Is this possible and legal?
Yes. It is possible and Legal one too. we generally use the same when we require any server side value to be set on client-side on runtime.
Hope this answers your query.
Thanks Much!
When php code is interpreted by the sever writing something like:
<?php
function foo()
{
<script type=text/javascript> ... </script>
}
?>
As part of the code in <?php ?> is interpreted as php and string inside the function doesnt represent any of php functions
You can echo javascript code (or any content of a HTML document) through your php code like:
<?php
function foo(){
echo "<script type=text/javascript> alert('it works!)'; </script>";
} ?>
so when you execute the function, you wil add the javascript to the document by echoing it and therefore execute it.
You can also use php variables to echo variables to javascript like:
<?php
function foo(){
echo "<script type=text/javascript> alert('{$phpVariable}'); </script>";
} ?>
or
<?php
function foo(){
echo "<script type=text/javascript> var variableFromPHP = {$phpVariable}; </script>";
} ?>
Yes, it's okay to do that. No, it's probably not a good idea. But there's nothing really stopping you.
Just be aware that if your variable happens to have a ' in it, you'll get messed up.
So, whenever you want to pass a variable from PHP to JavaScript, be sure to use json_encode:
<script type="text/javascript">
alert(<?php echo json_encode($something); ?>);
</script>
Note that there's no quotes in the JavaScript part - json_encode will add quotes if needed. This can be used to pass almost any kind of variable.
The short answer is no(edit : yes).
javascript is executed on the client and only on the client.
You can however echo javascript to the client.
So something like this :
$JSfunction = "<script>alert('This is working')</script>";
can be echoed to the page by doing echo $JSfunction;
edit :
Since you didn't mention where that function is located, I assumed you meant the PHP function on the server side.
To be clear, If it's written on the html page itself, it's perfectly fine and can be done.
complete answer
<? function phpfunc(){
$a=10; ?>
<script>var a ='<?php echo $a ?>'</SCRIPT> <?php } ?>
<?php phpfunc() ?>
<script>console.log(a);</script>
You must echo that $a
Yes you may use it like in normal html page. Use script include inside echo inside the php script.
You may use it outside the php script normally. Use container tag for the same in the php page.
I hope it would help.
Related
I have normal alert in my code, instead I need sweet alert as an alternative.
But the problem is my file is pure Php file, and there is no html tag to include the sweetalert.js file, hence it is not working.
<?php
// ...
echo '<script type="text/javascript" src="https://unpkg.com/sweetalert/dist/sweetalert.min.js">';
echo 'swal("Mobile Number not registered.Please login through Email");';
echo '</script>';
?>
The text content of a script element gets ignored, if the script element has a src set. The JS code you are dynamically outputting here via PHP, should not have worked to begin with, even if it was “static” instead.
Something like
echo '<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>';
echo '<script>swal("Mobile Number not registered.Please login through Email");</script>';
would work … but it’s not really nice. You should not output larger parts of static HTML via echo in the first place, but properly “break out” of the PHP code part, see https://www.php.net/manual/en/language.basic-syntax.phpmode.php
A proper way to do this:
<?php
// some PHP stuff here
?>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>swal("Mobile Number not registered.Please login through Email");</script>
<?php
// more PHP stuff here
When i use the php and js separately it is working correctly
<script> var test = 'asdasdasd'; </script> <?php $id =
"<script>document.write(test)</script>"; echo $id; ?>
But when I use the php inside the js function it is not working correctly
<script> var test = 'asdasdasd'; <?php $id =
"<script>document.write(test)</script>"; echo $id; ?> </script>
How can make work my second code?
The second code produces wrong HTML/JS:
<script> var test = 'asdasdasd'; <script>document.write(test)</script> </script>
- see for yourself in the generated page.
Since everything between <script> and </script> is considered to be JS code, it will try to parse the inner <script> as Javascript code...and fail.
I think the inner other of <script>...</script> tags (those that are in PHP markup) is not needed at all, just get rid of them and it will work.
You can insert any value from php to absolutely everywhere in your file. Literally everywhere.
While writing client-side code, in the right place open php tag <?php and echo what you need. Don't forget to write a closing tag ?> afterwards.
Here, you are echoing the opening script tag, which you already wrote before. That results in a syntax error:
<script>
<script>
...
</script>
So basically i want to html() a php file but i seem to fail, maybe it isn't possible but i want to make sure.
This is an example code (not the real one):
page.php:
<? //some code generating html ?>
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<? include('php/content.php'); ?>");
</script>
php/content.php:
<div class='realContent'>Awesome Stuff Here</div>
Note: I know there is .load(), $.post and many others. I just want to know if this practic is possible, .html()-ing a php file's contents.
Thank you in advance, awesome community!
d3nm4k.
The code in my example does what you want.
Maybe you got the problem with the short opening tags that is < ? ? >.
The file I'm trying to load in my example is located in app/View/Tests/def.php and works just fine.
Hope it helped you!
<div class='content'></div>
<?php
$fileLocation = APP . 'View/Tests/def.php';
// Ensure the file exists
echo "FILE EXISTS: <b>" . (file_exists($fileLocation) ? "TRUE" : "FALSE") . "</b><br /><br />";
?>
<?php
// Alternatively, you can try this one
//include $fileLocation;
?>
<script type='text/javascript'>
$('.content').html("<?php include($fileLocation); ?>");
</script>
You can do it, the code you gave i tried it just by adding ?php
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<?php include('php/content.php'); ?>");
</script>
When the php is executed on server it replaces content with actual html inside html() function. And on client side javascript plays its role to render it.
The output generated by the program below is Inner textjain. Fine, but I want $m php variable to be assigned the value returned from JavaScript function a().
<html>
<head></head>
<body>
<?php
$name='abhi';
?>
<div id="a">
</div>
<script type="text/javascript">
function a(){
var a="jain";
<?php echo $GLOBALS['name'] ; ?>=a;
document.getElementById('a').innerHTML="Inner text"+<?php echo $name; ?>;
return <?php echo $GLOBALS['name'];?>;
}
</script>
<?php echo $n='<script type="text/javascript">' , 'a();' , '</script>';
echo $n;?>
</body>
</html>
You can't. Once the page is delivered to the browser, which is where Javascript runs, your PHP server has already shut the request down. If you somehow really want to send something calculated in Javascript back to PHP, you'll have to make an additional request to the browser from Javascript (using AJAX) but even then the variable will be available in a new request, not the old one.
PHP runs on the server, Javascript runs on the browser, and they don't run concurrently, so you'll have to send your Javascript variable to the PHP script (and re-run the PHP script), which involves reloading the page.
You can e.g. a GET request (by loading script.php?yourvariable=value).
you can't. PHP runs at the server first and then javascript will run at client side.
Is it possible to edit javascript code so that before it executes on page load I can inject few code changes through PHP. I want to use PHP so that changes happen on the server before page is loaded onto client side.
For example, this is what I have:
<script>
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&'>")
</script>
And this is what I want:
<script>
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&oneMoreParameter=paramValue'>")
</script>
I tried this with jquery but because this is flash, once it is loaded then adding parameter doesn't do anything. That's why I thought doing it in PHP because it executes on server side.
Any help on this will be great.
Thanks!
Take a look below:
<?php
$test = 'test';
?>
<script type="text/javascript">
var test = '<?php echo $test; ?>';
</script>
Hope, this will be helpful to you!
$ob = ob_start();
//EXECUTE YOUR CODE HERE, THE PAGE AND STUFF e.g. include('./file.html');
$pi = ob_get_contents();
ob_end_clean();
$js = <<<'EOD'
video38.addParm("<param name='FlashVars' value='file=abc&fullscreen=true&width=440&height=241&oneMoreParameter=paramValue'>");
EOD;
$pi = preg_replace("#video38.addParm(.*?);#s", $js, $pi);
echo $pi;