FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help!!!





Spida48
I have been searching on the internet for two days now to find a simple php mailform script.

I need a form with 5 fields and it to be able to click on 'submit' and it send the form to my email.

As I don't understand php very well the scripts are so complicated that I just don't understand the first thing about it. Is there ANYONE OUT THERE that can help me with this script. Thanks Crying or Very sad
jmraker
About the simplest way is
Code:
<html><body>
<?php

$sent = false;

function formValid(){
   // Do validation, return false if there's an error, true if the form is ok to send
   return true;
}

if(isset($_POST['send'])){
   if(formValid()){   // If the form is valid, send the mail
      $to = 'me@myaddress.com';
      $headers = 'From: ' . $to . "\r\n";
      $subject = 'An email message';
      $body = 'First Line' . "\n\n";
      foreach($_POST as $k=>$v)
         $body .= $k . ': ' . $v . "\n";
      mail($to, $subject, $body, $headers);   // sends the email
      echo "Thank you";
      $sent = true;
   }
   else
      echo 'There are errors';
}

if(!$sent){   // if not sent, show the form
?>
<h1>Fill out this form</h1>
<form method="post">
Field 1: <input type="text" name="field_1" value="<?php echo $_POST['field_1'];?>" /><br>
Field 2: <input type="text" name="field_2" value="<?php echo $_POST['field_2'];?>" /><br>
Field 3: <input type="text" name="field_3" value="<?php echo $_POST['field_3'];?>" /><br>
Field 4: <input type="text" name="field_4" value="<?php echo $_POST['field_4'];?>" /><br>
Field 5: <input type="text" name="field_5" value="<?php echo $_POST['field_5'];?>" /><br>
<input type="submit" name="send" value="Send" />
</form>
<?php
}
?>
</body>


Should send an email like
Quote:
First Line

field_1: 11111
field_2: 22222
field_3: 33333
field_4: 44444
field_5: 55555
send: Send


Where the field names are field_1 - field_5, you can name the fields anything you want, and change the email address, subject and "First Line" to your liking
sonam
You can find simple script for php mail but this is not good because you must prevent spam, hack, etc. Here you can see easy way to build mail form.
http://www.frihost.com/users/sonam/blog/vp-83950.html

If you need any help feel free to ask here.

Sonam
Spida48
jmraker - you are an angel!!! This is just what I was looking for. It is only for a private site and it won't go on the search engines so I hope I will get away with spammers/hackers etc.

sonam - thanks I am looking for a script to do the same thing but secure - I will check out your link thanks - I am not very good at anything programmy - I have a brain block as soon as I see it!!! But will have a go. Thanks
Spida48
jmraker

This probably sounds a stupid question but you said change field_1 to anything I like etc., did you mean

Field 1: <input type="text" name="field_1" value="<?php echo $_POST['field_1'];?>" /><br>
Field 2: <input type="text" name="field_2" value="<?php echo $_POST['field_2'];?>" /><br>
Field 3: <input type="text" name="field_3" value="<?php echo $_POST['field_3'];?>" /><br>
Field 4: <input type="text" name="field_4" value="<?php echo $_POST['field_4'];?>" /><br>
Field 5: <input type="text" name="field_5" value="<?php echo $_POST['field_5'];?>" /><br>

the Field 1: or the "field_1" and then do I have to change the POST ['field_1']

I really am stupid when it comes to this!! Sorry Embarassed
rvec
"Field 1:" is just the description the users sees, you can change this to anything you want and it doesn't have to be anything related to the field or fieldname (but it normally does).
"field_1" is the variable name, and if you change that on one place you'll have to change it everywhere (only 2 places per post-variable in this script).
Spida48
Hi Sorry - I told you I really am stupid when it comes to this sort of thing!!! I put the code into the web page but I certainly didn't get what I thought I would get.

Code for the page:

<!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>Claire and Dave's Wedding</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000099;
}
body {
background-color: #b8cffe;
background-image: url(images/bg.jpg);
}
a {
font-size: 12px;
color: #000099;
}
a:hover {
color: #66CCFF;
}
h1 {
font-size: 14px;
color: #000066;
}
.style1 {font-size: 16px}
-->
</style>
<script type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
</head>

<body onload="MM_openBrWindow('http://www.stanhillcourthotel.co.uk','Hotel','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=640,height=450')">
<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<th width="620" height="250
" scope="col"><img src="images/heading1.jpg" alt="Heading" width="619" height="250" /></th>
<tr>
<th width="670" height="125" class="content-cell" scope="col"><div align="justify" class="content-cell">
<p align="center" class="title style1">RSVP Reply Form </p>
<?php

$sent = false;

function formValid(){
// Do validation, return false if there's an error, true if the form is ok to send
return true;
}

if(isset($_POST['send'])){
if(formValid()){ // If the form is valid, send the mail
$to = 'design@spidas.co.uk';
$headers = 'From: ' . $to . "\r\n";
$subject = 'RSVP Form Reply';
$body = 'Dear Claire and Dave' . "\n\n";
foreach($_POST as $k=>$v)
$body .= $k . ': ' . $v . "\n";
mail($to, $subject, $body, $headers); // sends the email
echo "Thank you";
$sent = true;
}
else
echo 'There are errors';
}

if(!$sent){ // if not sent, show the form
?>
<h1>RSVP Reply Form</h1>
<form method="post">
From: <input type="text" name="from" value="<?php echo $_POST['from'];?>" /><br>
Telephone No.: <input type="text" name="telephone_no" value="<?php echo

$_POST['telephone_no'];?>" /><br>
Favourite Song: <input type="text" name="favourite_song" value="<?php echo

$_POST['favourite_song'];?>" /><br>
Dietary Requirements: <input type="text" name="dietary-requirements" value="<?php echo

$_POST['dietary_requirements'];?>" /><br>
Reply: <input type="text" name="reply" value="<?php echo $_POST['reply'];?>" /><br>
<input type="submit" name="send" value="Send" />
</form>
<?php
}
?>
</tr>
<tr>

</table>
</body>
</html>

When it shows up I can't work out how to make the reply field have a message panel. Would it be simpler if I constructed an html form and then is there a php script thaat I could put in just to send it?
I'm sorry to be a pain, but I just don't get it. Embarassed
sonam
Please, when you post code put this code in code brackets. For example:
Code:
<?php
echo "I am posted in the brackets!";
?>


I am not sure but maybe you need to deffine action for your form.
Code:
<form method="post" action="">


Sonam
urtrivedi
I am not able to understand your problem.

I guess that you want to know how the code is flowing and what is actually happening.

I am explaining what I got from the code
1) This page is used by webuser.
2) User gives its telephone no, favourite song, dietary requirement and submits
3) on clicking submit text fields are validated.
4) If validated then this same page is called on submit and its sends posted data to one fix email ID 'design@spidas.co.uk'
5) For sending mail successfully your webserver must be configured for It.
6) If mail function fails it gives error.

I hope I have got you right. If not then please correct me.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.