PHP file rendering output as text why..? - php

I have two PHP files, both of them are contact forms. One file has HTML 4 doctype and the other has HTLM 5 doctype.
When I open the first file it renders properly i.e. completely loads web page with full colors, graphics etc. but when I render the second file it just displays the HTML code in the browser in the text format.
the content of the first file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Discover Brazil</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="layout.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="js/cufon-yui.js" type="text/javascript"></script>
<script src="js/cufon-replace.js" type="text/javascript"></script>
<script src="js/Myriad_Pro_400.font.js" type="text/javascript"></script>
<script src="js/Myriad_Pro_600.font.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#comp').focus();
});
</script>
<style type="text/css">
</head>
<body id="page1">
<?php
if(isset($_POST['sent'])){
// Contact subject
$comp= $_POST['comp'];
$ur_name = $_POST['ur_name'];
$position = $_POST['position'];
$add = $_POST['add'];
$city = $_POST['city'];
$pin =$_POST['pin'];
$state = $_POST['state'];
$tel =$_POST['tel'];
$Cell =$_POST['cell'];
$fax = $_POST['fax'];
$email =$_POST['email'];
$website = $_POST['website'];
$pro = $_POST['pro'];
$busi= $_POST['busi'];
$target = $_POST['target'];
// Details
$message="Name of the Company :- " . trim($_POST['comp']) .
"\n\nName :- ". trim($_POST['ur_name']) .
"\n\nPosition :- ". trim($_POST['position']) .
"\n\nAddress :- ". stripslashes(trim($_POST['add'])) .
"\n\nCity :- ". trim($_POST['city']) .
"\n\nPin Code :- ". trim($_POST['pin']) .
"\n\nState :- ". trim($_POST['state']) .
"\n\nTelephone No :- ". trim($_POST['tel']) .
"\n\nCell No :- ". trim($_POST['cell']) .
"\n\nFax No :- ". trim($_POST['fax']) .
"\n\nEmail ID :- ". trim($_POST['email']) .
"\n\nWebsite :- ". trim($_POST['website']) .
"\n\nCompany Profile :- ". trim($_POST['pro']) .
"\n\nBusiness With Brazil for Last 3 years :- ". trim($_POST['busi']) .
"\n\nTarget for Brazil for 2014 :- ". trim($_POST['target']);
$to = "email#domain.com";
$subject = "This is subject";
$header = "From:$email \r\n";
$sendmail = mail ($to,$subject,$message,$header);
}
?>
<!-- header -->
THe content of second file
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sl</title>
<meta name="description" content="Place your description here">
<meta name="keywords" content="put, your, keyword, here">
<meta name="author" content="Templates.com - website templates provider">
<meta charset="utf-8">
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
<link rel="stylesheet" href="css/layout.css" type="text/css" media="all">
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<script type="text/javascript" src="js/maxheight.js"></script>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/cufon-replace.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_300.font.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_400.font.js"></script>
<script type="text/javascript" src="js/jquery.faded.js"></script>
<script type="text/javascript" src="js/jquery.jqtransform.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript">
$(function(){
$("#faded").faded({
speed: 200,
crossfade: true,
autoplay: 3000,
autopagination:false
});
$('#domain-form').jqTransform({imgPath:'jqtransformplugin/img/'});
});
</script>
<!--[if lt IE 7]>
<script type="text/javascript" src="http://info.template- help.com/files/ie6_warning/ie6_script_other.js"></script>
<![endif]-->
<!--[if lt IE 9]>
<script type="text/javascript" src="js/html5.js"></script>
<![endif]-->
<style type="text/css">
<!--
.style1 {font-size: 16px}
-->
</style>
</head>
<body id="page1" onLoad="new ElementMaxHeight();">
<?php
if(isset($_POST['sent'])){
// Contact subject
$author= $_POST['author'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$details = stripslashes($_POST['details']);
// Details
$message="Name :- " . trim($_POST['name']) .
"\n\nEmail :- ". trim($_POST['email']) .
"\n\nSubject :- ". trim($_POST['subject']) .
"\n\nMessage :- ". stripslashes(trim($_POST['details']));
$to = "email#domain.com";
$subject = "This is subject";
$header = "From:$email \r\n";
$sendmail = mail ($to,$subject,$message,$header);
}
?>
<div class="tail-top">

Make sure the extension of the second file is a recognized document extension.
So if its, secondFile.txt or secondFile.php.bk , rename it to secondFile.php
edit: just a small guess, but have you tried closing the body and html tags with:
</body>
</html>
at the bottom?

Your problem definition is not very clear, your question/description could also describe several different problems.. So chances am naming potential causes that you already are aware of, but for completeness sake:
First of all, to see a HTML file as a rendered HTML page, you need to open it in a browser. This works local (from your own disk/computer) and from websites (via http connection)
Second, to have PHP code actually run you need to have some computer execute it before sending it to you. This usually happens on the computer that accepted your http connection, if it recognizes and can execute your *.PHP file.
Then there are things that can also complicate stuff, like the content-type in the header that PHP can alter before returning a webpage.
Update: Thanks for attaching source.
The reason a browser sometimes displays text instead of html, is related to the content-type. Mostly this can be derived by file extention, the doctype or other html properties.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
It could help to add this to your 2nd file, or it may be even better to properly fix the file so that the browser can correctly guess that its html to begin with. (unroot the problem at its cause)

Related

PHP/ jQuery Mobile Functionality/ Style not showing

I am trying to create a collapsible div (using the jQuery Mobile feature) using the data from a MYSQL database. The collapsible styles are not being applied and i cant figure out what the problem is?
possible the php script?
here's what i have
page.html
<html>
<head>
<title>My App</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes">
<!-- css files -->
<!--BoilerPlate-->
<link href="css/normalize.css" rel="stylesheet" type="text/css" />
<!--<link href="css/main.css" rel="stylesheet" type="text/css" />-->
<!--end boilerplate styles-->
<!--my custom css-->
<link href="css/mainstyles.css" rel="stylesheet" type="text/css" />
<link href="css/buttons.css" rel="stylesheet" type="text/css" />
<!-- jQuery mobile js/css -->
<script src="js/jquery-3.1.1.js" type="text/javascript"></script>
<link href="js/jquery.mobile-1.4.5.css" rel="stylesheet" type="text/css" />
<!-- end of custom styles -->
<!-- END css declaration -->
</head>
<body>
<div id="canvasArea"></div>
<canvas id="cnvChart"></canvas>
<!-- JAVASCRIPT Libraries -->
<script src="chart.js/Chart.js" type="text/javascript"></script>
<script src="js/lineGraph.js" type="text/javascript"></script>
<script src="js/jquery-3.1.1.js" type="text/javascript"></script>
<script src="js/jquery.mobile-1.4.5.js" type="text/javascript"></script>
<script>
// Listen for orientation changes
window.addEventListener("orientationchange", function () {
location.reload(true);
// Announce the new orientation number
}, false);
var canvasAreaDiv = document.getElementById(
'canvasArea');
if(window.innerHeight > window.innerWidth) {
//portrait mode, so show the php script!
$(canvasAreaDiv).load("php/reflective-notes.php");
}
else {
//show the graph
showGraph();
}
</script>
</body>
script.php
$username = $_SESSION['username'];
$reflectiveMessageQuery = "SELECT date, notes FROM table WHERE
userName = '$username'";
$result = mysqli_query($conn, $reflectiveMessageQuery) or die(mysqli_error());
$count = mysqli_num_rows($result);
$rowCount = 0;
while ($row = mysqli_fetch_array($result)) {
$date = $row['date'];
$note = $row['notes'];
if($note === NULL){
$note = "no notes...";
}
echo'<div data-role="collapsible"><h1>'. $date . '</h1> <p>'.$note.'</p>'
.'</div>';
}
?>

include head with php

I have trouble with the contruction of a working well formmated DOM, through php.
The source-code is diplayed right, but all the dev-tools of Chrome, Firefox and Edge, display the head-tag inside the body-tag. Can you please help me to spot the mistake, beacuse the frontend is now faulty displayed.
it look like this:
php-snippet:
<?php
session_start();
//doctype
echo "<!DOCTYPE HTML>\n";
//html
echo "<html>\n";
//html-head
echo "<head>\n";
include "inc/head.html";
echo "</head>\n";
//html- body start-end
echo "<body>\n
some content
</body>\n</html>\n";
?>
head.html:
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'>
sourcecode html:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'></head>
<body>
some content
</body>
</html>
You can either use file_get_contents() for this
$content = file_get_contents('head.php');
print $content;
Or use the include function but receive its output.
$content = include('head.php');
print $content;
NOTICE
Keep in mind, that if you decide to use include for this, it will execute the code inside head.php first, which file_get_contents() wouldnt.
Maybe this also helps you.
I would suggest using PHP's output buffer, changing your code to look like this:
<?php
session_start();
ob_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<?php include "inc/head.html"; ?>
</head>
<body>
some content
</body>
</html>
<?php
echo ob_get_clean();
?>

How to keep HTML indentation using DOMXPath?

I am using PHP (DOMDocument and DOMXPath classes to be more precise) to modify the content of an HTML file (for the moment I just change the title in the head tag and the first element of the body which is an h1).
The functionality is there meaning that I am able to modify what I desire into the document but the output is not keeping the indentation of the original file.
What solutions I found online are referring to XML, having to no effect when it comes to HTML.
My question is how can I keep my original indentation after using DOMXPath? It is even possible using this alone or I should consider some 3rd party library?
My PHP code snippet:
<?php
$dom = new DOMDocument;
$dom->loadHTMLFile($dst . '/index.html');
$xpath = new DOMXPath($dom);
$title_tag = $xpath->query('/html/head/title')->item(0);
$title_tag->nodeValue = $_POST['website-title'];
$body_tag = $xpath->query('/html/body/h1')->item(0);
$body_tag->nodeValue = $_POST['website-title'];
$dom->saveHTMLFile($dst . '/index.html');
?>
Original HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<!--[if lt IE 9]>
<script type="text/javascript" src="js/html5shiv.min.js"></script>
<script type="text/javascript" src="js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Hello world!</h1>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
</body>
</html>
Output HTML :
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Test Website</title><link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"><link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css"><link rel="icon" type="image/x-icon" href="favicon.ico"><!--[if lt IE 9]>
<script type="text/javascript" src="js/html5shiv.min.js"></script>
<script type="text/javascript" src="js/respond.min.js"></script>
<![endif]--></head><body>
<h1>Test Website</h1>
<script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/bootstrap.min.js"></script></body></html>
EDIT: After using $dom->formatOutput = true; (thank you PeeHaa for this) before saving the HTML file, the output is the following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test Website</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<!--[if lt IE 9]>
<script type="text/javascript" src="js/html5shiv.min.js"></script>
<script type="text/javascript" src="js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Test Website</h1>
<script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/bootstrap.min.js"></script>
</body>
</html>

php:two calendar is not showing

Two calendar is not showing in one php file. scenario is below
I have a php file name temp. Here i want to show two calendar(previous month and next month).
<!DOCTYPE html>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="left1"><?php include('calendar_previous.php');?></div>
<div id="right1"><?php include('calendar_next.php');?>hello</div>
</body>
</html>
My calendar_previous.php is
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="css/datetimepicker_css.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>BuildUp Real Estate</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="admin.css" />
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="css/jquery.1.4.2.js"></script>
<script type="text/javascript" src="css/jsDatePick_prev.jquery.min.1.3.js"></script>
<script type="text/javascript">
window.onload = function(){
g_globalObject = new JsDatePick({
useMode:1,
isStripped:true,
target:"inputField1"
});
g_globalObject.setOnSelectedDelegate(function(){
var obj = g_globalObject.getSelectedDay();
alert("a date was just selected and the date is : " + obj.day + "/" + obj.month + "/" + obj.year);
document.getElementById("div3_example_result").innerHTML = obj.day + "/" + obj.month + "/" + obj.year;
});
};
</script>
</head>
<body>
<div id="inputField1"></div>
</body>
</html>
My calendar_next.php is almost same to calendar_previous.php with two difference of javascript
<script type="text/javascript" src="css/jquery.1.4.4.js"></script>
<script type="text/javascript" src="css/jsDatePick_next.jquery.min.1.3.js"></script>
When i call calendar_previous.php and calendar_next.php separately it is showing previous and next month properly. But when i include this two php file in temp.php in different div only one calendar is showing. I want to show two calendar in temp.php in different div.
Any help is appreciate.
I guess its because now you are having two window.onload. So, that might be troubling. What you can try is place the contents of both window.onload to the second page and try.
2nd problem could be jquery conflict.
For that follow this
$s = jQuery.noConflict();
Now replace all of the $ with $s in one of your files.
That should resolve your problem. Tell me if it doesn't.

PHP variable as Javascript popup not working

I am using a PHP variable called LeagueLink. When the user is not logged in I want the variable to read the text (Already have a league...) followed by a link to a popup window. So far it is displaying correctly except when I click on the link nothing happens. I think I just have a syntax error from mixing so much PHP and JS, but I can't figure out where. Please help make the popup window link work if you can...
<?php
// this starts the session
session_start();
$_SESSION['userid'];
$message = "";
if ($_SESSION['userid'] == "") {
$message = "You must create an account or sign in to play!";
$LeagueLink = "Already have a league...<a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/signin.php\");' class='two'>Sign In</a>";
} else {
$message = "Hello, " .$_SESSION['userid'] . " make your picks!";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="js/jquery-easing-1.3.pack.js"></script>
<script type="text/javascript" src="js/jquery-easing-compatibility.1.2.pack.js"></script>
<script type="text/javascript" src="js/coda-slider.1.1.1.pack.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="icon" href="http://www.indiana.edu/favicon.ico" />
<title>YourFantasyFootballReality</title>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
<body>
<?=$message?>
<?=$LeagueLink?>
<?=$ActionLink?>
</body>
</html>
JavaScript:newPopup is a function that needs to be defined.
I think what you are looking for is the following: You don't need javascript to open the page in a new window. Just set the target property of the link to _blank.
$LeagueLink = "Already have a league...<a href='http://www.yourfantasyfootballreality.com/signin.php' target='_blank' class='two'>Sign In</a>";
EDIT: If you want it popping up, rather than opening in a new tab, you can resize it immediately after it is spawned. Add the following to your javascript.
function newPopup()
{
var url='http://www.yourfantasyfootballreality.com/signin.php';
windowProperties = "toolbar=no,menubar=no,scrollbars=no,statusbar=no,height=500px,width=500px,left=50%,top=50%";
popWin = window.open(url,'newWin',windowProperties);
}
and keep your PHP the same as before.

Categories