...

View Full Version : making a contact form write to a file...



cragllo
10-05-2004, 05:07 PM
I have a contact form on my site but want it to write to a file instead of sending it to my email, here is the code I am using:



<?
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "name@domain.com", "Feedback Form Site",
$message, "From: $email" );
header( "Location: http://www.domain.com/thankyou.php" );
?>


I would like for it to write to a .txt file...
But theres more!
I then want to be abloe to view the messages in a html document with a little delete button next to the message...
Could someone please do this for me?

But do it in such a way I can do the html document myself and add in the bits where i want them, so to display the name of the person i coul insert $name or something,

Thank you,
Craig.

sad69
10-05-2004, 10:04 PM
Have you tried the Work Offers forum on this site?

We're here to help, but not to do your work for you.. for free.

Good luck,
Sadiq.

cragllo
10-05-2004, 10:08 PM
Thats a good idea... Thanks..... sad69!

Celtboy
10-06-2004, 12:32 AM
I already have the "write to text file" bit done. Adding an embedded "delete" section would be simple enough. I'll do it as soon as I get a chance.

-Celt

cragllo
10-06-2004, 04:18 PM
thank you celtboy, thank you verry much!

Celtboy
10-10-2004, 07:20 AM
hmmm. Before I finish, do u want to store data in an xml file, or just use some custom separation tags?

cragllo
10-10-2004, 11:31 AM
I dont mind, some custom separation tags would be easier to understand I think...
Whatever you think would be best...

Celtboy
10-14-2004, 04:15 AM
no, i haven't forgotten...just baby taking up most of my time!

XML would be nice, but I haven't used PHP's parse functions before, so I'll have to read up on them first.

Do you want to allow html code in the user's input data?

-Celt

cragllo
10-14-2004, 04:03 PM
not realy...
but I dont mind...

Celtboy
11-02-2004, 12:41 AM
done. Need to use XML. Several bugs. Instructions in 'index.php' at top.

See attachment.

-Celt

ps. sorry took so long!

cragllo
11-02-2004, 04:07 PM
Thank You! Better late that never eh?

It keeps comimg up...

Parse error: parse error, unexpected ')' in /home/cragllo/public_html/radio/request/index.php on line 199

what wrong?

Celtboy
11-02-2004, 11:29 PM
delete one of the ")" beside the semicolon in index.php, line 199. Or use save the following, overwriting the original index.php:



<?php
/********************************************************************
Comments.php

Script Purpose:
Allow the submission of visitor comments. Comments should
be easily maintainable and viewable by the administrator.
All entries are stored in a flat-file database.

Script Setup:
- Set appropriate values in the 'config' section
- Make sure you have the appropriate files setup.
- Using the provided templates for the files, make sure
that you use the following values for substitutions in
your html, surrounded by '<!--{{' and '}}-->' :
NAME DATE EMAIL
SUBJECT MESSAGE DELETE(for admin page)

- The first time a comment is added, you will get a warning
regarding an undefined offset. This is because the function
'get_num_entries()' does not have any error checking.
It will not continue beyond the addition of the first
comment.

Script Use:
- Extract this file to the directory. The first time a
comment is added, it will create the comments file.
- Comments can be deleted by accessing the admin page.
To Access the admin page, append ?app_action=admin' to
the url. Example:

http://www.example.com/comments/index.php?app_action=admin

You can delete comments by simply clicking the button by
each corresponding comment.

Known Issues:
- No error checking
- Need function clean-up
- get_num_entries() bug on first comment addition
- Need to add UBB code.
********************************************************************/




//====================================
// Config Section
//====================================

/* The templates */
define("COMMENT_FILE","comment_file.txt");
define("COMMENT_FORM","comment_form.htm");
define("COMMENT_CONFIRMATION","comment_confirmation.htm");
define("COMMENT_ADMIN","comment_admin.htm");

/* Allow html in user comments? */
define("ALLOW_HTML",FALSE); // default is FALSE



//====================================
// Logic Section
//====================================

/* Decide what to do */
if (isset($_GET["app_action"])) {
if ($_GET["app_action"] == "admin") {
display_admin_form();
exit;
}
}

if (isset($_POST["app_action"])) {

switch ($_POST["app_action"]) {
case "submit_comment" :
perform_submit();
display_confirmation();
break;
case "delete_comment" :
perform_delete();
display_admin_form();
break;
default :
display_submit_form();
exit();
}

} else {
display_submit_form();
}


//====================================
// Functions Section
//====================================


function display_submit_form() {
$template = file_get_contents(COMMENT_FORM);
print $template;
}

function display_confirmation() {
$template = file_get_contents(COMMENT_CONFIRMATION);
print $template;
}


function perform_submit() {
$name = urlencode($_POST["name"]);
$email = urlencode($_POST["email"]);
$subject = urlencode($_POST["subject"]);
$message = urlencode($_POST["message"]);
$comment = array("name" => $name, "email" => $email, "subject" => $subject, "message" => $message);

$num_entries = (int)get_num_entries();
set_num_entries($num_entries+1);

$file_handle = fopen(COMMENT_FILE,"a+");

fwrite($file_handle,"<!-- COMMENT:BEGIN -->\r\n");
foreach ($_POST as $key => $value) {
if ($key == "app_action") { continue; }
fwrite($file_handle,strtoupper($key) . "=\"" . urlencode($value) . "\"\r\n");
}
fwrite($file_handle,"DATE=\"" . date("F j, Y, g:i a") . "\"\r\n");
fwrite($file_handle,"<!-- COMMENT:END -->\r\n\r\n");
fclose($file_handle);
}

function get_num_entries() {
$file = file(COMMENT_FILE);
return $file[0];
}

function set_num_entries($num_entries) {
$file = file(COMMENT_FILE);
array_shift($file);
$file_handle = fopen(COMMENT_FILE,"w+");
fwrite($file_handle,$num_entries . "\r\n");
foreach ($file as $line) {
fwrite($file_handle,$line);
}
fclose($file_handle);
}



function display_admin_form() {
/* the html for the delete button that we sub into the admin form */
$delete_button = "<input type=\"submit\" name=\"comment_<!--COMMENT_NUM-->\" value=\"Delete\" />";

/* We store the entire comments file (minus the first line) in this string */
$comments = "";

$output = "";

/* get the admin template */
$file = file_get_contents(COMMENT_ADMIN);

/* get an array breaking the form into an array, including the reusable block fore each entry */
$parsed_array = get_parse_block($file);

/* the start of the output */
$output = $parsed_array[0];

/* From the comments file, remove the first line */
$file = file(COMMENT_FILE);
array_shift($file);
foreach ($file as $line) { $comments .= $line; }

/* Parse out each comment, and store it in an array */
$finds = "/<!-- COMMENT:BEGIN -->(.)*?<!-- COMMENT:END -->/si";
$comments_block = preg_match_all($finds,$comments,$matches);


/* parse each line and split into name/value pairs */
for ($x = 0; $x < sizeof($matches[0]);$x++) {

$lines = explode("\r\n",$matches[0][$x]);

foreach ($lines as $value) {
if ($value[0] == "<") {
continue;
} else {
parse_str($value);
}
}

/* store the name/value pairs in an array */
$values_array = array("name"=>$NAME,"email"=>$EMAIL,"subject"=>$SUBJECT,"message"=>$MESSAGE,"date"=>$DATE);
foreach ($values_array as $key=>$value) {

if (ALLOW_HTML == FALSE) {
$values_array[$key] = htmlentities(trim($value,"\""));
} else {
$values_array[$key] = trim($value,"\"");
}
}

/* replace sections of the template with the name/value pairs */
$toutput = eregi_replace("<!--{{NAME}}-->",trim($values_array["name"],"\""),$parsed_array[1]);
$toutput = eregi_replace("<!--{{EMAIL}}-->",trim($values_array["email"],"\""),$toutput);
$toutput = eregi_replace("<!--{{SUBJECT}}-->",trim($values_array["subject"],"\""),$toutput);
$toutput = eregi_replace("<!--{{MESSAGE}}-->",trim($values_array["message"],"\""),$toutput);
$toutput = eregi_replace("<!--{{DATE}}-->",trim($values_array["date"],"\""),$toutput);

$del_button = eregi_replace("<!--COMMENT_NUM-->",(STRING)$x,$delete_button);
$toutput = eregi_replace("<!--{{DELETE_BUTTON}}-->",$del_button,$toutput);

/* add this new block to the final output */
$output .= $toutput;
}


print $output . $parsed_array[2];

}


function perform_delete() {
$comments = "";

/* From the comments file, remove the first line */
$file = file(COMMENT_FILE);
$num_comments = array_shift($file);
foreach ($file as $line) { $comments .= $line; }

/* Parse out each comment, and store it in an array */
$finds = "/<!-- COMMENT:BEGIN -->(.)*?<!-- COMMENT:END -->/si";
$comments_block = preg_match_all($finds,$comments,$matches);

/* figure out which comment to remove */
$the_keys = array_keys($_POST);
foreach($the_keys as $key) {
if (substr($key,0,8) == "comment_") {

$comment_to_del = explode("_",$key);
$comment_to_del = (Int)$comment_to_del[1];
}
}

/* remove that comment from the comments array */
$matches[0][$comment_to_del] = "";

/* write everything back to the comments file */
$file = fopen(COMMENT_FILE,"w+");

/* let's not forget to decrement the comment count! */
fwrite($file, $num_comments-1);

/* write the comments back */
for ($x = 0; $x < sizeof($matches[0]);$x++) {
fwrite($file,$matches[0][$x]);
fwrite($file,"\r\n");
}
fclose($file);

}


function get_parse_block($content) {
$finds = "/<!-- BLOCK:COMMENTS:START -->(.)*?<!-- BLOCK:COMMENTS:STOP -->/si";
$parse_block = preg_match($finds,$content,$matches);
$content = preg_split($finds,$content);

$return_array = array($content[0],$matches[0],$content[1]);

return $return_array;

}
?>


Sorry!

cragllo
11-03-2004, 04:23 PM
I dont know how to thank you! everything has just fallen into place!

one other thing though when you view them its shows \" and \ at the ends of the inputs, is there any way to get rid of these?

thank you,
craig.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum