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.
Related
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)
}
});
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;
I was wonder if there was a way to take the html results that was created from a php script and take that html code generated and put it inside another php script?
The problem I'm having is that I'm working inside a huge php script and I always have errors when I'm working inside it. I want to make additions to the code, but it never works. Files aren't opening. However, when I work outside it and within the same folder, my code works.
So I'm thinking if there's an "Iframe" for php code or maybe I should use an iframe instead?
Can't you just use include or require to include the code from the other script?
Create a function myFunction to output whatever html you want.
Put that code in a file whatever.php
Call include('whatever.php') from your other PHP file
Call myFunction() and your html will be outputted.
I'm learning all this web programming stuff after years writing .EXE Windows programs so bear with me.
I developed a basic .php and mysql website that works fine.
But I went to add javascript code to my index.php and I don't think the javascript code is executing.
My index.php has the form:
<?php
require_once blah blah
call_my_php_functionBlah();
?>
Then I added this code inside the php blocks of the '<\?\php' and "\?>" as follows:
<script type="text/javascript">
// some known-good javascript code that displays an image
</script>
Nothing showed up.
So I thought "ah-HAH, I blew it, all I need to do is -- move the javascript code outside
of the php block, at the bottom of index.php, and surely I'm good to go."
And still, Nothing showed up.
I checked the source of my 'known-good' javascript code and it said 'embed this javascript code
in your HTML file' so I thought "wow, I guess I need an index.html or something here."
So my questions:
1) should my index.php be able to run the javascript block of code?
I'm guessing 'No because index.php executes on the server and javascript runs on the client machine.'
2) How should I architect this if I want to keep my index.php, whose code works fine and I don't want to mess with it?
I'm thinking that this is an extremely basic client/server, php and javascript script organization issue that every web programmer knows how to handle, but like I said, I'm new to all this. I read in the archives about .htaccess etc. etc. but I
bet there's an easier way, and I'm not sure if the stuff I read applies.
the file name extension is completely irrelevant
PHP executes on the server and doesn't care at all about any Javascript
code inside <?php ?> tags must of course be valid PHP code to be executed by PHP
your browser receives whatever the result of your PHP execution is
you can use PHP code to output Javascript or simply have Javascript on the same page outside of <?php ?> tags
only whatever the browser receives matters, so use View Source
look at the browser's Javascript Console to debug client-side Javascript problems
Then I added this code inside the php blocks of the '" as follows:
Dont add your script inside the php block bring it outside php block.
After you are done with script you can reopen php block and write php again
index.php can run javascript, just that You need to echo the javascript code to put it in the page.
Anything that appears inside your php open/close tags has to be echoed or printed to be rendered to the html page. Anything outside your php open/close tags should appear in your html page but whether it works correctly or not is another matter not necessarily related to your php. The php interpreter doesn't run your javascript code, however, so it can't just sit inside your php tags.
Javascript will run inside .php file.
But you have to write outside the tags.
Eg:
index.php
<?php
echo "Helloooooo";
?>
<script>
function TestingMyFirstScript()
{
alert(1)
}
</script>
Javascript will execute in a PHP file but not inside of a PHP block. It executes in the server, yes and anything coming from PHP should be printed out to see. You should have the JS code outside of the PHP block and it can be anywhere in the page e.g.
It depends how to mix/match the code but of course keep it clean and easy to read (and debug).
<?php
// code here
?>
<script type="text/javascript">
// JS here
</script>
<?php
// some more code here
?>
Answer to both of your question is that you dont have to create a separate html file to execute your JS code. You can have HTML, JS, and PHP code in the same file. PHP code inside the PHP tags will be processed on the server and replace with HTML. The server generated HTML will be combined with other HTML present on the .php file and sent to the browser as one HTML.
There must be some error in the JS code which is causing the script to fail.
<?php
require_once blah blah
call_my_php_functionBlah();
?>
<script type="text/javascript">
// some known-good javascript code that displays an image
</script>
<?php
// other php code
?>
Most of the above comments should help you with your PHP + JS problem. However, if you are still getting errors with your output, try using:
alert("breakpoint 1");
//some code
alert("breakpoint 2");
throughout your Javascript function (it will show you where the code is failing). Good for beginners debugging. Also check out http://www.jslint.com/
Before I ask this question I must point out that I have tried to search for EVERYTHING!
My question is how can I run javascript from an external file instead of inside my php / html. What I'm trying to do is.
function ClearForm() {
document.form.message.value="";
}
function comeBack(){
if (document.form.message.value == "") {
document.form.message.value="What's on your mind?";
}
}
I have included<script type="text/javascript" src="javascript.js"></script> in the <head> and I have a file in the root called javascript.js and my php file is in the root too so that shouldn't be the problem! But how do I run that pieces of code you see above in the javascript.js file instead of in my php file. It work's fine if I have it in the php file but I want to separate things!
I have also tried to give the form / input field an id and then use getElementById in the external JavaScript file.
But as you can see and hear I'm kinda new to JavaScript so I'm apparently doing something wrong here.
If the above code is the only thing in your Javascript.js file, then you need to call the functions to run the code.
You've included the external Javascript file correctly - however, because all of your JS is included within functions, these function/s must be called before the code will run.
A call to 'ClearForm()' or 'comeBack()' from within your PHP file should run the code.
That JS file will have to be in the same folder as your PHP page.
Test whether the file is found or not by adding this line at the top of the js file
alert('js file found OK!');
document.form is an array, so if you only have one form use:
document.forms[0]
Also depending on which browser you use, find and install some Developer Tools to help you identify these errors.
You have declared those functions in the <head>. All fine.
The question is when do you want to call/run those functions?
If you simply want to run them at the end of the page, then you can add another external javascript file and include it using <script src="my_external_file.js"> right before the </body> tag.
Otherwise, you have to declare onXXX handlers, like onLoad() for the document, onClick() for certain elements, onSubmit() for forms, etc. These, too, can be declared in an external file, but specified after the relevant elements are loaded.