I have a website Im making. I have made a php file that pulls data from a database and displays it in HTML (the php and html are all within a php file). How do I make it so that I can call the external php file and load the data into a HTML document and also make it work with CSS allocated to that HTML doc?
ive tried this:
<script src="/some_local_link/data.php"</script>
and:
<?php include="/some_local_link/data.php"; ?>
but it doesnt work. Nothing is loaded. Can I remove the HTML from inside the PHP or how do I format it? It has and tags and everything but those are already in the main HTML file.
I was having trouble including files as well. Here is what I found.
Includes in PHP can only pull files from the current directory
<?php include('data.php'); ?>
Or directories up the "ladder"
One level
<?php include('../data.php'); ?>
Two levels
<?php include('../../data.php'); ?>
To my knowledge it's not possible to jump around directories for includes in php
You should use AJAX to load the data into the HTML.
$.ajax({
url: '/some_local_link/data.php',
type: 'GET',
dataType: 'html',
success: function(data){
$( "body" ).append(data)
}
});
Related
I have a folder 'js' with the jQuery function and the PHP script.
In another folder I have the HTML file. So this is the structure by now:
/server/js/global.js
/server/js/script.php
/server/html/student.html
The JavaScript code is called from HTML and its working fine, but the jQuery post function doesn't call the PHP script.
If I move the HTML file into the 'js' folder it works perfectly and the PHP script is called fine.
Why does this happens?
I mean: What is the path represented on the next path?
$.post('script.php',...);
Is it calling a file from the JavaScript folder or from the HTML folder?
The problem might be that the Javascript file global.js you are calling is using the path that all Javascript files use, which is the one relative to the webpage you are viewing at that moment. Check to see if this is the problem (like a relative path) and make sure the path is absolute instead of relative. So for example in the global.js the part that says:
$.post(
'script.php',
{ nameValue: name.value },
change it to:
$.post(
'/path/to/file/script.php',
{ nameValue: name.value },
As an example it will look like this for your case:
$.post(
'/server/js/script.php',
{ nameValue: name.value },
This way it will work for in any case that calls global.js out since the absolute path will not depend on where the viewing file is. It also adapts to any place you put your html file so there is no need to edit the file in the future (Assuming the absolute path will always be in the same place).
It depends how you're calling the JS code from your HTML file,
but try using JQuery post like this:
$.post('../js/script.php', function(data) { } );
I have this change request to move all my inline javascript to an external file. I tried a simple copy-paste to a new file, but no luck. I'm getting error at the following line:
var grp_list = <?php echo json_encode($arr_grp); ?>; and
url: "<?php echo $_SERVER['PHP_SELF']; ?>"
I have atleast 20 such occurrences. How do I replace these php variables in external javascript??
I checked lot of forums but did not find any solution.
Thanks a lot for your help!!
Well, you have JavaScript dinamically "assembled" in your PHP script. The easiest solution is to forget about translating the variables. Instead, put all JS code, including the PHP portions, in a PHP file which will pretend to be a JS file, using a custom header. So, your "JavaScript" file will be like this:
<?php
// Send a custom header, so that it will be interpreted as a js file.
header("Content-Type: application/javascript");
?>
JavaScript and PHP mixed code will go in here, with no modifications
Save this file as something like "javascript.php". Then, in your main HTML or PHP file, include it as:
<script src="javascript.php"></script>
That's it! The javascript.php file will be interpreted as a PHP file in the server and retrieved by the browser as JS. Only pay attetion on the kind of processing the PHP in the javascript file does: it may depend on the context you had in the main script, so additional adjustments may be necessary.
In short, you can't pass the PHP variables directly to an external JS file without some work in PHP generating the files, then sending custom headers to treat the file as JavaScript (edit: see post by Marcovecchio if this sounds like a likely solution)... a quick solution is to pass the variables inline so they are global, then use them inside your external file. This will allow for the majority of your JavaScript to be in external files, but also allow you to pass your variables from PHP to JS.
By no means is this the best solution, but it's more than likely the easiest to get working.
Here's an example:
<script type="text/javascript">
var grp_list = <?php echo json_encode($arr_grp); ?>;
var url = "<?php echo $_SERVER['PHP_SELF']; ?>";
</script>
<script type="text/javascript" src="external.js"></script>
Alright, so I'm trying to embed a php script that outputs a gallery of images onto my html div container. However, after an hour or so of searching I'm coming up clueless.
http://craftedbyfrank.com/test/instagram.php
That's the link to the php gallery script.
I want to display it onto the main page inside that div.
[REMOVED] - thanks!
I've tried the following, but I believe it's just wrong. I get no output.
<div id="container">
<?php include "instagram.php"; ?>
</div>
I'm trying to avoid actually pasting the php script into the html file.
php is not being parsed on your server. If you view the html source you see the actual php code you are using to include the file. Make sure that php is installed or you're not embedding the php from a cms that's encoding your tags.
You could possibly use AJAX to get the result from the PHP file and write to the containing div.
$.ajax({
type:'GET',
url:'http://craftedbyfrank.com/test/instagram.php',
data:'',
success: function(data){
$('#container').html(data);
}
});
You would possible need to adjust to get your fancy box plugin to work.
As #vletech suggested you can use AJAX to get the result from the php file.
However the main reason why it does not work is because you are trying to execute the php script inside an .html file: e.g. your page loads on - /test/index.html. In order to work the file has to be with a .php extension.
Currently i'm working on a project which requires dynamical content parsed using some simple javascript functions, which i need to load depending on which 'view' is currently using a given user.
I'm having issues using something like this on a .js or .php file:
** .php file that calls javascript.
<script type="text/javascript" src="*.js/.php" />
** .js file
<?php header(Content-type: 'application/javascript'); ?>
alert('this shows correctly);
<?php if(isset($_REQUEST['view']) {
if($_REQUEST['view']=='private_view') { ?>
var xname = <?php echo $_SESSION['x']; ?>
//Array stuff filled from MySQL queries.
<?php } ?>
<?php if($_REQUEST['view']=='public_view') { ?>
var other_variable = <?php echo $xxx; ?>
$(element).functions();
<?php } ?>
The php code i'm using works correctly, already tested it. I'm also convinced that there are other ways to work this around, like using different files and choosing them with a conditional right on .php where I define script call, but i'm so curious about why this isn't working.
I'm used to work like this wrapping HTML content between brackets, to hide or show depending on given conditions.
The output of this will be only alert call, no PHP error/warning/notification,
I can't seem to find a correct way to do this, have been searching for a while but only find how to parse .js as php modifying .htaccess file.
You should simply rename the .js file to a .php file.
How to write a PHP code (using public id) that can embed into HTML file (abc.html) when we open that HTML file that has to call another PHP file(there we can insert stats of that file into database).
if i cannot do this in php, is there any other way except renaming html file extension to php extension
Do you mean this?
<html>
...
<body>
<?php echo "test"; ?>
</body>
</html>
or
using exec('php foo.php'); to call another php file?
or
including another php file using
include 'foo.php';
require 'anotherfoo.php';
EDIT:
Or you can use ajax to call a .php file to the abc.html file.
$.ajax({
type: 'POST',
url: 'foo.php',
data: 'username=myuser&id=123456',
success: function(result) {
/* do something with result here */
}
});
You need to have the jquery library which can be downloaded here.
If I understood you correctly, you're wanting to use the PHP include function. You may also want to check PHP's include_once function depending on what you're doing.
You will need to make sure the "HTML" page is actually a PHP page. If your page is already written and exists as HTML and not PHP, you will need sufficient privileges to edit the MIME types and associate .html (or whatever file extention you're using) with PHP processes on the server side. Otherwise you will need to recreate the page as a PHP page using the .php file extension.
For example you may have code like this in the initial file:
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>My Web page title</TITLE>
</HEAD>
<BODY>
<p>Welcome to my website</p>
<?php include('this_page.php'); ?>
</BODY>
</HTML>
This will call the this_page.php file and allow it to execute any scripts within it.
I hope this helps you.
You cannot embed PHP into an web page, PHP is a server side scripting language, which means it is only executed on the server. The user does not even know whether you are using PHP (except for the .php extension, but that means nothing.)
You can however call a PHP script from another PHP script, e.g.
exec('php script.php');
include(other_file.php), include_once(other_file.php), require(other_file.php), require_once(other_file.php) ...
or
use an iframe or exec(other_file.php)
Within your HTML you could use a tag such as an image or script to call your php script. This could be useful if you want your PHP to do something simple such as to increment a counter in a database.
<img src="/path/to/script.php" alt="" />
This is trivial as long as you don't want the executing PHP to return anything visible within the page. If you use an image tag, after you've finished with whatever processing you are doing, it might be best to have your PHP return an image of some sort - but it could be as trivial as a 1px by 1px transparent gif.
$im = file_get_contents('/path/to/your/image/transparent.gif');
header('content-type: image/gif');
echo $im;