0

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

1

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

No. I have to insert a php code in a html page(abc.html) that can call another php file.
No. You cannot do that. php is a server-side scripting. how about renaming your page into abc.php?
Add a small unvisible image <img src="other_file.php?id=123">
is there any other way except renaming my file to php extension
Use ajax... I guess that's the only way. You have your abc.html file and the ajax calls a foo.php file to be passed to the abc.html file
|
1

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.

7 Comments

But my page extension is .html not .php
i want this to be happen in HTML extension only.
So as I said update your application/x-httpd-php MIME type to include html, it will be the only way for you to accomplish what you want to do buddy. You may need to contact your website host and have them do it depending on what permissions you have in your account.
wr, do you actually understand my answer?
Is there any other way to call if not using php.
|
0

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

Comments

0

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)

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.