I want to first load my php code to get some values and then load my web html with the values already loaded. This is possible? I am looking to do this, since I want to call my php code without using Ajax and at the same time the values obtained by the Php will be loaded in my web html.
Like this:
Load www.example.com -> redirect to www.example.com/index.php
---index.php---
<?php
//do something
include("YourHtmlWeb.html");
echo '<script type="text/javascript">',
'YourFunction("hi");',
'</script>';
exit;
?>
---Html-javascript---
<script type="text/javascript">
function YourFunction(msg){
alert(msg);
}
</script>
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
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.
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.
Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>
I am trying to use get_file_contents() in a setInterval() to repeatedly update some text which is displaying the contents of a file. Here's the code:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
<div id="theDiv"></div>
</body>
<script>
$("document").ready(function(){
setInterval(function(){
$("#theDiv").text("<?php echo file_get_contents('file.txt'); ?>");
console.log("<?php echo file_get_contents('file.txt'); ?>")
},500);
});
</script>
The problem is: it seems that the text is not updating until I reload the page.
the file_get_contents() is NOT set to reload by setInerval. You mixed up client-side JavaScript & server-side PHP.
You probably need the following:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('file.txt');
},500);
});
You have to create another php file to output the content and use ajax to get it.
Example:
$("document").ready(function(){
setInterval(function(){
$("#theDiv").load('/echo_the_content.php');
},500);
});
Create echo_the_content.php with:
<?php
echo file_get_contents('file.txt');
If your file.txt is in public dir (web accessible), then you could load it directly without another php file.
the setInterval get executed in the client side where as the php is evaluated in the server side so the command echo file_get_contents('file.txt'); is evalulated only once.
You need to use an ajax request where it returns the contents of the file
$("#theDiv").load("file.txt");
where the server side resource file.txt returns the desired response that has to be displayed in the div