Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
<form method="post">
Username: <input name="username" type="text" /><br />
Password: <input name="password" type="text" /><br />
Email: <input name="email" type="text" /><br />
Subject: <input name="subject" type="text" /><br />
<input type="submit" value="Submit" />
</form>
When someone fills out the form above, is it possible for the results to be uploaded to a text file, such as results.txt, rather than being emailed?
So for example, if the site link was hithere.com, the results would be uploaded to a password-protected text file at hithere.com/results.txt.
Thanks!
Here is a posible code to be executed from your .php form handler file
<?php
$myfile = fopen("result.txt", "a") or die("Unable to open file!");
$text = $_POST['username']."\n";
$text .= $_POST['password']."\n";
$text .= $_POST['email']."\n";
$text .= $_POST['subject']."\n\n";
fwrite($myfile, $text);
fclose($myfile);
?>
Also, you may need to add an action attribute to your form tag, whose value should be the same as the .php filename(or route if it is not in the same folder). For example if your php file is form_handler.php you should have <form method='post' action='form_handler.php'>
A little bit explaining on the php code above, you open the file with the second parameter "a" to tell the server you don't want to override the file's content but just add some content after the existing one(append). If you wanted to override the contetn that parameter should be "w" Then you set all the text in a variable, and write it in the file with fwrite(). After this you need to close the file with fclose()
Edit: In case you don't know, "\n" are linebreaks.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Developing a school website in WordPress and need to upload Progress Report of students in the server in pdf format. File name of Progress report will be the Admission number of the students (Eg. 123.pdf). When students enter their Admission number in the input box, they should get their progress report file downloaded. As of now using below form box to redirect to file url.
<script>
function process()
{
var url="http://demo.websign.in/" +
document.getElementById("url").value;
location.href=url;
return false;
}
</script>
<form onSubmit="return process();">
ENTER YOUR ADMISSION NUMBER: <input type="text" name="url" id="url">
<input type="submit" value="DOWNLOAD">
</form>
Any help as I am a theme developer and no much knowledge in coding?
You do not need form tag to download file. Also instead of type "submit" use "button". The js you need is something like this:
function myFunction()
{
var downloadUrl = document.getElementById("url").value;
document.getElementById("downlod").href = downloadUrl + '.pdf';
}
<div>
<label>ENTER YOUR ADMISSION NUMBER:</label> <input type="text" name="url" id="url" onkeypress="myFunction()">
<a href="foo" id="downlod" download><span>Download</span></a>
</div>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In the following code, I would like to get the input, create the page, then go to the redirected page.
But instead I am getting the input, then going to the redirected page (it's skipping creating the page).
How can I achieve the first one?
<?php
$newfile = $_GET['abc'];
$file = 'example.com';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
<form onsubmit="location.href='http://www.example.com/file-created;">
<form name="form" action="" method="get">
<input type="text" name="abc" placeholder="Type Here. . .">
<input type="submit" value="Go">
</form>
</form>
I tried using echo, but it was of no use (maybe I am making a mistake), and when I input something (without redirect), my url changes to example.com?abc=[input_here]. Why?
When you don't have the onsubmit, it is submitting the form to the server to the same page that is currently loaded because you don't have a value for the action="" set. So if all of that code was in index.php, the form will by default submit to index.php because you didn't tell it go to anywhere else.
Since you are using method="get", it is going to add all the form data as a query string to the action (which you didn't specify, so it adds it to the current url). So it appends abc=(whatever input) to your url and submits the form.
Now, when you have the onsubmit event in the url, instead of submitting the data to the server, it is just redirecting to that url. Nothing is going to happen with all of the form data that was entered. It is basically thrown away.
What you need to do is have your code submit to your server, do whatever processing you need to do, and then redirect to the url you want.
So something like:
<?php
// check if "abc=(whatever)" is in the url, and if so do the copy to, otherwise skip it
if(isset($_GET["abc"])) {
$newfile = $_GET['abc'];
$file = 'example.com';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
} else {
header("Location: http://yoursite.com/" . $newfile);
exit;
}
}
?>
<form name="form" action="" method="get">
<input type="text" name="abc" placeholder="Type Here. . .">
<input type="submit" value="Go">
</form>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working on a site to help learn some PHP MVC. At the moment the site carries out the CURD commands but I am looking to improve on them.
The items of the database are displayed in on column with two buttons, one to delete and one to update. When the user clicks the update button a form (update_item_form.php) is displayed that will allow the user enter the item information to be update. The form consists of three fields (title, price and description)
What I am trying to do: When the user clicks the update button the form will be pre populated with all the item information connected with that row.
How will can I send the row information to the form when the user clicks the button?
file_get_contents()
$rightBox = file_get_contents( "templates/update_item_form.php" [database value] );
Update_item_form.php
<h2>Update Item!</h2>
<h4>Fill in the form to update an entry.</h4>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='updateItem' />
<p>
<label for="fTitle">Title</label> <input type="text"
id="fTitle" name="fTitle" placeholder="title"
maxlength="25" required />
</p>
<p>
<label for="fPrice">Price</label> <input type="text"
id="fPrice" name="fPrice" placeholder="price"
maxlength="25" required />
</p>
<p>
<label for="fDescription">Description</label> <input type="text"
id="fDescription" name="fDescription" placeholder="description"
maxlength="500" required />
</p>
<p>
<div class="form-group">
<div class="controls">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</p>
</fieldset>
file_get_contents won't parse a PHP file. All you're doing is loading the code into $rightBox, not the output that I assume you are after.
For that, you can use output buffering.
ob_start();
include "templates/update_item_form.php";
$rightBox = ob_get_clean();
This will store output between ob_start() and the ob_get_clean() into $rightBox.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have written a HTML and PHP code which should show a survey form and invite you to enter your email address and have a submit button for you to click. However, when I "run" the code, I get some gibberish which I wrote in my code, which I did not expect to appear on my survey form. The code that I have written is as follows:
session_start();
if (!empty($_POST['posted']) && !empty($_POST['email'])){
$folder = "surveys/" . strtolower($_POST['email']);
// send path information to the session
$_SESSION['folder'] = $folder;
if(!file_exists($folder)) {
// make the directory and then add the empty files
mkdir($folder, 0777, true);
}
header("Location: 08_6.php");
}
else { ?>
<html>
<head>
<title>Files and folders - Online Survey</title>
</head>
<body bgcolor="white" text="black">
<h2>Survey Form</h2>
<p>Please enter your email address to start recording your comments</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="posted" value="1">
<p>Email address: <input type="text" name="email" size="45" /><br />
<input type="submit" name="submit" value="Submit"></p>
</form>
</body>
</html>
<?php }
What's happening is that I'm getting:
session_start(); if (!empty($_POST['posted']) && !empty($_POST['email'])){...
and whole bunch of other stuff appearing at the top of the webpage, which is certainly not what I want. Could one of you experts please tell me what I'm doing wrong?
You have not included an opening <?php tag in your page.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm having this problem where I can't manage to accomplish this simple script. Basically, I want to take a post from a form, assign it to a variable, and then append the variable to a preexisting example.txt file. I apologize for the lack of examples, I switched to my phone after I went out to dinner with my inlaws. Could anyone post a simple example andd allow me to build off of that foundation? thanks in advance
The following should give you a foundation to start.
<?php
// Check to see if the form was submitted
if(isset($_POST['action']) && $_POST['action'] == 'add'){
$file = 'example.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $_POST['test'];
// Write the contents back to the file
file_put_contents($file, $current);
}
?>
<form method="POST">
<!-- This becomes PHP variable $_POST['test'] -->
<input type="text" name="test" />
<input type="hidden" name="action" value="add" />
<input type="submit" value="Add"/>
</form>
Check out http://us3.php.net/file_put_contents for more information.
If your html form has a text field like this
<input type="text" name="firstName" value="Name">
you can access that value in your php script using
$_POST['firstName'];.
If you want to append text in php, you can simply do it using "."
Ex: $post_string = 'Ex:' . $post_string;