Click to See Complete Forum and Search --> : User Message!


Jick
08-01-2003, 08:34 PM
I wanted to know if there was a script out there or a way that I could make it so users can go to a form on my page and type a message to me and then when they submit it is saved to a file. Then on the main page of my site there would be a link to another file where I could login with an admin pass and it will display all the entries in that file so I could see who has sent me a message. Also if possible have it so next to the link on the main page it would display "New Messages" if there was a message I have not seen yet, kinda how this forum changes the images on the main page if there are new posts. If someone could help me out with this I would greatly appreciate it. :D

pyro
08-01-2003, 11:19 PM
I'd be willing to help you with specific questions, but not write it for you. (Unless there is some $$'s involved... :D) So, if you have something you were specifically questioning, let me know...

Jick
08-01-2003, 11:27 PM
Ok, let me start with a simple question first! My first question is how can I get my form to write to a file? Probably TXT if you think thats a good idea. I do not currently have MySQL access so I cannot write it to a database other wise I would probably do that! :)

pyro
08-01-2003, 11:32 PM
Look into fopen() (http://us4.php.net/manual/en/function.fopen.php) to open the file, fread() (http://us4.php.net/manual/en/function.fread.php) to read the file, fwrite() (http://us4.php.net/manual/en/function.fwrite.php) to write to the file, and then fclose() (http://us4.php.net/manual/en/function.fclose.php) to close the file.

Jick
08-01-2003, 11:38 PM
Ok thanks, I found this in the fwrite() function link you gave me:
<?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')) {
print "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent)) {
print "Cannot write to file ($filename)";
exit;
}

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

fclose($handle);

} else {
print "The file $filename is not writable";
}
?>

But from what I can see it writes this: (Add this to the file\n) to the file. I need to write the fields of a form to the file! How would I change this script to do that?

pyro
08-01-2003, 11:48 PM
You get the values that users submitted into form fields using either _POST (http://us4.php.net/manual/en/reserved.variables.php#reserved.variables.post) or _GET (http://us4.php.net/manual/en/reserved.variables.php#reserved.variables.get). This might also help: http://www.webdevfaqs.com/php.php#globalvariables

Jick
08-02-2003, 12:04 AM
My gosh! I have to be the worst person when it comes to php on this forum. I didn't understand the stuff in the link you gave me? Could you possibly spear any of your genius for me? :p

Da Warriah
08-02-2003, 08:05 AM
alright, you have your fields named, like so:

<form method="post" action="page.php">
<input type="text" name="blah" />
</form>

and make sure each has a unique name...now, using the global $_POST variables (in this case, in page.php), you access all the values of the textboxes etc using their name:

$_POST["blah"];

you can then use that to piece together the message whatever way you want...

Jick
08-02-2003, 09:21 AM
So from what I understand I do it like this:
<?php
$filename = 'test.txt';
$somecontent = "$_POST["the form input field names here"];";

// 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')) {
print "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent)) {
print "Cannot write to file ($filename)";
exit;
}

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

fclose($handle);

} else {
print "The file $filename is not writable";
}
?>
E'm I wrong? I'm probably wrong!

pyro
08-02-2003, 09:25 AM
This
$somecontent = "$_POST["the form input field names here"];"; needs to be:
$somecontent = $_POST["the form input field names here"];

Jick
08-02-2003, 09:36 AM
Ok thanks and do I seperate the form fields with commas like this:
$somecontent = $_POST["field1,field2,field3"];
Just thought I better ask before I screw it up! :p

pyro
08-02-2003, 11:01 AM
No, you will have to do one of these things:

$somecontent = $_POST["field1"].$_POST["field2"].$_POST["field3"];
OR
$somecontent = $_POST["field1"];
$somecontent .= $_POST["field2"];
$somecontent .= $_POST["field3"];
OR
$somecontent = $_POST["field1"];
$somecontent1 = $_POST["field2"];
$somecontent2 = $_POST["field3"];

Jick
08-02-2003, 11:22 AM
This is the format I want it to look like in the TXT file:

Name: their name here
E-mail: their email here
Subject: their subject here
Message: their message here
----------------------------------
Name: their name here
E-mail: their email here
Subject: their subject here
Message: their message here
----------------------------------
Name: their name here
E-mail: their email here
Subject: their subject here
Message: their message here
----------------------------------
Etc...

How could I set it up to look like that?

Bootsman123
08-02-2003, 12:05 PM
$contents = "Name: " . $name . "\nE-mail: " . $email . "\nSubject: " . $subject . "\nMessage: " . $message . "\n-----------------------\n";

Jick
08-02-2003, 12:17 PM
Thanks, and where would I put that peice of code?

Jick
08-02-2003, 02:02 PM
Thanks anyway
Never mind... I sovled it with the help of Jona over Yahoo! Messenger. Thanks for your help anyway.

Ok next problem:
I need it so it will have a link on my main page to a file that is password protected with the info they submitted in it.

Bootsman123
08-02-2003, 04:11 PM
To protect a file or directory use htaccess.

Jick
08-03-2003, 10:15 AM
Ok here is my current code:
<?php
$filename = 'data.txt';
$date = date("g:ia, F j, Y");
$somecontent = "-----------------------------------------\nName: ".$_POST["name"]."\nEmail: ".$_POST["email"]."\nSubject: ".$_POST["subject"]."\nMessage: ".$_POST["message"]."\nDate: ".$_POST["$date"]."\n-----------------------------------------\n\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')) {
print "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent)) {
print "Cannot write to file ($filename)";
exit;
}

print "<font face='Verdana' color='black' size='2px'><b>The following info has been submitted:<br>----------------------------------------</b><br><b>Full Name:</b><font color='blue'> $name</font><br><b>E-mail Address:</b><font color='blue'> $email</font><br><b>Subject:</b><font color='blue'> $subject</font><br><b>Message:</b><font color='blue'> $message</font><br><b>----------------------------------------<br>We will get back to you asap. Thanks.<br><br><a title='Go Back' href='javascript:history.back(1)'>Go Back</a></b></font>";

fclose($handle);

} else {
print "The file $filename is not writable";
}
?>
Can some body look at it and tell me why it's not posting the date along with the other info in my data.txt file? Everything works fine right now and I'm not getting any errors. All it does is post (date: ) in the data.txt file when I want it to put this (date: 8:11am, August 03, 2003). How do I fix what I have to do that? Thanks. :)

pyro
08-03-2003, 12:46 PM
Because $date is not a _POST variable. Try this:

"...\nDate: $date \n..."

Da Warriah
08-03-2003, 12:49 PM
well your not sending the $date variable through a form, so you dont need the $_POST variable...

"\nDate: ".$date."\n

Jick
08-03-2003, 01:12 PM
Cool... Thanks it works now. I had one more question though. Right now it's putting the time they sent the message according to the server time. Is there a way so it will put the time they sent the message in pacific standard time since that is the time zone I'm in? I was told that it was possible to have a script that took the server time and then added or subtracted however many hours you needed to. Is this possible and if so how? Thanks. ;)

pyro
08-03-2003, 01:55 PM
Try something like this:

<?PHP
$offset = -1; //numbers of hours offset
$time = time()+$offset*3600;
$date = date("g:ia, F j, Y",$time);
echo $date;
?>

Jick
08-03-2003, 02:14 PM
Thanks for your answer Pyro but I was wundering how I could merge my code and that code you just gave me? Also is there any thing I would have to change about "\nDate: ".$date."\n for it to work like I want? Thanks. :D

pyro
08-03-2003, 02:27 PM
All you should have to do is replace this:

$date = date("g:ia, F j, Y");

with this:

$offset = -1; //numbers of hours offset
$time = time()+$offset*3600;
$date = date("g:ia, F j, Y",$time);
echo $date;