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>
Related
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.
I'm trying to save cookie using img tag. I used img tag in thankyou.php and wrote cookie related code in pixel.php file.
These three files are in the same directory(Folder Name: landing).
Here is index.php file code.
<html>
<head>
<title></title>
</head>
<body>
<form action="thankyou.php" method="GET">
<input type="text" name="name" placeholder="Name" required />
<input type="email" name="email" placeholder="email" required />
<input type="submit" value="Subscribe" />
</form>
</body>
</html>
Here is thankyou.php file code.
<html>
<head>
<title></title>
</head>
<body>
<h3>You've successfully subscribed for this campaign.</h3>
<img alt="" width="1" height="1" style="display:none" src="pixel.php?e=<?= $_GET['email'] ?>&c=book_bonus_video_1" />
</body>
</html>
Here is pixel.php file code.
$sCampaign = # $_GET['c'];
$sCampaign = urldecode($sCampaign);
$sEmail = # $_GET['e'];
$sEmail = urldecode($sEmail);
setcookie('campaign_' . $sCampaign,strtolower($sEmail),time()+60*60*24*365,'/');
header('Content-Type: image/gif');
die(base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw=='));
When I submit form(index.php), thankyou.php page is opened but cookie is not created. But when i write URL ( http://www.example.com/landing/pixel.php?e=user#email.com&c=book_bonus_video_1 ) in browser it creates cookie.
I don't know where is issue in my code.
Thanks in advance for guiding me.
I am writing an intranet site in HTML, and what online users to be able to add in their own infomation (such as skype name) so that I dont have to go through and add it all myself for the 1000 people at the company, and also so that when new starters come in, they can add their own infomation themselves.
I currently have a 'Users' page with a link in it to another page where there is a form code in it looking like this:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
<?php
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = file_get_contents("users.html");
$posts = "$msg - $name\n" . $posts;
file_put_contents("users.html", $posts);
echo $posts;
?>
I want the infomation users put into this form to be automatically put onto a list on the 'users.html' page, visible to everyone. I had hoped the above code would achieve this, but I cant get it to work.
Also, how would I specify whereabouts in the 'users.html' page the infomation is put? I want it to be amongst the:
<div id="content">
<div class="content_item">
..coding, so that it loads into a list inside the webpage.
Many Thanks in advance :)
Don't try to edit users.html with each submission. That way lies madness.
Store the submitted data in a database. Use the PDO library to do this.
Have the page that displays that data pull it from the database on demand.
Although the use of database would be better and files tend to grow rather large with time, am answering your actual question as per your posted code.
To show the values that were written from a form (seperate from below), you could set up a page with this inside it:
<div id="content">
<?php echo file_get_contents("users.html"); ?>
<div class="content_item">
And the seperate page form would be like the following (tested)
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Title Here</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && empty($_POST['msg']) && empty($_POST['name']) ) {
die("All fields need to be filled, please try again.");
}
if(isset($_POST['submit'])) {
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = $msg . "-" . $name;
$file = "users.html";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $posts . "\n<br>") or die("Couldn't write values to file!");
fclose($fp);
}
echo $posts;
echo "<hr>Previous messages<hr>";
$file = "users.html";
if (file_exists($file)) {
$fp = file_get_contents($file, "r") or die("Couldn't open $file for writing!");
echo $fp;
exit;
}
else {
echo "The file is empty";
}
?>
I'm building my first PHP website (attempting to, anyway!), and I'm trying to create a contact form whose contents are submitted to me via email. I've got the email part down, but I'm having trouble getting the form ("contact.php") to accept data. It automatically refreshes as soon as I type a character in any field. Here's the relevant code:
<div class="contactform">
<form name="contactform" method="post" action="contact-receiver.php">
<fieldset><legend><strong>Required Information</strong></legend>
First Name: <input type="text" name="firstName" size="35" maxlength="30"/>
Last Name: <input type="text" name="lastName" size="35" maxlength="30"/>
Email: <input type="text" name="emailAddress" size="60" maxlength="55"/>
</fieldset>
<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</form>
</div>
This works fine when tested independent from the rest of the site. However, here's the context:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<?php include 'header.php'; ?>
<?php
$id = $_GET['id'];
switch($id)
{
case 'main':
include 'storeinfo.php';
break;
case 'shop':
include 'inventory.php';
break;
case 'cart':
include 'cart.php';
break;
case 'contact':
include 'contact.php';
break;
default:
include 'error.php';
}
?>
<?php include 'footer.php'; ?>
</body>
</html>
"contact.php" works fine when displayed as a separate page, but won't accept any input when accessed as an include file. If I try to enter data in any of the fields, the page immediately refreshes after I type the first character, and the data is lost.
If anyone could point me in the right direction, I would really appreciate it!
EDIT
Disabling Javascript didn't work. I cleared my cache and restarted my browser (Firefox) just to be sure. While I'm working on that voodoo priest, here's the page source for index.php?id=contact:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Main</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div>
<p><img src='headerimg.png' class='header'/></p><a href='index.php?id=main'>
<img src='mainbutton.png' class='nav1'</a><a href='index.php?id=shop'>
<img src='shopbutton.png' class='nav2'</a><a href='index.php?id=cart'>
<img src='cartbutton.png' class='nav2'</a><a href='index.php?id=contact'>
<img src='contactbutton.png' class='nav2'</a>
</div>
<div class="contactform">
<form name="contactform" method="post" action="contact-receiver.php">
<fieldset><legend><strong>Required Information</strong></legend>
First Name: <input type="text" name="firstName" size="35" maxlength="30"/>
Last Name: <input type="text" name="lastName" size="35" maxlength="30"/>
Email: <input type="text" name="emailAddress" size="60" maxlength="55"/>
</fieldset>
<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>
</form>
</div>
<div id = "footer">
<p>©2012</p>
</div>
</body>
</html>
It sounds like a Javascript-related problem.
Check and make sure you're not including any scripts which try to autocomplete, as being misconfigured might cause it to send a request upon key up which would cause the behavior you're mentioning.
An easy way to test this is to disable JavaScript in your browser and see if the issue continues. If it does, it means you have ghosts in your computer and should see a voodoo priest. If the issue doesn't persist, it means it's an issue with some JavaScript on your site.
Posting some contents of header.php will help, as well. OR, you could simply post the complete HTML page source once - that is, visit your index.php?id=contact page, hit view source, and show that here.
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