How to include php with JQuery? - php

I have been struggling with this for a while now.
What I found is this:
<div id="include"></div>
<script>
$(function(){
$("#include").load("/test.php");
});
</script>
This does work when in test.php is a simple code like:
<?php echo 'echo world'; ?>
But it doesn't work when I want to grab Wordpress variables.
<?php $current_user_id = $current_user->ID;
echo $current_user_id; ?>
There's nothing outputed. No error. Just a blank.
It's like it doesn't send the variables to the test.php. So test.php doesn't echo values filled. When I use <?php include 'test.php';?> it does fill the variables and works as I wanted.
So basically, .load simply loads the file.
But I want <?php include;?> to be executed instead of .load.
When I use <?php include;?> I can grab variables, which I need.
Is there other command instead of .load to do that?
Any help would be appreciated.

Related

how do I pass a variable/value via URL to another PHP file (via HTTP) using the include function? (PHP)

I'm trying to read the a variable/value that was passed in the URL.
as an experiment, I started with 2 simple files.
test.php
<?php
//option1
//include "http://myhost.com/test2.php?tempvar=testonly";
//option2
//$tempvar = "testonly";
//include "http://myhost.com/test2.php";
//option3
$tempvar = "testonly";
include ("test2.php)";
?>
test2.php
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
console.log(getURLParameter("tempvar"));
$("#test").html(getURLParameter("tempvar"));
function getURLParameter(name) {
return (decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search.toLowerCase())||[,""])[1].replace(/\+/g, '%20'))).toUpperCase()||"";
}
});
</script>
<div id="test"><?php echo tempvar; ?></div>
some observations:
If I call http://myhost.com/test.html?tempvar=testonly from my browser tab, it works fine (i.e. testonly is displayed on the page)
if I call http://myhost.com/test.php from my browser, no "testonly" is displayed on my page. So it seems my getURLParameter javascript function is not finding the tempvar that was passed via include. According to the include manual (example 3), this should work.
Only option3 works in my test.php BUT in my situation, this is useless to me as test2.php can be in another host. and it's possible test2.php can be replaced by another programming language like java (.jsp) or coldfusion or even just plain HTML and DB communication is handled through AJAX calls.
According to phpinfo, the server is using PHP 5.1.6. maybe I need a specific PHP extension to make it work?
Thanks a lot
change
<?php echo tempvar; ?>
To
<?php echo $tempvar; ?>
Btw, you are just declaring a variable and include the second file. If you want to pass a variable from one page to another you need to use:
Depends how you pass the variable (GET or POST)
GET
<?php echo $_GET['tempvar'];?>
POST
<?php echo $_POST['tempvar'];?>
Or you can use for both
<?php echo $_REQUEST['tempvar'];?>

Advanced PHP url getting

Okay, so I have two pages, home.php and page.php. On home.php, I load page.php onto it using jquery. In the url of home.php there is a GET value, u. I want to access that GET value on page.php. You can see that I tried the regular $_GET method, but this does not work. If on page.php I can just get the url of home.php with the GET variables in the url, I can get the info from there. But right now I cannot get the url of home.php, it just gets page.php. I hope that makes sense and thanks for the help!
Home.php
<div id='holder'></div>
<script>
$('#holder').load('page.php');
</script>
Page.php
<?php
$u = $_GET['u'];
echo $_SERVER['PHP_SELF']; // echo's "page.php". I need it to echo "home.php?u=test"
//some php
?>
What you can do is pass the $_GET variable to the requested page.
Do it like this:
<div id='holder'></div>
<script>
$('#holder').load('page.php<?php echo "?u=" . $_GET[" u" ]; ?>');
</script>
Stackoverflow doesn't parse it the way I want but I'm sure it works.

Getting another URL's Javascript variable in PHP?

I was wondering if there was some way to get a javascript variable from another url into a variable within PHP?
For example:
A page called exampleurl.com/page (which I do not have control over) with some inline javascript like:
function examplefunction() {
evar = "http://ikmp.co/Usdfio1";
}
Assuming there's HTML and other javascript surrounding this variable I want, how would I be able to retrieve it? Is there a special function to do this, or would it be easier to simply parse the HTML through PHP and then trim the variable to what I want?
In theory, the PHP would have a URL set to retrieve the JS from, and it would echo only what is in that variable.
PHP code (hypothetical) at mysite.com/getvariable.php:
$url = 'exampleurl.com/page';
$js_var = get_js_data($url, $evar);
echo $js_var;
Thanks in advance to anyone who can help me out here!
how about this:
HTML FILE:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<div id="result"></div>
<script>
var javascriptVar ='pie'; //this is whatever the js variable you want it to be
$('#result').load('path/to/my/myphpfile.php?var='+javascriptVar );
</script>
myphpfile.php
<?php
$javascriptVar =$_GET['var'];
echo $javascriptVar;
?>
notice the php code HAS to be in a seperate file than the html for this to work

Assigning output of external Javascript src to avoid script hang out

I have two websites: domain1.com and domain2.com.
The script on domain1.com/external.php is:
<?php
echo <<<ots
<!--
document.write('Hello World!');
//-->
ots;
?>
I want to execute this external.php script on domain2.com, so I use:
<script src="http://domain1.com/external.php"></script>
The problem is - the Javascript often hangs out, so I wanted to include the < script.. right below the < /body>. However, I must print the Hello World! text in a specific place on the page (ie. right after the </head>).
Question - can I include the < script.. right below the < /body> to assign the output somehow and then put this variable on the page after the script executes?
Or any other similar solution? I cannot use JQuery.
You can use innerHTML to replace the content of one element on the page:
document.getElementById('idOfTarget').innerHTML='Hello World!';
Please note that you can modify the element only after it's created, so put the script before </body> if possible or use window.onload event:
window.onload=function(){
document.getElementById('idOfTarget').innerHTML='Hello World!';
};
See also this mine earlier answer to see how to replace an element by the classname or the tagname.
If you need to use PHP in your script, then you still can do that:
window.onload=function(){
document.getElementById('idOfTarget').innerHTML='<?php
echo "Hello World!";
?>';
};
Please note that if you want to update a lot of elements, you may want to use AJAX/JSON.
You can but you have to use in your external.php file
header('Content-type: text/javascript');
So it's possible to use it like
<script src="http://domain1.com/external.php"></script>

basic php question. adding javascript to .php page

Hi i am not a php developer, ive never touched it before. but i have been asked to add a google shopping cart tracking code to a website. when someone completes an order then get sent to finishorder.php. when i go the finishorder.php file it looks like this:
include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();
which just looks like server script to me (coming from a .net background), so i presume i cannot add the javascript here, how does php decide get the layout for this page? how can i add the javascript code to this page.
You can do this:
include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();
echo '<script type="text/javascript">YOUR JS HERE</script>';
OR
<?php
include(dirname(__FILE__)."/init.php");
$GLOBALS['ISC_CLASS_ORDER'] = GetClass('ISC_ORDER');
$GLOBALS['ISC_CLASS_ORDER']->HandlePage();
?>
<script type="text/javascript">YOUR JS HERE</script>
Hmm?
But I think that HandlePage() method will do something with our page so I'd look inside this method Class ISC_ORDER->handlePage() what it does... You can then echo Your within this method on appropriate place...
EDIT:
<?php
echo '<script type="text/javascript">//<!--
alert("Hello to multiline JS script");
alert("Do You get it?");
//--></script>';
?>
You can add javascript inside a php code as
<?php echo "<script> alert('this is a javascript code')</script>"; ?>
You can add script in PHP page by 2 ways
The first way is to add it in PHP tags
<?php
//PHP CODE
if($_POST['submit']){
echo '<script>alert('Hello')</script>';
}
?>
The second way is to add it after PHP code
<?php
//PHP CODE
?>
<script>
alert('Hello');
</script>

Categories