Allow sender to specify subject line
I recently had the following question from a FCF user:
QUESTION
Hello, I am using your contact form script and I’d like to know if it’s possible for the sender to add his own subject to the e-mail. If so, how would I go about adding that feature?
ANSWER
This could be achieved several ways, I’ll explain the simplest method here for clarity.
Step One:
Open form.php in your favorite text/script editor.
Step Two:
Go-to line number 36 or there abouts (with the line: required.add('answer_out', 'NUMERIC');)
Step Three:
Create 1 new lines above line number 36, and enter the following :
required.add('subject_line', 'NOT_EMPTY');
We are including validation for our new subject line field - this will prompt the user to include a subject line if they try to submit the form without it. At this stage we have only included JavaScript validation - later we will also include PHP validation.
Step Four:
Goto line 46 or there abouts (with the line <div class="r">)
Insert the following code ABOVE line 46:
<div class="r">
<label for="subject_line" class="req">Subject: <em>*</em></label>
<span class="f">
<input type="text" name="subject_line" size="40" id="subject_line" onBlur="trim('subject_line')">
</span>
</div>
Step Five:
Save form.php, we are done with that file now.
Step Six:
Open process_form.php in your favorite text/script editor.
Step Seven:
Go to line 89 or there abouts (with the line $reqobj->add("answer_out","NUMERIC");)
Insert the following code just below line 89:
$reqobj->add("subject_line","NOT_EMPTY");
Here we have included the PHP validation which makes sure the subject line exists.
Step Eight:
Go to line 159 or there abouts (with the line // create email headers )
Insert the following code just above line 159:
$ns = new clean;
$ns->comments($subject_line);
$email_subject = $ns->message;
The above code cleans up any HTML/Script which the user may have entered in the subject line.
Step Nine:
save process_form.php and upload both form.php and process_form.php to your webserver.
What the above script modification will do
With the above changes to your scripts, you can now allow the form users to enter their own subject line. The script will validate the subject line value, and if successful, you will receive your email message with the users subject line.