Jump to content

Simple Php Contact Form-helpme


darknightelor

Recommended Posts

Hello all,I'm using a contact form from this source http://www.thewebhelp.com/php/php_contact_...age_validation/,And I've run into a few problems:1.I want to add a name (full user name) field into the form. Can you please show me what to do (just paste the code with the changes).2.I wanna send the message in a non-English language which uses UTF-8. But when I get the email, the subject is in gibberish, how do I make it be sent in UTF-8?This is the code (which you can also download from the link I gave):Contact.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Contact Us</title><script type="text/JavaScript"><!--function MM_findObj(n, d) { //v4.01  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);  if(!x && d.getElementById) x=d.getElementById(n); return x;}function MM_validateForm() { //v4.0  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);	if (val) { nm=val.name; if ((val=val.value)!="") {	  if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');		if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';	  } else if (test!='R') { num = parseFloat(val);		if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';		if (test.indexOf('inRange') != -1) { p=test.indexOf(':');		  min=test.substring(8,p); max=test.substring(p+1);		  if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';	} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }  } if (errors) alert('The following error(s) occurred:\n'+errors);  document.MM_returnValue = (errors == '');}//--></script></head><body> <form action="mailer.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" style="padding:2px; border:1px solid #CCCCCC; width:180px; height:14px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;" value="<?php echo $_GET['from'];?>"/><br /><br />Subject:<br /><input name="subject" type="text" id="subject" style="padding:2px; border:1px solid #CCCCCC; width:180px; height:14px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;" value="<?php echo $_GET['subject'];?>"/><br /><br />Type verification image:<br /><input name="verif_box" type="text" id="verif_box" style="padding:2px; border:1px solid #CCCCCC; width:180px; height:14px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;"/><img src="verificationimage.php?<?php echo rand(0,9999);?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /><br /><br /><!-- 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;}?>Message:<br /><textarea name="message" cols="6" rows="5" id="message" style="padding:2px; border:1px solid #CCCCCC; width:300px; height:100px; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px;"><?php echo $_GET['message'];?></textarea><input name="Submit" type="submit" style="margin-top:10px; display:block; border:1px solid #000000; width:100px; height:20px;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:11px; padding-left:2px; padding-right:2px; padding-top:0px; padding-bottom:2px; line-height:14px; background-color:#EFEFEF;" value="Send Message"/></form></body></html>

Mailer.php

<?php// ----------------------------------------- //  The Web Help .com// ----------------------------------------- // remember to replace you@email.com with your own email address lower in this code.// load the variables form address bar$subject = $_REQUEST["subject"];$message = $_REQUEST["message"];$from = $_REQUEST["from"];$verif_box = $_REQUEST["verif_box"];// remove the backslashes that normally appears when entering " or '$message = stripslashes($message); $subject = stripslashes($subject); $from = stripslashes($from); // check to see if verificaton code was correctif(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){	// if verification code was correct send the message and show this page	mail("you@email.com", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$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;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>E-Mail Sent</title><style type="text/css"><!--body,td,th {	font-family: Arial, Helvetica, sans-serif;	font-size: 12px;}--></style></head><body>Email sent. Thank you.<br /><br />Return to <a href="/">home page</a> ? </body></html>

NOTE: I save it as UTF-8 and then using Notepad++ Convert it to ANSI. Please help me,Thanks.

Link to comment
Share on other sites

I did change it. But no matter what the subject comes as gibberish. I even tried:enctype="multipart/form-data" accept-charset="UTF-8" in the formand "header("Content-Type: text/html; charset= Windows-1255");" in the php (tried also utf-8 and).Also, please can you tell me how to add a "name" field into this form (I managed to add name field, but it caused the "from" field to disappear making no email sender).EDIT: Okay, I managed to receive the message in the real language. I used ISO 8859-8. Now I only need to know how to add another field.

Link to comment
Share on other sites

That script's security is a bit poor, the post vars shouldn't be accessed using the REQUEST superglobal (use the POST one instead) and they should be validated (e.g. proper email address). Also, the escape slashes should be negated by disabling magic_quotes, not by manually removing them, which is unreliable (what if you install on a system without magic quotes?). http://www.php.net/manual/en/security.magi...s.disabling.phpTo add an extra "field", just concatenate that string onto the message (in the example, the form has a field called "name". Alternatively, you could give the name as the alias for the from address.

$name = $_POST['name'];//...mail("you@email.com", "Online Form: $subject", "{$_SERVER['REMOTE_ADDR']\n\n$message\n\nName: $name", "From: $name <$from>");

Link to comment
Share on other sites

The code you gave me is not good.Gives me this error:"Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING "How to fix it, what is the proper code?Edit: OKay, I managed to fix this problem. But now I got more problems:1.I've got a header at the top of the php :

header("Content-Type: text/html; charset=ISO 8859-8");

But it cause problem because there is another header in the "else":

 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");

And when the else applies, it gives "Header may not contain more than a single header, new line detected." error...2.Subject title still in gibberish...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...