Place PHP results inside HTML page
You can do this with AJAX. It might look a bit challenging, but it is frankly much simpler than many think. In fact, it's pretty easy.
Ajax goes in your javascript code, and looks like this:
$('#stSelect').change(function() {
var sel_stud = $(this).val();
//alert('You picked: ' + sel_stud);
$.ajax({
type: "POST",
url: "your_php_file.php",
data: 'theOption=' + sel_stud,
success: function(whatigot) {
alert('Server-side response: ' + whatigot);
} //END success fn
}); //END $.ajax
}); //END dropdown change event
Note that the data from the PHP file comes into your HTML document in the success function of the AJAX call, and must be dealt with there. So that's where you insert the received data into the DOM.
For example, suppose your HTML document has a DIV with the id="myDiv"
. To insert the data from PHP into the HTML document, replace the line: alert('Server-side response: ' + whatigot);
with this:
$('#myDiv').html(whatIgot);
Presto! Your DIV now contains the data echoed from the PHP file.
The ajax can be triggered by a change to an control's value (as in the above example), or just at document load:
$(function() {
//alert('Document is ready');
$.ajax({
type: "POST",
url: "your_php_file.php",
data: 'Iamsending=' + this_var_val,
success: function(whatigot) {
//alert('Server-side response: ' + whatigot);
} //END success fn
}); //END $.ajax
}); //END document.ready
Look at this example for ideas on how it works.
Note that the above examples use jQuery, and therefore require this reference in the tags of your page:
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
How to retrieve the contents of PHP page in HTML page?
PHP cannot modify the content of the page after it has been served to the browser. However this would be trivial with a JavaScript Library such as jQuery. or by using AJAX to call the php script to update an element. There are examples on StackOverFlow like: Change DIV content using ajax, php and jQuery
How to call output of a php page in an html page in a specific div?
Since you didn't mention via jquery or ajax I am assuming you are going with a simpler old fashioned way So here is the code
SEND YOUR MESSAGE BACK TO HTML PAGE LIKE THIS
$msg = "hello"
header("Location: your_html_page.html?msg=".$msg);
IN HTML DO THIS
<div id="div1">
<?php
error_reporting(0);
echo $_GET['msg'];
?>
</div>
USING JQUERY :
Post the data to php
$.post("your_php_page.php",{data:data},function(returned_data_from_php){
$("#div1").append(returned_data_from_php); // or
$("#div1").html(returned_data_from_php);
});
Using Ajax
$("form#postit").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
url: "your_php_page.php",
type: 'POST',
data: formData,
async: false,
success: function (returned_data_from_php)
{
$("#div1").append(returned_data_from_php); // or
$("#div1").html(returned_data_from_php);
},
cache: false,
contentType: false,
processData: false
});
return false;
});
PHP
what ever you want to return from php it should be echo and there are many ways to do that
(Single string) echo "Success";
(Multiple Strings) echo $msg1.$msg2;
(Array data)
for example ...
$data = array();
$i = 0;
while($numbs = mysql_fetch_array($result))
{
$row_numb = $numbs['id'];
$data[$i] = $row_numb;
$i++;
}
echo json_encode($data);
// this will pass a json type object to the java script
When you get to javascript you want to access all the elements of the array one by one well that's easy too
JAVASCRIPT
parsed_it = JSON.parse(data); // data is the json type you got from your php
so parsed_it[0],parsed_it[1],parsed_it[2]
and so on you can access all the data
hope it helps !!
How to place output from an external php file to a specific place on the page
<?php
if (isset($_GET['outputHtml']))
{
echo "<p class='commentBox'>" . "This is from external PHP" . "</p>";
echo "<p class='commentBox'>" . "This is also from external PHP" . "</p>";
include 'writephp.php';
}
This in your write.php is not correct, see writephp.php
is a complete HTML page with <html>
<head>
and <body>
tags and you are including it after echoing some <p>
tags, that won't make your new boxes appear magically inside the body of the page, you would be creating some invalid markup with the two <p>
at the beginning of the file followed by the <!DOCTYPE><html>
etc...
EDIT
Regarding the update to your question that now reads "How place output from an external php file to a specific place on the page"
As @Robert Seddon-Smith correctly noted in his answer, using include
will make the content appear exactly at the point when the call is made, so if you wanted to include your new boxes in your main file then the call to include
should be backwards, that is, you should include the content inside the main file, you can use this example to test it:
Modify write.php
to just echo the boxes when the $_GET variable is present
<?php
if (isset($_GET['outputHtml']))
{
echo "<div class='commentBox'>" . "This is from external PHP" . "</div>";
echo "<div class='commentBox'>" . "This is also from external PHP" . "</div>";
}
?>
And make the include call in your main file writephp.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Php </title>
<link rel="stylesheet" href="writephp.css">
</head>
<body>
<div id="allContent">
<?php for($i=0; $i<3; $i++): ?>
<div class="commentBox">
<p> Just a box</p>
</div>
<?php endfor; ?>
<?php include 'write.php'?>
<a href="writephp.php?outputHtml">Php output</a>
</div>
</body>
</html>
This will include your two new boxes inside the container when you click in the link.
Also you will note that there is nothing special about styling content from an included file, that is because in the end all the markup is rendered as a single HTML page, it doesn't matter if the content is echoed from the main page or the included file, it will obey any rules in your CSS
display php result in html page
You can't run PHP in .html files because the server does not recognize that as a valid PHP extension unless you tell it to. To do this you need to create a .htaccess file in your root web directory and add this line to it:
AddType application/x-httpd-php .htm .html
This will tell Apache to process files with a .htm or .html file extension as PHP files.
for more info please read this http://php.about.com/od/advancedphp/p/html_php.htm
How to display php file output inside a html div?
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.
Display PHP inside of an HTML page
You'll need to use .htaccess to run your file through the PHP parser. Something similar to:
<FilesMatch "\.(htm|html|shtm|shtml)$">
SetHandler application/x-httpd-php5
</FilesMatch>
Related Topics
Multiple Auto Increment in MySQL
Fatal Error - Too Many Open Files
How to Implement Single Sign-On (Sso) Using Microsoft Ad for an Internal PHP App
Scrape Web Site Generated by JavaScript
Apple Push Notification with Sending Custom Data
How to Remove a Key and Its Value from an Associative Array
Windows 7 PHP + Symfony2 Terribly Slow
MySQL Transactions Within Transactions
How to Get PHPunit Mockobjects to Return Different Values Based on a Parameter
Pdo Multiple Fetching of Same Query
Fatal Error: Call to Undefined Function Mcrypt_Encrypt()
Zip All Files in Directory and Download Generated .Zip
PHP Sessions Login with Remember Me
Printing to Pos Printer from PHP
Clean Way to Throw PHP Exception Through Jquery/Ajax and JSON
Why Is It Good Save to Save Sessions in the Database