declaring/calling php function from another file using ajax - php

How do I call my functions from functions.php to html.php with the use of AJAX? I want to declare my functions inside my html.php file without using include or require. only ajax pls. Thanks allot! see below
functions.php
<?php
function samplefunc(){
echo "Hello world";
}
?>
html.php
<html>
<body>
<?php samplefunc(); ?>
</body>
</html>

add the below code in html.php
$.post("functions.php", {call_function: samplefunc})
.done(function(response) {
alert(response);
}
and add below code in functions.php
function samplefunc() {
echo 'Answer';
}
if ($_POST['call_function'] == "samplefunc") {
samplefunc();
}

Related

Load JavaScript when condition is true

How do I load a script if a PHP condition is true?
For example, I got an external script called script.js and I wanna load this if it's on the product page of WooCommerce.
I've tried the following but it doesn't work because it prints the code on the page:
<?php
add_action( 'template_redirect', 'check_product_page' );
function check_product_page() {
if(is_product()) {
include ('script.js');
}
}
?>
If I write the script inside a PHP like the below, it causes a fatal error:
<?php
echo
'<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>';
?>
Try this:
<?php
if($condition == true){
echo '<script type="text/javascript" src="script.js"></script>';
}
?>
or:
<?php if($condition == true): ?>
<script type="text/javascript" src="script.js"></script>
<?php endif; ?>
Actually this works on my side, checkout maybe you have another issue:
echo '<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>';
It's because the single quotes are inside your string. You should actually do it the way which other people have shown, but if you need it to be an inline script you can do it like this:
<?php
//some php can go here..
?>
<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>
<?php //more php can go here... ?>

jquery not working on php include file

Jquery Not Working On Include File.Here is My Code Sample.But in LocalHost That's Work Fine.and also i am using Bootstrap.
<body>
<?php
include('header.php');
if(UserType=="User")
{
// In This Portion Jquery Work :)
include('BProfile.php');
}
elseif(userType=="Premium")
{
//In this Portion Jquery Dose Not Work but file load properly :(
include('PProfile.php');
}
else
{
echo 'unable To Load Profile';
}
include('footer.php');
?>
//I also Put The Jquery On Top But Dose Not Work for PProfile
<script type="..." src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</body>
Thanks and best reagards

Call a function from one PHP file to another

I am not quite sure why have I not been able to call a function from say File1 to File2 . I have used the require_once also .
Eg :
File1.php
<?php
function Test()
{
alert("Hello");
}
?>
File2
<?php
require_once("File1.php");
My code
?>
<script type="text/javascript">
My code....
$("#email_summary").click(function() {
<?php
Test();
?>
});
</script>
Could you please let me know my mistake ?
Thanks guys :)
you can not call php function by JavaScript like you did
$("#email_summary").click(function() {
Test();
});
</script>
should be
$("#email_summary").click(function() {
<?php echo Test(); ?>
});
</script>
and
function Test()
{
return 'alert("Hello")';
}
edit: if you want to make it work like your code you can do this by
File1.php
<?php function Test() { ?>
alert("Hello");
<?php } ?>
and than just include like
<?php
Test();
?>

calling javascript function from php

I am trying to call a javascript function from php. According to all of the examples I have been looking at the following should work but it doesn't. Why not?
<?php
echo "function test";
echo '<script type="text/javascript"> run(); </script>';
?>
<html>
<script type="text/javascript">
function run(){
alert("hello world");
}
</script>
</html>
Your html is invalid. You're missing some tags.
And you need to call the function after it has been declared, like this
<html>
<head>
<title></title>
<script type="text/javascript">
function run(){
alert("hello world");
}
<?php
echo "run();";
?>
</script>
</head>
<body>
</body>
</html>
In this case you can place the run before the method declaration, but as soon as you wrap the method call inside another script tag, the script tag has to be after the method declaration.
Try yourself http://jsfiddle.net/qdwXv/
the function must declare before use
it should be
<html>
<script type="text/javascript">
function run(){
alert("hello world");
}
<?php
echo "function test";
echo run(); ;
?>
</script>
</html>
As others have suggested, the function needs to be declared first. But, if you need to echo out the JavaScript from PHP first, you can either store it in a PHP variable to echo out later, or have your code wait for the dom to finish loading first...
document.ready = function() {
run()
}
If you're using jQuery or another framework, they probalby have a better way of doing that... In jQuery:
$(function(){
run();
})

<?PHP ?> tag can be used in javascript

Can we use the<?php ?> tag in javascript? If yes then my next question is; can we use session in this tag? Example:
success:function(data)
{
if(data)
{
<?php
if(isset($_SESSION['UserId']))
{ ?>
window.location.href="coll_delivery_det.php";
}
else
{
?> window.location.href="courier.php"; <?php
}
} ?>
}
If I understand what your looking to do you would want to use php to echo out your javascript commands.
success:function(data)
{
if(data)
{
<?php
if(isset($_SESSION['UserId']))
{
echo "window.location.href=\"coll_delivery_det.php\";";
}
else
{
echo "window.location.href=\"courier.php\";";
}
} ?>
}
Yes. But only if the page is executed as actual PHP page.
If you use PHP code through your javascript or HTML I suggest using templatish statements, like so:
<?php if ($someVariable) : ?>
var i = 0;
<?php else : ?>
var i = 2;
<?php endif; ?>
It'll be much more clear what statements are closed. Instead of the following:
<?php if ($someVariable) { ?>
var i = 0;
<?php } else { ?>
var i = 2;
<?php } ?>
In fact, you can't use PHP tags in JavaScript.
You can only generate either whole JS code or only some data for it using PHP.
The specific solutions posted here address your current situation, I'd just like to touch on the reasoning behind them.
Javascript logic is executed in your browser.
PHP logic is executed on the server.
Embedding conditional PHP statements directly in javascript won't do what you want, but you can use PHP to generate the javascript your browser needs to execute.
Yes as long as you are doing this within a file that will be executed as PHP but your code is syntactically incorrect from what I can see. Try this instead:
success:function(data) {
if(data) {
<?php if(isset($_SESSION['UserId'])) { ?>
window.location.href="coll_delivery_det.php";
<?php } else { ?>
window.location.href="courier.php";
<?php } ?>
}
}
It is worth noting that you cannot go the other way. Javascript variables cannot be used in PHP code as by the time the users browser executes the Javascript the PHP execution cycle is terminated. The only way to pass it back this way would be to make an Ajax request.
Also the PHP will only be run once each page load so using your code if $_SESSION['UserId'] is set then the users browser would just see:
success:function(data) {
if(data) {
window.location.href="coll_delivery_det.php";
}
}
Otherwise if it is not set it will just be rendered from PHP as:
success:function(data) {
if(data) {
window.location.href="courier.php";
}
}
In this way javascript is generated from your PHP code.
Yes, php can generate anything: html, css and JavaScript as well. So you can do something like that on your .php page:
function() {
<?php if($data) { ?>
window.location.href="coll_delivery_det.php";
<?php } else { ?>
window.location.href="courier.php";
<?php } ?>
}
However you need to remember that PHP is generating JavaScript as any other text, so you can't use Javascript variables in PHP script. eg. something like that will not work:
function test(){
var r=1;
<?php if ($r==1){ ?>
alert('r = 1');
<?php }?>
}
If you're using apache you can create a .htaccess file in your javascript directory with the following contents:
AddHandler php-cgi .js
This will make your .js files run as php, but retain its original extension.
the easiest way is to create a page that generates a dynamic javascript and includes the header for the javascript.
mysite.com/js.php
<?php header("Content-type: application/x-javascript");?>
success:function(data) {
if(data) {
<?php if(isset($_SESSION['UserId'])) { ?>
window.location.href="coll_delivery_det.php";
<?php } else { ?>
window.location.href="courier.php";
<?php } ?>
}
}
but you probably dont want to do that.. browsers save the included javascript files on cache, and you could have some problems with that..
the best way to proceed is to have your js files separated and and pass some parameter to the functions or classes using a <script> tag inside your <header>

Categories