My HTML/php page is presented as clear text in browser - php

I have this problem that my .php file is presented as clear text in the browser. I.e., the page is not transcoded in any way.
This is my html-fil with a form
<html>
<head>
</head>
<body>
<form id="myForm" action="commentNoJQ.php" method="post">
Name: <input type="text" name="name" />
Comment: <textarea name="comment"></textarea>
<input type="submit" value="Submit Comment" />
</form>
<p>Click on the button to load result.html file:</p>
<div id="stage" style="background-color:blue;">
STAGE
</div>
<input type="button" id="driver" value="Load Data" />
</body>
</html>
This is my .php file that the form is posting to. This file is presented as it is in the browser:
<html>
<head>
</head>
<body>
<?php
$myFile = "/Library/WebServer/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $_POST["name"]);
fclose($fh);
?>
</body>
</html>
What is the problem?
It's php-related sine when I create a normal html-file to post to, the normal file is processed as it should be.
This is how it looks in the browser:
I checked the logs (access_log, error_log), no info at all. Are there any other logs I should check?
Using Apache 2.2 and Mac OSX 10.6.4
Thanks in advance!
/Niklas

PHP is not properly configured on Apache I think.
UPDATE: links http://php.net/manual/en/configuration.changes.php
http://www.php.net/manual/en/install.macosx.php

Related

Download user input in textarea using HTML and PHP?

I'm working on learning PHP and I'm trying to essentially have the user enter input into two different text areas, and having the content downloaded into a single .txt file once they click the button. I know my code is extremely rough, I haven't found any solutions online for my specific case-scenario, but here it is:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="./style.css" />
</head>
<div class="navbar">
Home
Resume
Projects
Contact
Social
Logout
</div>
<body>
<label for="textbox">Professional Summary</label>
<textarea id="textbox"></textarea>
<label for="bio">Brief Biography</label>
<textarea id="bio"></textarea>
<input type="button" id="homebt" value="Submit" />
</body>
</html>
<?php
if (isset($_POST['submit'])) {
$text = $_POST['bio'];
print ($text);
$filename = 'index.txt';
$string = $text;
$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);
header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');
die;
}
Currently nothing happens when the button is clicked. The entire code is in a file called adminIndex.php, and I have Apache Web Server running to open the file in my browser for testing purposes. If anyone could potentially help me out with what I'm doing wrong, I'd really appreciate it!
Edit (made the following changes to the end of the HTML code):
<form name='form' method='post'>
<label for="textbox">Professional Summary</label>
<textarea id="textbox"></textarea>
<label for="bio">Brief Biography</label>
<textarea id="bio"></textarea>
<input type="submit" id="homebt" value="Submit" name="submit" />
</form>
</body>

I cant upload files

I made a site, and I wand to upload files through a php form, but when I try to do so, I have the following error:405 Not Allowed
nginx. Can anyone help me?
I have checked my php.ini file, and file_uploads were on by default
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form method="post">
Enter Your Text Here:<br>
<input type="text" name="textdata"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];
$fp = fopen('data.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
?>

return file for download on click with PHP

I am trying to build a website people can fill/upload necessary information in a form. Based on these information, the server does some computation, generates an output file and return the file. Everything goes well except the last step of returning. Following is a simplified version of my site that you can test directly. In this example, it should return a file containing the file name uploaded by the user while stay on the same page. However, it actually returns the html code of the page and the file name. What should I do to get only the output file not the html code? Thanks a lot!
I tried with flask, and everything went well. Now, for some reason I would like to translate everything to php. I am really new to website building and lack a lot of background knowledge.
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" autocomplete="on"
enctype="multipart/form-data">
<div> Upload your file: <input type="file" name="file"/> </div>
<input style="margin-left: 0.5em;" type="submit" id="click"
value="Click Me!" name='submit_btn'/>
</form>
<?php
if(isset($_POST['submit_btn']))
{
$fp = fopen("./output.txt", "w");
fwrite($fp, $_FILES['file']['name']."\n");
fclose($fp);
header("Content-Type:text/plain");
header("Content-Type: application/download");
header('Content-Disposition: attachment;
filename="output.txt"');
readfile("output.txt");
}
?>
</html>
You're trying to return both HTML and a file. Each HTTP request can result in only one or the other. Separate your concerns. Start with a PHP resource which only returns the file you posted to it:
<?php
if(isset($_POST['submit_btn']))
{
$fp = fopen("./output.txt", "w");
fwrite($fp, $_FILES['file']['name']."\n");
fclose($fp);
header("Content-Type:text/plain");
header("Content-Type: application/download");
header('Content-Disposition: attachment;
filename="output.txt"');
readfile("output.txt");
}
?>
Then write an HTML page which posts to that PHP resource:
<!DOCTYPE html>
<html>
<body>
<form action="yourPHPFileAbove.php" method="POST" autocomplete="on"
enctype="multipart/form-data">
<div> Upload your file: <input type="file" name="file"/> </div>
<input style="margin-left: 0.5em;" type="submit" id="click"
value="Click Me!" name='submit_btn'/>
</form>
</body>
</html>
Note specifically how the action attribute in the <form> posts to a specific PHP resource, not just back to itself.

Unable to write to txt file in php

I'm trying to simply overwrite a txt file with a number entered in a text box in PHP. Both the PHP and text file are in the html directory of my apache2 server. Every time it executes it just displays the die() string. I also tried using fwrite() with the same results. Any help would be much appreciated.
<HTML>
<TITLE>Home Automation Interface</TITLE>
<BODY>
<H1>Air Conditioning Power(Relay 1)</H1>
<H2>Turn AC On/Off</H2>
<H3>
<p>
<form action="relayToggle.php" method="get">
<input type="submit" value="AC Power">
</form>
</p>
<form method= "post" action = "homeInter.php">
<p>
<label for="acTemp">AC temperature (F):</label><br/>
<input type="text" id="temp" name="temp">
</p>
<button type="submit" name="submit" value="send">Change Temperature</button>
</form>
<p><strong>
<?php
$temp= $_POST['temp'];
echo $temp;
file_put_contents("/var/www/html/temp.txt", (string)$temp) or die("Unable to open file!");
?>
}
}
?>
</strong>
</p>
</H3>
</BODY>
</HTML>
Wrap your PHP code Ina valid checker to see if form was submitted
Like this:
if ($_POST):
//All functions go here
endif;
If the file is in the same directory as your PHP file just change the directory to the file name.file Extension in your file_put_content()

PHP how to clear memory when refreshing

I am facing the following issue. I have a simple textarea where user will use to submit text which is subsequently written to a text file in the server. This is working.
But when I refresh the page it adds in the last added text into the text file again causing duplicate entries.
Any idea what I must do to prevent this? Below is the code I used for the textarea portion.
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['text_box'])) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
}
?>
Pages that are loaded via POST will cause the browser to ask the user to resubmit the information to view the page resulting in the actions performed by that page happening again. If the pages is requested via GET and has variables in the querystring the same thing happens but silently (without the user being prompted to d it again).
The best to work around this is to use the POST/REDIRECT/GET pattern. I used it in an example about processing payments that I wrote for Authorize.Net. Hopefully that points you in the right direction.
A simpler so
You can just store a simple hash on session and regenerate it every time.
When the user reloads the page the php wont be executed.
<?php
if(isset($_POST['text_box']) && $_SESSION['formFix'] == $_POST['fix']) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
}
?>
<html>
<body>
<form name="form" method="post">
<input type="text" name="text_box" size="50"/>
<?php
$value = md5(rand(1,999999));
$_SESSION['formFix'] = $value;
?>
<input type="hidden" name="fix" value="<?= $value; ?>" />
<input type="submit" id="search-submit" value="submit" />
</form>
</body>
</html>
ps: the order of the blocks will matter, so you need to invert em.
As John said, you need to redirect user after form submit.
fclose($fh);
// and
header("Location: success.php or where else");
exit;
Note: Your redirection won't work unless ob_start is not called before, cos your page contains html outputs.
// form.php
<?php ob_start(); ?>
<html>
<body>
<? if (isset($_GET['success'])): ?>
Submit OK! New submit
<? else: ?>
<form name="form" method="post" action="form.php">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit" />
</form>
<? endif; ?>
</body>
</html>
<?php
if(isset($_POST['text_box'])) {
$a = $_POST['text_box'];
$myFile = "textfile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $a."\r\n");
fclose($fh);
// send user
header("Location: form.php?success=1");
exit;
}
?>

Categories