Why is there no output in the given PHP file - php

I am trying to calculate ( by a PHP file ) the diameter, area and circumference of a circle whose radius is given by the user in a html file. User gives the values of the radius and PHP script calculates the above mentioned quantities. The problem is that when I am executing the program, the html takes the input but the PHP script isn't showing the output.
The code is as follows:-
circle.html:-
<html>
<head>
<title>Circle</title>
</head>
<body>
<form method="GET" action="circle1.php">
<center>
<h1>Circle</h1>
<table border= 2 width=200>
<tr align=center>
<td>Radius</td>
<td><input type="text" name=radius size=5></td>
</tr>
<tr align=center>
<td colspan=2>
<input type="submit">
<input type="reset">
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
circle1.php:-
<html>
<head>
<title>Circle</title>
</head>
<body>
<center>
<h1>Circle</h1>
<?php
define("PI",3.14159,TRUE);
GLOBAL $d,$c,$a,$r;
$r=$_GET['radius'];
$d=2*$r;
$c=2*PI*$r;
$a=PI*$r*$r;
?>
<table border = 2 width=200>
<tr>
<td>Diameter</td>
<td><?php echo $d; ?></td>
</tr>
<tr>
<td>Circumference</td>
<td><?php echo $c; ?></td>
</tr>
<tr>
<td>Area</td>
<td><?php echo $a; ?></td>
</tr>
</table>
<h2>Back</h2>
</center>
</form>
</body>
</html>
sorry for such a long post. Any help would be appreciated.

Some server setups such as various Amazon Linux AMIs require you to output HTML through php print functions such as echo to properly execute. If you have this type of setup, then you'll need to execute your server-side PHP code before you output all your client-side HTML.
Try this for circle1.php:
<?php
define("PI",3.14159,TRUE);
GLOBAL $d,$c,$a,$r;
$r=$_GET['radius'];
$d=2*$r;
$c=2*PI*$r;
$a=PI*$r*$r;
echo'
<html>
<head>
<title>Circle</title>
</head>
<body>
<center>
<h1>Circle</h1>
<table border="2" width="200">
<tr>
<td>Diameter</td>
<td>'.$d.'</td>
</tr>
<tr>
<td>Circumference</td>
<td>'.$c.'</td>
</tr>
<tr>
<td>Area</td>
<td>'.$a.'</td>
</tr>
</table>
<h2>Back</h2>
</center>
</form>
</body>
</html>';
?>

Your code could be cleaned up a bit, but it should work. There isn't anything intrinsically wrong with it. I literally cut and pasted it as a test and it works fine.
If the first page (pure HTML) works, but you get nothing for the second (circle1.php) then the first thing to check is if your webserver is configured to use PHP and if the path to the php.ini is accurate because the bottom line is that your PHP parser is out of the loop.

Your code is correct. You must have some problem related to php.ini or apache . Check weather your <?php code is escaping or not by checking source code. . If it is , put this code on the top error_reporting(E_ALL); and try to debug it.

You need to encapsulate your input properties with quotes.
Replace
<input type="text" name=radius size=5>
With
<input type="text" name="radius" size="5">

Related

How to exclude HTML from PHP and PHP file still able to link with the HTML file?

View table<-- this is my example. And code is provided too. I need to separate HTML from PHP, moving HTML to another file but my PHP code still be able to link with it. Is there any idea? I am trying to make something like View model controller.
<html>
<head>
<meta charset="utf-8">
<title>View Records</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p>
Dashboard
| View Records
| Add Admin
| Logout
</p>
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
<th><strong>ID</strong></th>
<th><strong>Username</strong></th>
<th><strong>User Password</strong></th>
<th><strong>Full Name</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
</body>
<?php
$count=1;
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td align="center"><?php echo $row["ID"]; ?></td>
<td align="center"><?php echo $row["username"]; ?></td>
<td align="center"><?php echo $row["user_pass"]; ?></td>
<td align="center"><?php echo $row["fullname"]; ?></td>
<td align="center">
Edit</td>
<td align="center">
Delete</td>
</tr>
<?php $count++; } ?>
</tbody>
</table>
</div>
</body>
</html>```
Separating the php and html is a good start, and helps you see the next step in converting to OOP and then MVC.
MVC at this point is too broad for a simple answer here, but I would recommend this as a first step as it has the underlying principles:
PHP is always at the top; never output anything until all your logic is finished
Load configuration
Work with user input and redirect if POST
Execute business logic
Exit PHP and output HTML. Remember, PHP is essentially a templating language, might as well use it as such
Your code would then look something like this:
<?php
// load database connection, etc
$url = 'your url';
// deal with user input. Always use POST if data will be changed!
if($_POST['action'] == 'delete') {
// delete from admin where id=?
header('location: '.$url);
die;
}
// end "controller" section
// begin "model" section
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
// end "model" section
// begin "view" section.
// Note, you could simply put the html in a separate file and just include it here.
?>
<html>
<head>
<meta charset="utf-8">
<title>View Records</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p>
Dashboard
| View Records
| Add Admin
| Logout
</p>
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
<th><strong>ID</strong></th>
<th><strong>Username</strong></th>
<th><strong>User Password</strong></th>
<th><strong>Full Name</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
</tbody>
<?php while($row = mysqli_fetch_assoc($result)): ?>
<tr>
<td align="center"><?= $row["ID"] ?></td>
<td align="center"><?= $row["username"] ?></td>
<td align="center"><?= $row["user_pass"] ?></td>
<td align="center"><?= $row["fullname"] ?></td>
<td align="center">
<form method="post">
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="<?= $row["ID"] ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</body>
</html>
Notice that following this pattern is laying the groundwork for moving to mvc. But first, start working on your oop chops and move all the business logic into a model object.
After that, you can move the template to its own file, and have the model injected into the view, which lets the view have access to the info it needs, and then render the output.
Meanwhile, you'll need a router (all traffic is rerouted to index.php) and controller, and figure out which interpretation of MVC you will use ;)
Here's a very basic solution.
Make an HTML file as a template. E.g. "template.html". Use HTML comments as placeholders for the data. (I went with comments as it means the HTML remains compliant)
I've left out some of the non-relevant bits, so hopefully you get the idea:
<html>
...
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
<th><strong>ID</strong></th>
<th><strong>Username</strong></th>
<th><strong>User Password</strong></th>
<th><strong>Full Name</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<!--ROW-->
<tr>
<td align="center"><!--ID--></td>
<td align="center"><!--username--></td>
<td align="center"><!--user_pass--></td>
<td align="center"><!--fullname--></td>
<td align="center">
Edit</td>
<td align="center">
Delete</td>
</tr>
<!--ENDROW-->
</tbody>
</table>
</div>
</body>
</html>
Then, in your PHP code, you read in the html, find the row template, and replace the fields as needed:
<?php
// Read the template
$html = file_get_contents('template.html');
// Find the row template
$regRowTemplate = '/<!--ROW-->(.*)<!--ENDROW-->/i';
preg_match($regRowTemplate, $html, $m);
$rowTemplate = $m[1];
// Start building our replacement rows
$htmlRows = '';
$count=1;
$sel_query="Select * from admin ORDER BY id ASC;";
$result = mysqli_query($con,$sel_query);
while ($row = mysqli_fetch_assoc($result)) {
// Start with a fresh copy of the template
$htmlRow = $rowTemplate;
// Replace comment placeholders with values
foreach ($row as $key => $value) {
$htmlRow .= str_replace('<!--' . $key . '-->', $value, $htmlRow);
}
// Append to our rows
$htmlRows .= $htmlRow;
$count++;
}
// Replace the row template with our expanded rows
$html = preg_replace(regRowTemplate, $htmlRows, $html);
// Do something with the html
Source untested, but should give you a good starting point. I kept it pretty raw. If I was doing this for real, I'd allow for the possibility of spaces in the comment placeholders by using a regular expression instead, but for now it's good enough.

Getting a $value into a .php page form table for a calculator

Sorry if this is really basic programming knowledge, but if it is should only take a moment to help me. I've tried lots of things and searches already but can't get it to work.
I'm doing a tutorial from http://www.hungrypiranha.org/make-a-website/online-calculator
I'm making a settings page that will edit a config.php file with a simple price in it. I have set it to 5. I will work out some way to make that editable later with a user interface. One step at a time.
<?php
//config file
$price = "5";
?>
Next I put the calculator script into the index.php page,
but I want to draw out the $price value from the other file and put it into the form table. No matter what I try it doesn't go into the form table.
It goes anywhere I put the code <?php echo $price; ?>, but not into the form table.
<html>
<head>
<title>Net Calc</title>
<link rel="stylesheet" href="styles.css">
<meta name="viewport" content="width=300">
</head>
<body>
<div>
<?php include("config.php"); ?>
<?php echo '<p>Netting Price Calculator</p>'; ?>
<p>Current price per m2 is $<label><?=$price?></label></br>
link to Settings
<?php
if (isset($_POST['valuea'])) $valuea = $_POST['valuea'];
if (isset($_POST['valueb'])) $valueb = $_POST['valueb'];
$answer = $valuea * $valueb * $price;
echo <<<_END
<form method='post' action=''>
<table border='0' width='300px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>Netting Price Calculator</strong></td></tr>
<tr class="calcrow"><td>Height</td><td align="left"><input type='text' name='valuea' value="$valuea"/></td></tr>
<tr class="calcrow2"><td>Width:</td><td align="left"><input type='text' name='valueb' value="$valueb"/></td></tr>
<tr class="calcrow"><td>Price:</td><td align="left"><?php echo $price; ?></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/><input type='reset' value='Reset'/></td></tr>
_END;
?>
<tr class="calcrow">
<td><i>Total:</td>
<td align="left">$<?php echo $answer; ?></td></i>
</tr>
</table>
</form>
</br><?=$price?>
</br><?php echo $price; ?>
</br>
</div>
</body>
</html>
What I put into the $price variable doesn't want to show on the page.
Hope you understand. If you get what I'm asking and know of any better calculator tutorials please let me know.
It's better for everyone to write more readable code.
Also I suggest you to write html outsite php tag. So you will see easier debug.
Your problem is happen around echo <<<_END. So, I suggest you to do like this.
<?php
if (isset($_POST['valuea'])) $valuea = $_POST['valuea'];
if (isset($_POST['valueb'])) $valueb = $_POST['valueb'];
$answer = $valuea * $valueb * $price;
//your form was here but I move it outside of the php tag.
?>
<form method='post' action=''>
<table border='0' width='300px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading">
<td colspan="2">
<strong>Netting Price Calculator</strong>
</td>
</tr>
<tr class="calcrow">
<td>Height</td>
<td align="left">
<input type='text' name='valuea' value="<?php echo $valuea?>"/>
</td>
</tr>
<tr class="calcrow2">
<td>Width:</td>
<td align="left">
<input type='text' name='valueb' value="<?php $valueb?>"/>
</td>
</tr>
<tr class="calcrow">
<td>Price:</td>
<td align="left"><?php echo $price; ?></td>
</tr>
<tr class="submit">
<td colspan="2"><input type='submit' value='Calculate'/>
<input type='reset' value='Reset'/>
</td>
</tr>
I also update your input value from value="$valuea" to <?php echo $valuea?>
because I write it outside php tag. so when you want to display php variable just do <?php and ?> inside html.
I test this your varible inside table form work fine.
little thing
I suggest to put include at first line of file.
<?php include("config.php"); ?>
And if you really need and the script will not work without it. you better use require.
<?php require_once("config.php"); ?>
As I pointed out in comments; the problem here is that you're inside heredoc
and this <?php echo $price; ?> is literally printing that.
If you look(ed) at your HTML source, you would have seen
<td align="left"><?php echo $price; ?></td> rather than the parsed variable assigned.
Simply remove the <?php echo ; ?> and keep $price from inside the form.
However, you will receive undefined variable(s) notices for all the variables you have declared.
You will need to first check if they are set, and/or not empty.
References:
http://php.net/manual/en/function.empty.php
http://php.net/manual/en/function.isset.php
Or using a ternary operator: (See under Ternary Operator)
http://php.net/manual/en/language.operators.comparison.php
Example pulled from the manual on the ternary operator:
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
This can be adapted to variables also and replacing empty by isset.
Error reporting would have signaled that:
http://php.net/manual/en/function.error-reporting.php
HTML stickler:
</br> isn't a valid HTML markup tag; <br/> and <br> are.

PHP not displaying properly

I am having an issue on designing a web page. I want to display a POST variable inside of an HTML table.
Here is the form code from the first page:
<form action="buy.php" method="post">
<input type="text" name="uid" />
<input type="submit" value="Buy Now" />
</form>
This code works fine if I am displaying the POST variable on a normal blank PHP file.
But when I go to use it in an html table it just won't display.
Here is the table code:
<td id="bal"><?php echo $_POST['uid']; ?></td>
<td id="amt">test1</td>
<td id="type">test2</td>
The first tabledata just appears blank.
Can anyone help me fix this?
Here is the entire code in the buy.php file: http://pastebin.com/ffWAP92C
(was having trouble posting it in here )
This is what the problem looks like:
Change this <td id="bal"> <?php echo "$_POST['uid'];" ?> </td> to <td id="bal"> <?php echo $_POST['uid']; ?> </td>
Try this one,
<html>
<head>
<title></title>
<style type="text/css">
</style>
</head>
<body>
<center><h1>Purchase Account ID</h1></center>
<table border="1" style="width:100%;">
<tr>
<td><b>Account ID</b></td>
<td><b>Account Type</b></td>
<td><b>Account Price</b></td>
</tr>
<tr>
<td id="bal">
<?php
if(isset($_POST['uid']))
echo $_POST['uid'];
else
echo "Nothing";
?>
</td>
<td id="amt">test1</td>
<td id="type">test2</td>
</tr>
</table>
</body>
</html>
Hope this works.
Remove " signs inside php tag. i.e. replace
<td id="bal"> <?php echo "$_POST['uid'];" ?> </td>
with
<td id="bal"> <?php echo $_POST['uid']; ?> </td>
OR insert semicolon after " sign. i.e.
<td id="bal"> <?php echo "$_POST['uid']"; ?> </td>
you can copy past below code in a test.php file. i have updated code. It is working in my localhost.
<html>
<head>
<title></title>
<style type="text/css">
</style>
</head>
<center><h1>Purchase Account ID</h1></center>
<body>
<table border="1" style="width:100%;">
<tr>
<td><b>Account ID</b></td>
<td><b>Account Type</b></td>
<td><b>Account Price</b></td>
</tr>
<tr>
<td id="bal"> <?php if(isset($_POST['uid'])) echo $_POST['uid']; ?> </td>
<td id="amt">test1</td>
<td id="type">test2</td>
</tr>
</table>
</body>
<form action="test.php" method="post">
<input type="text" name="uid" />
<input type="submit" value="Buy Now" />
</form>
</html>

Integrating PHP files into HTML files

I'm new in this language. I'm working on a website. I use HTML file like this:
<html lang="en">
...
<div class="" id="temperatura" name="temp">
<?php require 'php/staticsTemp.php'; ?>
<h3 class="centered">Temperature</h3>
<hr>
<br>
<table class="tg" border="5">
<tr>
<th class="tg-031e">Temperature ºC</th>
<th class="tg-031e">Date & Time</th>
</tr>
<tr>
<td class="tg-031e">33</td>
<td class="tg-031e">44</td>
</tr>
</table>
</div>
...
</html>
And I want to substitute the value 33 and 44 in the table to values that are inside the PHP file.
My PHP looks like this:
<?php
include("ligacaobd.php");
$sql="SELECT * FROM Valores ORDER BY Momento DESC LIMIT 20";
$result = mysql_query($sql, $ligacaobd) or die(mysql_error());
$rowValor = mysql_fetch_assoc($result);
do
{
$data[date('d/m/Y H:i:s', $rowValor['Momento'])]=$rowValor['Temperatura'];
}
while ($rowValor= mysql_fetch_assoc($result));
?>
Any thoughts? I tried with function POST, but in HTML doesn't work.
You must convert your html in .php as it is and include this php code, either directly in the page or via another php page.
You will then be able to manipulate your variables and do something such as:
<td class="tg-031e"><?php echo $myVariable1; ?></td>
<td class="tg-031e"><?php echo $myVariable2; ?></td>
I'm not quite certain what you mean, but I will try to help you.
First of all don't* use mysql functions anymore. These functions are no longer maintained. Use **mysqli or PDO instead.
If you want to show variables in a HTML document you can do the following thing.
<tr>
<td class="tg-031e"><?php echo $var1; ?></td>
<td class="tg-031e"><?php echo $var2; ?></td>
</tr>
Also I would recommend you separating your HTML files from the PHP or at least place your PHP code at the top of your document. For example:
<?php
$var1 = 'Example Variable';
?>
<html lang="en">
<head>
<title>Example</title>
</head>
<body>
<?php echo $var1; ?>
</body>
<html>
Yet the best practice is separating HTML from PHP.
Since you are new to the PHP language, I have some great tutorials for you.
Take a look at http://www.w3schools.com. They have some basic PHP tutorials for you to start with.
Good luck.
Don't use mysql_* functions anymore! They are deprecated in PHP 5.5 and should be removed in 5.6. Use PDO instead.
Not tested yet, but should work. You just need to change your <table /> code to this:
<table class="tg" border="5">
<tr>
<th class="tg-031e">Temperature ºC</th>
<th class="tg-031e">Date & Time</th>
</tr>
<?php foreach ($data as $datetime => $temperature): ?>
<tr>
<td class="tg-031e"><?php echo $temperature; ?></td>
<td class="tg-031e"><?php echo $datetime; ?></td>
</tr>
<?php endforeach; ?>
</table>
Hope it helps :)

Echo HTML with PHP in the middle

I am new to PHP and am running into a small problem with this code. I am trying to make a layout for a page that uses more PHP to fill in the blanks.
When I view the source <? include $navbar ?> is commented out but <?=$pagetitle?> works, why is that?
For reference:
$navbar = "navbar.php";
and navbar.php:
<?php echo "Select Car Change Profile"; ?>
Layout.php:
<?php
echo "
<html>
<head>
<title>Race Data. <?=$pagetitle?></title>
</head>
<body>
<div id='page'>
<table border='1'>
<tbody>
<tr>
<td colspan='3'>Banner goes here.<?=$pagetitle?></td>
</tr>
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><? include $navbar; ?></td>
</tr>
<tr>
<td>Content</td>
<td>Right menu</td>
</tr>
<tr>
<td colspan='3'>Footer</td>
</tr>
</tbody>
</table>
</div>
</html>
";
?>
I'm sure knowing this will help amny future problems I run into.
Also, what are the diferences in using <? ?> vs <?php ?>?
In php, you always need to use <?php /*code*/ ?>
The shorthand version to echo something is <?= /*string*/ ?>, but to run code, such as an include you would need to start with <?php. In your example, this would be:
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><?php include $navbar; ?></td>
</tr>
Correction
I only just noticed that you placed the PHP tags inside another set of PHP tags. You're kind of doing it the hard way. In a PHP file, anything is regarded as an echo, except for content inside <?php ?> tags. So this should work perfectly for you:
<html>
<head>
<title>Race Data. <?=$pagetitle?></title>
</head>
<body>
<div id='page'>
<table border='1'>
<tbody>
<tr>
<td colspan='3'>Banner goes here.<?=$pagetitle?></td>
</tr>
<tr>
<td rowspan='2'>Left menu</td>
<td colspan='2'><?php include $navbar; ?></td>
</tr>
<tr>
<td>Content</td>
<td>Right menu</td>
</tr>
<tr>
<td colspan='3'>Footer</td>
</tr>
</tbody>
</table>
</div>
</html>
The difference is that I didn't put <?php ?> tags around the whole thing.
Try
short_open_tag=On;
in php.ini
And restart your Apache server.

Categories