|
-
Feb 29th, 2004, 12:19 PM
#1
Thread Starter
Fanatic Member
PHP Nub
Yes, im a newb to php. What I need is pretty simple...i think.
i have this page here
It doesn't really do anything right now. What I was wondering is how can I have whatever they type in the box, be sent to a text file on my server when they click send.
-
Feb 29th, 2004, 12:37 PM
#2
you can do something like:
PHP Code:
<?
//check if the form has been submitted
if(!empty($_POST['textfield'])){
/************************
* this WILL replace anything in the file "file.txt", if you want
* it to just add on to the file, replace "w+" with just "w"
************************/
//open the file for writing purposes
$open = fopen("file.txt", "w+");
//write to the file
fwrite($open, $_POST['textfield']);
fclose($open);
echo "successfully written to!\n";
echo "<br><br>\n";
echo "<pre>\n$_POST[textfield]\n</pre>\n";
}else{
?>
<form method="post" action="fwrite.php">
<textarea name="textfield" cols="77" rows="16">insert duelie script here</textarea><br><br>
<input type="submit" name="Submit" value="send to muffinator">
</form>
<?
}
?>
I changed your form because it wouldn't work how you originally set it up.. your <textarea> wasn't in a form, and so it wasn't being properly submitted.
-
Mar 3rd, 2004, 05:12 PM
#3
Thread Starter
Fanatic Member
Hey that worked great thanks alot man. Is there a way I could have it sent to me in an email? Oh and one other thing. I tried changing the w+ to just w but it still erases the previous contents of the txt file instead of adding on to it.
-duc
-
Mar 3rd, 2004, 05:37 PM
#4
ooh, I mixed some of it up.. "w" is for writing only; "w+" is for reading and writing, so use "a" instead, which will append to the current file, and "a+" if you also want to read it. The script I gave you doesn't read anything, so you just need to use "a".
I'm not used to using fopen() anymore..
Also, for sending yourself an email, check out PHP's built-in mail() function.. I don't have any experience with it, so I can't really help you out there.. Link's here: http://ca2.php.net/manual/en/ref.mail.php
-
Mar 4th, 2004, 06:33 PM
#5
Thread Starter
Fanatic Member
Thanks that worked great. If I wanted to make a simple little news system on my website, would it be safe to use the PHP writing to a text file like I just did? I would just post what I want in the text file, and have the textfile displayed in the news section. I know its not top notch secure but do you think its safe enough as long as know one knows the link to the actual fwrite.php [which i will change]
-duc
-
Mar 5th, 2004, 01:55 AM
#6
It'd be better to work with a database for the news system, but yeah, you could work with text files.
-
Mar 5th, 2004, 04:33 PM
#7
Like mendhak said, yes you can. Even though databases are more efficient, text files work, and they are a simple way of working with news.
You can use the exact script you are using right now for it, or you can slightly modify it so that you are required to enter in a password, and even a username, if you want.
I took my original script and tweaked it a bit to get this effect for you..
PHP Code:
<?
$password = "password";
//check if the form has been submitted
if(!empty($_POST['textfield'], $_POST['pass']) && $_POST['pass'] == $password){
/************************
* this WILL replace anything in the file "file.txt", if you want
* it to just add on to the file, replace "w+" with just "w"
************************/
//open the file for writing purposes
$open = fopen("file.txt", "w+");
//write to the file
fwrite($open, $_POST['textfield']);
fclose($open);
echo "successfully written to!\n";
echo "<br><br>\n";
echo "<pre>\n$_POST[textfield]\n</pre>\n";
}else{
?>
<form method="post" action="fwrite.php">
password: <input type="text" name="pass" maxlength="15"><br><br>
<textarea name="textfield" cols="77" rows="16">insert duelie script here</textarea><br><br>
<input type="submit" name="Submit" value="send to muffinator">
</form>
<?
}
?>
That should work.. you just have to change the value of $password.
-
Mar 5th, 2004, 10:47 PM
#8
Thread Starter
Fanatic Member
thanks alot man. just what i wanted.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|