how to write a php embed code that calls another php in a html page

7

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

888

Answer

Solution:

Do you mean this?

<html>
...
<body>
<?php echo "test"; ?>
</body>
</html>

or

usingexec('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.

729

Answer

Solution:

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.

160

Answer

Solution:

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');
816

Answer

Solution:

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)

699

Answer

Solution:

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;

People are also looking for solutions to the problem: php - Validate email and ban certain domains

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.