PDA

View Full Version : PHP Contact Form - found at TWH site - need more fields?


Mondo-Man
04-08-2008, 03:56 PM
Hi,
First, I'll confess to being new to PHP, but I did hand-code the HTML and CSS for my own Web site, so I'm familiar with concepts and terms of other languages in a basic sense. That said...

I have the cool "PHP contact form with image verification" that I found here (http://www.thewebhelp.com/php/php_contact_form_with_image_validation/)at TheWebHelp, and I am trying to tweak the code to suit my specific needs.

I have created my own fields and cobbled together that form table and this code. Now, when the message is delivered, the only information provided is the name, the email, and the message. I have a contact.php page and another file called mailer.php. In mailer.php, I have this line of code which I believe affects what information is delivered (See? I'm trying...).

mail("todd.ruby@example.com", 'Online Form: '.$name, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from");

I would like to have in the body of the email that's delivered to me the following fields from my form:

Name
Title
Organization
Phone Number
Email
Subject
Message

In the header of the email message, I would like only the following:
Name
Email
Subject

Can someone help me tweak the code to send those fields divided thus in my mailer email?

Thanks OH so much!

krystaldragon
04-08-2008, 11:04 PM
I, too, would love an answer to this question. I've tried my hand at altering various things in both the index and mailer scripts. Nothing seems to work.

Mondo-Man
04-08-2008, 11:28 PM
Hey, Krystal--
This was my first post--made too quickly perhaps. I looked at the other three (3) threads in this PHP forum, and both are about this contact form. The thread (http://thewebhelp.com/forum/showthread.html?t=15)started by Purple Rose dated 3-10-08 I think has the correct code in her last post. I haven't yet had a chance to try it, but she says it works for her.

http://thewebhelp.com/forum/showthread.html?t=15

krystaldragon
04-08-2008, 11:49 PM
I think I'm victim of the same thing. I'm currently dissecting her code as well. Would you like me to post what I think the ultimate "changes" are, locations, etc, at least as best as I can figure out.

Thanks for the reply!

Mondo-Man
04-09-2008, 12:59 AM
That'd be great--I'll do the same and perhaps we'll get this working for us :-)

krystaldragon
04-09-2008, 04:30 PM
I think I have it.

On the index or contact page, you'll add something new like:

<input name="fname" type="text" id="fname" value="<?php echo $_GET['fname'];?>"/>

This will add the new field for someone to fill in. It seems to work with the Radio buttons and all that. I just used the text for example.

On the Mailer page I added:

$fname = $_REQUEST["fname"]; (under the load variables area)
$fname = stripslashes($fname); (under the remove backslash thing)

mail("email@domain.com", 'Online Form: '.$subject, 'Message1: '.$message."\n".'fname: '.$fname."\n", "From: $from");

Here's what I've noticed about the above line.

The comma is a separator for the various fields, To, Subject, etc.
Things in the body of the text are separated with a period.
So, the line is basically like this:
mail(To filed, subject line, first item in the message.second item in the message.third item in the message.etc, from field);

The ticks and quotes are just used for added stuff, like if you want to include a space, etc. the \n is a new line. enclose in "".

If you'd like to see my full code I'm more than willing to post it. I just have a lot of fields. Decided to break it down.

Mondo-Man
04-09-2008, 06:18 PM
Krystal--
I just got this advice, too, from another forum where I posted our question(s):

-------------------------------------------
Emails can only have four "boxes": To, Subject, Message and the From field, so if you have more, some have to be combined. (recall the the sample form has only 4 fields

You do this by enclosing the fields in "....."

mail("todd.ruby@example.com", "$name $email $subject", "$name $title $organisation $phone $email $subject $message", "From: $from");

on the assumption that you have the above fields.

The above will appear on the same line but you can force a line break with \n or double with \n\n like

"$name\n$email\n$subject", etc.

See the notes here, just above Numbers of input boxes

http://www.wickham43.net/formemail.php
--------------------------
...I'm also still trying to figure how to make some info appear in the email header, and other info to appear in the email body; perhaps those commas I made red above are the dividers? I also have more than 4 fields (address, phone, etc).

I'm just getting a chance to get back to this trouble this afternoon and will post whatever works for me; it'd be helpful if you could post your code, too.
THANKS!
--TBR
PS--I added myself to the map at your web page :-)

krystaldragon
04-09-2008, 11:53 PM
WOOT! Thank you!

Here are my scripts. The radio buttons aren't properly working yet, but that's an error on my end. But the rest of the script is reporting properly.

I'm not able to post my whole script as it is too long for the page, but here are the important parts. If you'd like a full script. bop me an email addy, I'll email it to ya.

Contact page:
<form action="apmailer.php" method="post" name="form1" id="form1" style="margin:0px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px; width:300px;" onsubmit="MM_validateForm('from','','RisEmail','subject','', 'R','verif_box','','R','message','','R');return document.MM_returnValue">
Your e-mail:<br>
<input name="from" type="text" id="from" value="<?php echo $_GET['from'];?>"/>
<input name="Application" type="hidden" id="Application" value="<?php echo $_GET['Application'];?>"/>
<p>
Your Name:<br>
<input name="name" type="text" id="name" value="<?php echo $_GET['name'];?>"/>
<p>
Your Character's Name:<br>
<input name="charname" type="text" id="charname" value="<?php echo $_GET['charname'];?>"/>
<p>
Are you 18 years of age or older:<br>
Yes<input name="age" type="radio" id="age" value="<?php echo $_GET['age'];?>"/>
No<input name="age" type="radio" id="age" value="<?php echo $_GET['age'];?>"/>
<p>
Your Specialization:<br>
<input name="spec" type="text" id="spec" value="<?php echo $_GET['spec'];?>"/>
<p>
Type verification image:<br />
<input name="verif_box" type="text" id="verif_box"/>
<img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" />
<p>
<!-- if the variable "wrong_code" is sent from previous page then display the error field -->
<?php if(isset($_GET['wrong_code'])){?>
<div style="border:1px solid #990000; background-color:#D70000; color:#FFFFFF; padding:4px; padding-left:6px;width:295px;">Wrong verification code</div><br />
<?php ;}?>
Reason why you wish to join our Guild:<br>
<textarea name="message" cols="15" rows="5" id="message"><?php echo $_GET['message'];?></textarea>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left" valign="top">
<center>
<input name="Submit" type="submit" value="Send Message"/>

And the Mailer:
<?php
// remember to replace you@email.com with your own email address lower in this code.

// load the variables form address bar
$Application = $_REQUEST["Application"];
$message = $_REQUEST["message"];
$from = $_REQUEST["from"];
$verif_box = $_REQUEST["verif_box"];
$name = $_REQUEST["name"];
$charname = $_REQUEST["charname"];
$age = $_REQUEST["age"];
$spec = $_REQUEST["spec"];

// remove the backslashes that normally appears when entering " or '
$message = stripslashes($message);
$subject = stripslashes($subject);
$from = stripslashes($from);
$name = stripslashes($name);
$charname = stripslashes($charname);
$age = stripslashes($age);
$spec = stripslashes($spec);

// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("Loreene@sfaguild.net", 'Online Form: '.$Application, 'Name: '.$name."\n".'Character Name: '.$charname."\n".'Over 18: '.$age."\n".'Spec: '.$spec."\n".'Message: '.$message, "From: $from");
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
} else {
// if verification code was incorrect then return to contact page and show error
header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
exit;
}
?>

If you'd like to see it in action you can go to: http://sfaguild.net/application.php
Or if you want to see the full script like I said, bop me an email and I'll email it to you.

bendshead
04-12-2008, 01:02 PM
Krys, Mondo -

I think I was in same position as both of you, and thanks to your insightful posts I got a little motivated to try to modify the script myself (and with results!). Added fields for name and telephone number and changed subject field to dropdown.

A couple of things I'll want to do:

1. a more sophisticated validation design. Right now the script returns dialog boxes -- my preferences is for error messages above the form. I've tried this by looking at other scripts but I just can't get it.

2. send a copy of the email to the poster/the user who is submitting the form. I haven't tried this yet, but have some ideas.

If you have had any success with the above, would much appreciate your replies as you are both quite articulate, and I feel like we're speaking the same language!

Regards,
Hank

bendshead
04-12-2008, 05:35 PM
A couple of things I'll want to do:

1. a more sophisticated validation design. Right now the script returns dialog boxes -- my preferences is for error messages above the form. I've tried this by looking at other scripts but I just can't get it.




This type of validation was nicely handled by http://code.google.com/p/mc-validation/.
Some decent customization options, easy to implement, functions for all of the common of validation rules already prepared.

I still would like to be able to send a copy of the email to the user who submits it. I'd also like to like to know if there's an easy way to return the same page after it's successfully submitted and have a Thank you message atop the page. I would prefer this over having the form action take me to the mailer.php/new page.

Thanks

krystaldragon
04-14-2008, 08:27 PM
Good afternoon!

I'm honestly not certain about the error message / sophistication of the script, but the confirmation email shouldn't be a problem.

The code would be:

mail("you@email.com; $from", "subject", "message", "$from");

I have confirmed that this works but here are the problems with this code.

Whomever you are sending to will see your email address. This is not a 'carbon copy' email. Both addresses show up in the "to" field.

I'm not certain if this will work on all servers. The people I host my website with use Linux servers. I'm not certain if the ; is the separator on all servers. Would be something to look into.

As for your other question on the redirect and not using the "mailer.php" page. I'm not certain I understand what you want. Technically the mailer page is the thank you page. If you wanted to have the form and the thank you all in one page I suppose you could do something like an if statement. Basic psudo code would be something like:

If the submit button is pressed then display (html thank you page) else display (html form page).

I'd guess the actual code would be like:
if($_POST[submit]) {
(mailer.php stuff here)
}
else {
(contact.php stuff here)
}

Just speculation there.

Mondo-Man
04-15-2008, 03:59 PM
Here's what I finally used in the mailto line, with the four fields doing extra duty to pass all the extra fields I have:

mail("my.email@example.com", "Online Form: $name $email $subject", "Contact: \n$name\n$title\n$organization\n$phone\n$email\n\n $subject\n Message: $message", "From: $from");

All those /n through the code puts each item onto a separate line, no need for "" except around the primary fields' groupings.

Thanks for your help!

krystaldragon
04-15-2008, 05:02 PM
I always end up adding so much more work. lol

Thank you for that!