Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

HTML Forum


You are currently viewing our HTML Forum as a guest. Please register to participate.
Login



Post a Project »

Find a Professional HTML Freelancer!

Find a Freelancer to help you with your HTML projects

FREE Outsourcing eBook!

Reply
Trying to create a form and store input as .txt
Old 10-30-2010, 01:45 AM Trying to create a form and store input as .txt
Novice Talker

Posts: 9
Name: Kyle Newton
Trades: 0
I've coded c++, asm, and linux for near 3 years now, but recently I've created a website and have been pretty much teaching myself html, css, and php. I've been using templates, and finding snippets of code and modifying them to work with everything i need. What I'd like is a template to do the following

Create a small drop box, I'll add in my options to it
Create one "short form" and throw on a submit button. I can kind of figure this out, but what I really want help with is when someone hits the submit button, it redirects to my site's home page, and saves their input to a file that will be on my ftp server. Could anyone help me piece together how to save this to a file?
ZeldaKrazy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-30-2010, 02:04 AM Re: Trying to create a form and store input as .txt
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
you have to use fwrite
www.php.net/fwrite
Example from php.net

PHP Code:
<?php
$filename 
'test.txt';
$somecontent "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    
// In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    
if (!$handle fopen($filename'a')) {
         echo 
"Cannot open file ($filename)";
         exit;
    }

    
// Write $somecontent to our opened file.
    
if (fwrite($handle$somecontent) === FALSE) {
        echo 
"Cannot write to file ($filename)";
        exit;
    }

    echo 
"Success, wrote ($somecontent) to file ($filename)";

    
fclose($handle);

} else {
    echo 
"The file $filename is not writable";
}
?>
edit // see fopen for opening mode. Here it's for adding. You can create one file for each answer, or better, create a file per day - like if(!is_file('my_'.date('Y-M-d').'.txt')) [MAKE NEW FILE] else [JUST ADD TO THIS]

OR Better... use a database
__________________
you can
Please login or register to view this content. Registration is FREE

Last edited by vectorialpx; 10-30-2010 at 02:07 AM..
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 10-30-2010, 02:14 AM Re: Trying to create a form and store input as .txt
Novice Talker

Posts: 9
Name: Kyle Newton
Trades: 0
So on this template I found, and I have a similar form already on my website that redirects to e-mail, again I piece together templates, but I'm having trouble piecing this one. How can I edit this to record just the drop box option and whatever they typed in the little box? I would like to just append to a .txt file.

HTML Code:
<!--Start Where...? Form -->

	<FORM METHOD=POST ACTION="http://www.threadsoftime.com/cgi-bin/formmail.cgi" name"where">

		<input type=hidden name="recipient" value="info@threadsoftime.com">

		<input type=hidden name="redirect" value="http://www.threadsoftime.com/Merchant2/info/where_popup.shtml?success">

		<input type=hidden name="subject" value="How did you hear about us?">

		<input type=hidden name="email" value="info@threadsoftime.com">

		<input type=hidden name="realname" value="User at:Threads of Time">

	<table>

		<tr valign="top">

			<td colspan="2">

			<select name="How did you hear about us?"> 

				<option selected>Choose One...</option>

				<option value="friend">Friend</option>

				<option value="link">Link</option>

				<option value="web">Web Search</option>

				<option value="ad">Magazine Ad</option>

				<option value="show">Show</option>

				<option value="shop">Shop</option>

				<option value="webring">Webring</option>

				<option value="other">Other</option>

			</select>

			</selected>

		</tr>

		<tr>

			<td><font face="Arial,Helvetica" size="-1">Please Specify:</font></td>

		</tr>

		<tr>

			<td><input type=text name="Please Specify" size="22"></td>

		</tr>

		<tr>

			<td><font face="Arial,Helvetica" size="-2">(Such as Google, Renaissance Magazine, Web Link Site Name, etc...)</td>

		</tr>

	</table>

	<!--End Email Link copy & Contact Form -->



<!-- End Where...? Form -->

									</td>

								</tr>

							</table>

						</td></tr>

					</table>

			<!-- End Table Box -->

			<font size="-2"><br></font>

<div align="center">



<input type="Image" name="Submit" src="http://www.threadsoftime.com/Merchant2/graphics/00000001/Button-Submit.gif" alt="Submit">

</form>

Last edited by ZeldaKrazy; 10-30-2010 at 02:16 AM..
ZeldaKrazy is offline
Reply With Quote
View Public Profile
 
Old 10-30-2010, 02:37 AM Re: Trying to create a form and store input as .txt
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
man, don't EDIT this, just make your own...
This form is not valid and it's not what you need

Code:
<form id="form1" name="form1" method="post" action="somewritescript.php">
	<p><textarea cols="70" rows="10" name="myfield" id="myfield"></textarea></p>
	<p><input type="submit" name="go4it" id="go4it" value="make submit" /></p>
</form>
and in your somewritescript.php file, make the thing
PHP Code:
<?php
if(!isset($_POST['myfield'])) exit;
$filename 'test.txt';
$somecontent $_POST['myfield'];

    if (!
$handle fopen($filename'a')) {
         echo 
"Cannot open file ($filename)";
         exit;
    }

    if (
fwrite($handle$somecontent) === FALSE) {
        echo 
"Cannot write to file ($filename)";
        exit;
    }

    @
fclose($handle);
    echo 
"Success, wrote";
    
    
/*
    // OR make a redirect back
    header('Location: your-redirect-link.php');
    */
} else {
    echo 
"The file $filename is not writable";
}
?>
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 10-30-2010, 02:49 AM Re: Trying to create a form and store input as .txt
Novice Talker

Posts: 9
Name: Kyle Newton
Trades: 0
Ok I think I'm getting it. Is there a way to store two entries? One from a drop down and one from the short form? So how would i take in two arguments to write?


Also, there's a syntax error on line 23 of your .php, I'm not sure what it is?
ZeldaKrazy is offline
Reply With Quote
View Public Profile
 
Old 10-30-2010, 02:54 AM Re: Trying to create a form and store input as .txt
Novice Talker

Posts: 9
Name: Kyle Newton
Trades: 0
Oh, sorry my mind was slipping for a minute, I could just do

PHP Code:
    if (fwrite($handle$somecontent) === FALSE) {
        echo 
"Cannot write to file ($filename)";
        exit;
}
    if (
fwrite($handle$someothercontent) === FALSE) {
        echo 
"Cannot write to file ($filename)";
        exit;

couldn't i?
ZeldaKrazy is offline
Reply With Quote
View Public Profile
 
Old 10-30-2010, 03:04 AM Re: Trying to create a form and store input as .txt
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
yep
or, you can also do
PHP Code:
 if (fwrite($handle$somecontent."\n\n".$someothercontent) === FALSE) {
        echo 
"Cannot write to file ($filename)";
        exit;

__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 10-30-2010, 03:09 AM Re: Trying to create a form and store input as .txt
Novice Talker

Posts: 9
Name: Kyle Newton
Trades: 0
Oh sweet, ok, that's cool.

Can you tell me whats wrong with your line 23? Still complaining about your syntax and I can't debug php for crap xD
ZeldaKrazy is offline
Reply With Quote
View Public Profile
 
Old 10-30-2010, 03:25 AM Re: Trying to create a form and store input as .txt
Novice Talker

Posts: 9
Name: Kyle Newton
Trades: 0
Actually, I got the short menu working, the only thing I need now is what variable do i set someothercontent to? I'm using this,

HTML Code:
<select name="mydropdown">
<option>Choose One...</option>
<option>Friend</option>
<option>Business Card</option>
<option>Google Search</option>
<option>Google Ads</option>
<option>Yelp</option>
<option>Yahoo! Business</option>
<option>Other (Please Specify)</option>
</select>
and want to be able to store their selection as well.
ZeldaKrazy is offline
Reply With Quote
View Public Profile
 
Old 10-30-2010, 03:46 AM Re: Trying to create a form and store input as .txt
Novice Talker

Posts: 9
Name: Kyle Newton
Trades: 0
I figured everything out, working perfectly for me! Also, apparently to add a newline in a .txt you need "\r\n" I Appreciate all your help and left you some Talkupation. Lemme know if you have a tip jar :P If you wanna see it in action, my website is www.sqntech.com

Thanks again!
~Kyle
ZeldaKrazy is offline
Reply With Quote
View Public Profile
 
Old 10-30-2010, 03:52 AM Re: Trying to create a form and store input as .txt
vectorialpx's Avatar
Extreme Talker

Posts: 249
Name: octavian
Location: Bucharest
Trades: 0
\n it's OK for a text file
http://en.wikipedia.org/wiki/Newline

\r = CR
and, yes... it's more ok to have both
__________________
you can
Please login or register to view this content. Registration is FREE
vectorialpx is offline
Reply With Quote
View Public Profile Visit vectorialpx's homepage!
 
Old 10-30-2010, 04:19 AM Re: Trying to create a form and store input as .txt
Novice Talker

Posts: 9
Name: Kyle Newton
Trades: 0
Yes, I know what \n is lol, again, I've been coding c++ for near 3 years, just websites are totally new to me. I tried it multiple times with \n and it would just butt the words up next to each other, I looked it up and remembered having to use \r\n in certain instances :P But I'm happy that everything works, so thank you for the help!
ZeldaKrazy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Trying to create a form and store input as .txt
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.79322 seconds with 12 queries