FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

If you have created a form on your site you can help...

 


Captain Fertile
HI guys

I've searched and can't find a similar thread so here goes:

PLEASE help me...

I really need help with some forms on my site.

I use Dreamweaver and wonder what code (or oprtions to choose) I need to use to get a form working on my site.

I can create the forms the way I want them, add the buttons but Have no idea what I need to do to get the forms working on a site.

The forms merely need to send an email to a specified email account containing the input information whether or not the email received is in plain English doesn’t matter to much I can read HTML but plain English would be cool.

It would also be ideal if the form would go to a confirm page (which I already have created) or even a message to let the poster know (pop up box maybe) that the email has gone (I have been to sites where nothing happens and you wonder whether your message has gone).

I know all this has to do with CGI bins etc. but I have no idea how it works and descriptions on the web read like gobbledegook.

Any examples I can try to cut and paste or simply tell me what I need to do. I know it will be straightforward once I have done it a couple of times but right now it seems pretty difficult

I am sure some of you Gurus out there remember what it was like to be learning so please help a beginner in trouble.

Please help if you can.
Confused
welshsteve
Give me a minute. I'll do one for you.



Had to do something important for work. Looks like I was beaten to it Very Happy


Last edited by welshsteve on Tue Dec 12, 2006 4:26 pm; edited 1 time in total
aningbo
captian, here you go.. its in php and not cgi.

here's the form sample...
Code:

<form action="formemail.php" method="post">
<input type="hidden" name="thankspage" value="thanx.php">
<input type="hidden" name="ccsubmitter" value="yes">
Name
    <input type="text" size="35" maxlength="256" name="name">
E-Mail
    <input type="text" size="35" maxlength="256" name="email">
website
<input type="text" size="35" maxlength="256" name="website">
Comment
   <textarea rows="10" name="comment" cols="37"></textarea>
<input type="submit" value="send">
</form>


and the php script to process. name it as "formemail.php"

Code:

<?php

$adminemail = 'youremail';

$version = '1.10';
$controlvars = ' thankspage submitteremail ccsubmitter ';
$messagetoadmin = $HTTP_POST_VARS['submitteremail'] ." has filled out a form with this content:


";
 $messagetosubmitter = "You have submitted a form with the content listed below. thank you.


";
while(list($key, $value) = each($HTTP_POST_VARS))
{
 if (!stristr($controlvars, ' '. $key .' '))
 {
  $messagetoadmin .= $key .': '. $value .'

';
  $messagetosubmitter .= $key .': '. $value .'

';
 }
}
mail($adminemail, 'Form Submitted: '. stripslashes($HTTP_POST_VARS['subject']), stripslashes($messagetoadmin), 'From: '. $HTTP_POST_VARS['submitteremail']);

if ($HTTP_POST_VARS['ccsubmitter'] == 'yes')
{
 mail($HTTP_POST_VARS['submitteremail'], 'Form Submitted: '. stripslashes($HTTP_POST_VARS['subject']), stripslashes($messagetosubmitter), 'From: '. $adminemail);
}
if ($_POST['autoresponse'] != '')
{
 $body = geturl($autoresponse);
 mail($submitteremail, 'Re: '. stripslashes($HTTP_POST_VARS['subject']), stripslashes($body), 'From: '. $adminemail);
}
header('Location: '. $HTTP_POST_VARS['thankspage']);
// just in case redirect doesn't work
die('<meta http-eqiv="refresh" content="0;url='. $HTTP_POST_VARS['thankspage'] .'">');
function geturl($url)
{
 if (version_compare("4.3.0", phpversion(), "<"))
 {
  $filecontents = @file_get_contents($url);
 }
 else
 {
  $fd = @fopen($url, 'rb');
  $filecontents = @fread ($fd, 30000000);
  @fclose ($fd);
 }
 return $filecontents;
}

?>


it can process any kind of html form inputs and so dont bother with the fields. just add any number. gud luck.
Captain Fertile
Thanks very much aningbo, that is more help than I could have hoped for.

I have never used .php before so a couple of questions.

Arrow Will any page containing this form need to have the .php extension also? All the other pages I have on the site are basic .htm pages.

Arrow Will I be able to add my own form elements (drop down lists, browse buttons for attaching files etc.) using Dreamweaver? And if I do this will I have to alter your code?

Arrow Will the pages have to uploaded to a live server before I can test them?

Sorry to be a pain, I know newbies can ask stupid questions but using CGI and .htm (or the dreaded .asp) is bad enough, .php is completely new territory for me.

Thank you for your continued patience and help.
welshsteve
See answers below

Captain Fertile wrote:

Arrow Will any page containing this form need to have the .php extension also? All the other pages I have on the site are basic .htm pages.

No. The pages that have the form can be just .html or .htm extension. The key here is in the ACTION attribute in the HTML form. That MUST point to the PHP file

Arrow Will I be able to add my own form elements (drop down lists, browse buttons for attaching files etc.) using Dreamweaver? And if I do this will I have to alter your code?


If you wish to add fields, the PHP will require amending so that the e-mail function handles the additional fields.


Arrow Will the pages have to uploaded to a live server before I can test them?

It is possible to install PHP and Apache on a local machine, see THIS THREAD. But it is probably best if you save the files to your public_html directory in your frihost domain account.


Sorry to be a pain, I know newbies can ask stupid questions but using CGI and .htm (or the dreaded .asp) is bad enough, .php is completely new territory for me.

Thank you for your continued patience and help.


I have a slightly simpler script that might be easier to explain how to add additional fields. I will do this in another post, or I can PM you instead
welshsteve
I've private messaged you but thought I'd post here so others can benefit from it if they need it.

Here is a simple e-mail form. I've placed the elements inside a table for alignment purposes.
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Simple e-mail form</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

</head>

<body>

<form method="post" action="domail.php">
   <table border="0" cellspacing="5">
      <tr>
         <td valign="top">Name:</td>
         <td valign="top"><input type="text" size="40" name="pname" /></td>
      </tr>
      <tr>
         <td valign="top">E-Mail:</td>
         <td valign="top"><input type="text" size="40" name="email" /></td>
      </tr>
      <tr>
         <td valign="top">Message:</td>
         <td valign="top"><textarea rows="10" cols="60" name="msg"></textarea></td>
      </tr>
      <tr>
         <td valign="top"><input type="submit" name="btnSubmit" value="Send" /></td>
         <td valign="top"><input type="reset" name="btnReset" value="Reset Form" /></td>
      </tr>
   </table>
</form>

</body>
</html>


Here is a simple e-mail script called domail.php which will handle the e-mailing of the form data.
Code:

<?php
  $pname = $_REQUEST['pname'] ;
  $email = $_REQUEST['email'] ;
  $msg = $_REQUEST['msg'] ;

  if (!isset($_REQUEST['email'])) {
    header( "Location: emailform.html" );
  }
  elseif (empty($pname) || empty($email) || empty($msg)) {
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>E-Mail Form</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
   
</head>

<body>

<h1>Error</h1>
<p>Not all fields have been filled in.  Please try again.</p>
<p><a href="emailform.html">Back to form</a></p>
</body>
</html>';
}
  else {
    mail( "[color=blue]$email, yourename@yourdomain.com", "Email from website",
          "$pname", "Dear Captain Fertile,\n\n$msg\n\nKind Regards\n\n") ;
    header( "Location: thanks.html" );
  }
?>


Notice the names in red match the names given to the input elements in the HTML form.

This e-mail will e-mail a copy of the resulting e-mail to you and to the person filling out the form. The bit in [color=blue]blue does this.

Now I'll explain the bit below
Code:

"Email from website",
          "$pname", "Dear Captain Fertile,\n\n$msg\n\nKind Regards\n\n") ;
    header( "Location: thanks.html" );


"Email from website" = Subject of the e-mail being sent.

"$pname" = This appends the name given in the form to the end of the e-mail being sent.

"Dear Captain Fertile,\n\n$msg\n\nKind Regards\n\n" = this is the message. \n denotes a carriage return. $msg embeds the text from the message field as the text of the e-mail.

header( "Location: thanks.html" = this tells the browser to redirect to a page called thanks.html after the form is submitted.

This line..

elseif (empty($pname) || empty($email) || empty($msg)) {

Checks to make sure that all the fields have been filled out. If they haven't, the HTML page inside the echo part is displayed on screen.

Now saying you wanted to ask the user how they heard about you. You want to add a select box to the e-mail form with the options - "Word of mouth", "Search engine", "You told me".

Firstly, you add this select to the e-mail form.
Code:

<form method="post" action="domail.php">
   <table border="0" cellspacing="5">
      <tr>
         <td valign="top">Name:</td>
         <td valign="top"><input type="text" size="40" name="pname" /></td>
      </tr>
      <tr>
         <td valign="top">E-Mail:</td>
         <td valign="top"><input type="text" size="40" name="email" /></td>
      </tr>
      <tr>
         <td valign="top">How did you hear about me?</td>
         <td valign="top">
            <select name="how">
               <option value="word of mouth">Word of mouth</option>
                 <option value="a search engine">Search Engine</option>
                 <option value="you telling me">You told me</option>
            </select>
         </td>
      </tr>
      <tr>
         <td valign="top">Message:</td>
         <td valign="top"><textarea rows="10" cols="60" name="msg"></textarea></td>
      </tr>
      <tr>
         <td valign="top"><input type="submit" name="btnSubmit" value="Send" /></td>
         <td valign="top"><input type="reset" name="btnReset" value="Reset Form" /></td>
      </tr>
   </table>
</form>


Now you amend the PHP file to include this field, called "how" into the e-mail handling.

Code:

<?php
  $pname = $_REQUEST['pname'] ;
  $email = $_REQUEST['email'] ;
  //additional field added here
  $how = $_REQUEST['how'] ;
  $msg = $_REQUEST['msg'] ;

  if (!isset($_REQUEST['email'])) {
    header( "Location: emailform.html" );
  }
  //additional field added here
  elseif (empty($pname) || empty($email) || empty($how) || empty($msg)) {
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>E-Mail Form</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
   
</head>

<body>

<h1>Error</h1>
<p>Not all fields have been filled in.  Please try again.</p>
<p><a href="emailform.html">Back to form</a></p>
</body>
</html>';
}
  else {
    mail( "$email, yourename@yourdomain.com", "Email from website",
          "$pname", "Dear Captain Fertile,\n\nI heard about your website via $how and thought I would take a look.\n\n$msg\n\nKind Regards\n\n") ;
    header( "Location: thanks.html" );
  }
?>


Hope this makes sense. I was going to highlight changes in bold or colours but the code tags don't allow text formatting.
Captain Fertile
I would like to thank you guys for the quick replies with this.

The time and effort is greatly appreciated.

I donated 50Frih$ to aningbo for the script he provided and I have donated 100Frih$ to welshsteve for his scripts, the PM and the detailed instructions.

I hope these donations are okay and you should have received them by now.

Thank you so very much I will see how I get on with your help.

Many many thanks
The Captain
welshsteve
Thank you Captain Fertile, much appreciated. Glad to be of help and GOOD LUCK!
aningbo
i'm a little late because of my exams and glad steve did helped you with all the necessary.

the form i provided works with any kind of form fields and you dont have to worry about any fields again, just keep on adding them in any html form. yeah guud luck.
motha
don't forget to set the $adminemail variable to your email address. just change youremail to your email address leaving the quotes there
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

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