my php file doesnt output but instead shows source file - php

I am trying to add a php code in my html file, but whenever i do, my file instead of outputting my expected outcome, it instead presents the source code
again, i am creating a server for my project at school and i am a total noob in programming as i learn it all by myself without formal education about it. i tried to save the file as both .html and .php, although .html presents the output, but not the desired one. now i have two files below and since i am a noob in programming, my concept was to have users input the form in the .html file and output in on an identical one but in .php
this is the first file, saved in ABM11.html
<form action="AMB11.php" method="post"><table border="0">
<tr>
<td>Name</td>
<td align="center"><input type="text" name="StudentName" size="30"></td>
</tr>
<tr>
<td>Subject</td>
<td align="center"><input type="text" name="Subject" size="30"></td>
</tr>
<tr>
<td>Final Grade</td>
<td align="center"><input type="text" name="FinalGrade" size="30"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit"></td>
</tr>
</table>
</form>
this is the second file, saved as ABM11.php
<?php
$studentname = $_POST['StudentName'];
$subject = $_POST['Subject'];
$finalgrade = $_POST['FinalGrade'];
echo $studentname '<\br>';
echo $subject '<\br>';
echo $finalgrade '<\br>';
?>
basically i just want users to answer a form then have that data be posted on the same page perpetually, not have my noob source code presented

it seems , the problem with your web server , weather you are using apache or nginx . its not running properly or its not configured properly on your local machine so rather then executing php code its just showing that file on the browser .
You can debug like , create one simple php file just like echo "yourname "; and put it to your www directory and try to run it form browser if the server is proper configured then it must print and if its not prited then its mistake with your local server configuration .
and also on your source code there is one mistake of echo and .
Wrong : echo $finalgrade '<\br>';
True : echo $finalgrade .'<\br>';
You must concat br with .
that is mistake 110% but as you said in your question the code is showing on the page is not only because of this , its surely configuration problem
if . is the only mistake then it wont show you the source code on the page but you will get any php error if thats the only issue .so check your config well first.
Thanks
Akshay Champavat

If you combine variables and strings in an echo, you need to add dots to connect them. So instead of this
echo $studentname '<br>';
you need to write it as
echo $studentname.'<br>';
And similar in all other lines.
(and no backslashes, by the way!)
And also ("sourcecode displayed...") make sure to open/call the php file via a server (for example XAMPP, via localhost), not simply as a file on your computer.

Related

Implementing PHP Inside a HTML Table

Currently, I am building a table full of content. I can sucessfuly make one row that has one cell (the first cell) saying data, however when I attempt to use php to echo something, it does not show up. Why is this?
If you need my whole program I would be happy to include it, however to stay clean I am going to only include a section of the table.
Code:
<tr>
<td>data </td>
<td><?php echo "hi"; ?> </td>
<td> </td>
<td> </td>
</tr>
What you are doing is very right.
There are two reasons why that may not be working.
1- check if your filename ends with .php
and
2- check if you are running your code from a webserver that supports php.
To know if php is installed just try to right a simple php page iwth the following content.
if it renders it means you have php installed

Html - how to make the browsing button open in a desired path

I have this html code(+some backdoor php codes) to delete images from my website, but the problem is that I need that browsing dialoge to be opened in a specific path and be stuck on that path, I like I don't want the user to be able to navigate through the folders as he likes, is that possible to be done in any ways?
<table>
<tr>
<td>Select a project : </td>
<td><input type='file' name='userFile'><br></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="upload_btn" value="delete"></td>
</tr>
</table>
thanks
No, it isn't … and it shows the user's local file system rather then the servers anyway.
Write some server side code to generate a list of files that could be deleted and generate an HTML document listing them instead.

HTML form opens PHP script instead of running it

I am using WAMP with MySQL and PHP to run a local server. The WAMP server is online and I use a simple HTML script to run PHP file that connects to a database and inserts data in a table that I have created on WAMP server.
The problem is that instead of running (executing) my script, the browser opens it in text mode. As in I can see the script.
Now there have been similar questions on this forum and others and people have solved the problem. However what makes my problem unique is that I am able to run a test.php. I am able to display text with it as well as open info.php so PHP is running on my server, however when I use the HTML form it refuses to run and only opens the script.
I am using Chrome browser and I have also checked it in Firefox with the same result.
I have one HTML file that is a form linked to my PHP files which handles connection and insertion of values in the table on the WAMP server. The name of the database is test.
form.html
<form action= "create_product.php" method= "get">
<center>
<table>
<td><label><b><font size="5"> Name</label></td>
<td><font size="5"><input type="text" name="name" /></td>
</tr>
<tr>
<td><label><b><font size="5"> Description</label></td>
<td><font size="5"><input type="text" Description="description" /></td>
</tr>
<tr>
<center><td></td></center>
<center><td><input type="submit" value="Submit"/> <input type="reset" value="reset"/>
</tr>
</table>
</center>
insert_product.php
<?php
$con= mysql_connect("localhost", "admin","");
$db = mysql_select_db("test");
$name = $_GET["name"];
$description = $_GET["description"];
$query = "Insert into products('name','description')values('".$name."','".$description."')";
$result = mysql_query($query);
?>
I had the same problem and was surfing all known technical forums. I finally figured out what was the problem - nothing to do with PHP or Apache setup. Nothing wrong with the code (at least on mine). Here's what was NOT working:
I double-clicked to open the file from Finder (or explorer if you are a Windows user), and the URL in browser is
"/file/xxxxx/foo.html"
. This would open
/file/xxxx/bar.php"
, which doesn't work!
However, if I typed into the URL in browser
"localhost/~xxx/foo.html"
, it runs nicely when
"bar.php"
is called.
Perhaps this is what you can try.
"file:///XXXX/foo.php"
doesn't work, but
"localhost/XXX/foo.php"
does.
Make sure that you open it localhost/yourfile.php not like C:\wamp\yourfile.php
Start all services
or try xampp download
tried wamp and xampp , in my opinion xampp is better
This is edit- If u have tested that PHP is working on your local server,then the most probable cause for text display could be missing php end tag(?>)
Also please note that if u have saved your file as anything other than .php,it will not be parsed. Files containing PHP code but saved as HTML or doc file renders the php codes within it useless.
<<--EDIT ENDS-->>
u gave action= "create_product.php" in form although u specified the php file as insert_product.php
Also as a good practice,always provide database connection variable in mysql_select_db
use $db = mysql_select_db("test",$con);
instead of $db = mysql_select_db("test");

populate form fields with php

I have a form for users to fill out information and am wondering how to retain the previously typed information if the user redirects back to the page.
So, I have two files,
form.html
validate.php
Under form.html:
<form id="regForm" action="index.php?validate" method="post" onsubmit="return regValidation();" >
<tr>
<td width="150px" >First Name: <font color="red">*</font> </td>
<td><input type="text" name="firstNameField" id="firstNameField" value/ ></td>
</tr>
and under validate.php, I have stored the info with $_SESSION:
<?php
session_start();
$_SESSION['first'] = $_POST['firstNameField'];
?>
but when I tried to populate the firstNameField with $_SESSION['first'] as follow
<td><input type="text" name="firstNameField" id="firstNameField" value="<?php echo isset($_SESSION['first']) ? $_SESSION['first'] : NULL; ?>" /></td>
The field will literally be replaced with <?php echo isset($_SESSION['first']) ? $_SESSION['first'] : NULL; ?>
Can someone tell me why and how to properly fix it?
Thanks.
Change the file name to form.php instead of form.html so Apache knows to parse PHP inside that file.
Alternatively you can modify your .htaccess file and add a line like this:
AddType application/x-httpd-php .html
This tells Apache to to render all files that end in HTML using PHP.
Why make this complicated by having two files?
Just have the one.
The first time around nowt is filled in as the $_POST has nothing in it. Subsequent times that variable will have the appropriate value.
This will save you a lot of heartache in the future as you do not need to keep two files in step.
Also it removes the complexity of using sessions.
By default, any file with the file extension of .html will not be parsed, therefore exposing <?php $yourCode->exposed = true; ?>
If you rename the same file, changing its file extension from .html to .php, then
<?php echo 'HelloWorld'; ?> will be parsed, and the end result is HelloWorld
This default behavior can be overridden as described above by Tim
Change the file name to form.php instead of form.html as is the php tag
or add .html handler

IE is not loading page the page, does open file to clipboard

The task, a rather simple one. In one .HTML page is the HTML script whose task is to 'collect imput' and pass 'this data' (given to variable names) onto another .php page, then display the variables.
This simple .HTML page is -
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form action="processorder.php" method="post">
<table border="0">
<tr bcolor="#cccccc">
<td with="150">Item</td>
<td with="5">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td align="center"><input type="text" name="tireqty" size="3" maxlength="3" />
</td>
</tr>
<tr>
<td>Oil</td>
<td align="center"><input type="text" name="oilqty" size="3" maxlength="3" />
</tr>
<tr>
<td>Spark Plugs</td>
<td align="center"><input type="text" name="sparkqty" size="3" maxlength="3" />
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit Order" />
</td>
</tr>
</table>
</form>
</body>
The input is collected and the call
<form action="processorder.php" method="post">
fails to 'load the .php file into the browser but will OPEN this file onto the clipboard.
The simple processorder.php file is
<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at ";
echo date('H:i, jS F Y');
echo "</p>";
echo '<p>Your order is as follows:</p>';
echo $tireqty.'tires<br />';
echo $oilqty.';bottles of oil<br />';
echo $sparkqty.';spark plugs<br />';
?>
</body>
</html>
Nothing too complicated, just a simple exercise in passing variables around. So, the question remains as to why the call does not load the .php file but instead opens the .php file and the variables collected in one page are not passed onto another page.
Each page loaded onto the browser does load seperately but of course, the variables are not then collected in one page and passed onto the other page which is the principale objective.
If the line on the orderform.html page which is
<form action="processorder.php" method="post">
is then edited to be
<form action="processorder.html" method="post">
then the page is loaced onto the browser and not opened to the clipboard. The other change to this .html file is the tag
All this is happening inside the Rapid PHP 2007 editor.
PHP is 'present' in the files and configured with the Rapid 2007 editor.
One question is about the IE browser and how the browser detects/decides not to load the file, execute the PHP parts or what the problem is by not loading the file but opening the file to the clipboard.
Your PHP files are probably delivered as text/plain by your server (or you don't use any server, or your server doesn't support PHP)
It sounds like you haven't got PHP correctly installed. First create a simple php page with just:
<?php
phpinfo();
?>
If this doesn't does the same, and you just see the source code then you need to check your PHP stack setup.
A good place to get started is by downloading a complete stack such as EasyPHP:
http://www.easyphp.org/
Then following the install instructions.
http://www.easyphp.org/introduction.php
PHP has nothing to do with Internet Explorer and vice-versa, but considering that you've tagged your question with internet-explorer, I guess you're not having that issue with Firefox, Chrome, Opera, etc. ... and that would mean it's nothing related to PHP.
In that case - you actually have a whole bunch of problems:
Your HTML code is not valid according to W3C standards.
You have extra output before the actual HTML code starts.
IE has a long history of web standards incompatibility and on top of that, it's pretty bad at guessing content types too. So even if you were conforming to the standards - you could still have the same problem, although I doubt you're using IE 6, so that's probably not the case. But I guess it's best explained in here.
So ... what you could do to fix it:
Remove the white-spaces(new lines count too!) preceeding the <html> tag.
Send a content-type header with PHP(it's still not related to it - you'd have the same problem if you were using e.g. Perl or Ruby).
Removing the xmlns attribute could probably work ... but only in IE 6, and you'd still have to remove the white-spaces.
Include a DOCTYPE declaration - this would make sure that the browser knows what type of document it's trying to render, instead of leaving it to guess in "dumb mode".

Categories