PHP contact form. Merge data from contact form into another text file

I need help in coding a PHP contact form. My goal is to take the data from the Contact form (Name, Summary, Details, Due Date, Check boxes, etc) and merge this data into an existing text file. This text file will have existing text that cannot be overwritten. Hopefully I can just take the variables from the contact form and replace them into the existing text file.

Existing PHP Contact Form data-
[php]<?php
$action = $_GET[“action”];
$myText = $_POST[“loginid”].$_POST[“summary”].$_POST[“details”].$_POST[“ddate”].$_POST[“application”];

if($action = “save”) {
$targetFolder = “C:\Apach2\htdocs\wp1a”;
file_put_contents($targetFolder.“mytext.txt”, $myText);
}
?>

New Hire Template Login ID:
Summary:
Details:
Due Date:
MS Word MS Excel MS PowerPoint

[/php] ===============================================

Here is the existing mytext.txt with Variables. These variables will be merged from the Contact form-

===============================================

[php]{
“fields”: {
“project”:
{
“id”: “11900”
},
“summary”: $_POST[“summary”],
“duedate”: $_POST[“ddate”],
“issuetype”: {
“id”: “20”
},
“customfield_12900”:“11182”,
“customfield_11000” : {“name”:“HelpDesk”},
“customfield_11600”: {“value”:“System” },
“customfield_11801”: {“value”:“Request” },
“customfield_11100”: {“value”:“AD/LDAP” },
“customfield_11102”: {“name”:$_POST[“loginid”] },
“customfield_11100”: {“value”:$_POST[“application”] }
}
}[/php]

So the Variables ($_POST[“loginid”].$_POST[“summary”].$_POST[“details”].$_POST[“ddate”].$_POST[“application”]) will need to be merged into the existing file mytext.txt.

Thanks in advance,

I noticed this one has not been answered. Did you get a solution for it as yet? If not, here is one way to do it…

What you basically want is a “template” system. I do this all the time in HTML emails. Basically, you create the base form
which is in your case a text file. (HTML email pages are basically just a webpage and therefore are just text.)

Then, you place the info you want inserted into it using simple string replace functions. Works like a charge. Here is how
I do this. Create a file with place-markers in it for the items you want to insert. I use characters that would never be used
in the text data. Some programmers use brackets or braces. I use FIRST-NAME which will never come up in normal
text. Two underscores, caps, dashes instead of spaces and two ending underscores. You can not miss that in a form or
HTML page. Next, I acquire my form data, validate it and merge it into the file. Since this is done in string format, you can
write it back out as needed to a new file or whatever. Here is a sample of code to do that…
[php]
// Read the existing file (In your case a text file, in my case a webpage to send as an email…)
$message = file_get_contents(“templates/EmailTemplate.html”); // Note folder and filename
$message = str_replace(“NAME”, $name, $message); // Replace name from form
$message = str_replace(“SUMMARY”, $summary, $message); // Replace summary where it belongs
[/php]
Repeat this for each of the fields you need in the text file. Since you want to keep the template the same for next time, just
make sure not to save the output to the same filename if you need to save it. If you are using it for displays or emails, no
need to save it back.

Easy! Hope that helps you…

Sponsor our Newsletter | Privacy Policy | Terms of Service