Forms on webpage

Discussion in 'Web Design and Development' started by jammer08, Jul 25, 2006.

  1. jammer08 macrumors member

    Joined:
    May 26, 2006
    #1
    I am currently hosting my website on a macbook using apache, and I need to add some forms to my site that will submit the info to me via email. I have the forms set up, but when you try to submit a form on the site, you get an error. Anyone know how to correct this? Do I need a mail server running or something? Any help is greatly appreciated.
     
  2. Palad1 macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #2
    You need to set-up a cgi script to handle the mail.
    If you don't have time to learn the know-how, in the meantime, try this:
    PHP:
    <FORM METHOD=POST ACTION="mailto:YOURADDRESS@YOURHOST.COM" ENCTYPE="text/plain">
    <
    INPUT TYPE="text" NAME="username"> : name <BR>
    <
    INPUT TYPE="text" NAME="email"> : email <BR>
    comments <BR>
    <
    TEXTAREA NAME="COMMENTS" ROWS="10" WRAP="hard">
    </
    TEXTAREA>
    <
    INPUT NAME="redirect" TYPE="hidden" VALUE="index.html">
    <
    INPUT NAME="NEXT_URL" TYPE="hidden" VALUE="index.html">
    <
    BR>
    <
    INPUT TYPE="submit" VALUE="Send">
    <
    INPUT TYPE="reset" VALUE="Clear">
    </
    FORM>
    </
    CENTER>
    substitute YOURADDRESS@YOURHOST.COM with your email address.
    Be warned though, the form will be sent by the person visiting your site, not the server, thus if he doesn't have an email client installed, you're S.O.L.

    You may want to read this to get you started on the wonderful world of CGI programming ;) http://developer.apple.com/internet/opensource/php.html
     
  3. jammer08 thread starter macrumors member

    Joined:
    May 26, 2006
    #3
    Thanks for the information. That really won't work for my situation... I need it to be send by my server... Thanks for the resource, I will read up and try to figure it out. Thanks again.
     
  4. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #4
    if you want your server to send it out, then yes you'll need a mail server running. Tiger comes with Postfix already, it's only a matter of enabling it.

    Most easily done with Postfix Enabler but can be done by editing a few system conf files - there's a hint here (it's for Panther but i think it still applies).

    Then you need to send the mail to Postfix from the form.

    Not sure about other alternatives, but it's pretty straightforward in PHP using the mail() function:
    Code:
    <?
    
    // get posted data into local variables
    $EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
    $EmailTo = "you@yours.com";
    $Subject = "Email Subject";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Address = Trim(stripslashes($_POST['Address'])); 
    $Telephone = Trim(stripslashes($_POST['Telephone'])); 
    $Message = Trim(stripslashes($_POST['Message']));
    
    // validation
    $validationOK=true;
    if (Trim($EmailFrom)=="") $validationOK=false;
    if (Trim($Name)=="") $validationOK=false;
    if (!$validationOK) {
      print "<meta http-equiv=\"refresh\" content=\"0;URL=contacterror.html\">";
      exit;
    }
    
    // prepare email body text
    $Body = "";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "Address: ";
    $Body .= $Address;
    $Body .= "\n";
    $Body .= "Telephone: ";
    $Body .= $Telephone;
    $Body .= "\n";
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $Message;
    $Body .= "\n";
    
    // send email 
    $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    
    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=contactok.html\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=contactbad.html\">";
    }
    ?>
     
  5. jammer08 thread starter macrumors member

    Joined:
    May 26, 2006
    #5
    THANK YOU! this is what I have been looking for! :)
     
  6. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #6
    As far as i remember, on a mac you don't need to alter anything in the php.ini file - there's more on that side of things here, but most of it is for Windows) which you might want to check thru - although it says in the default ini file that it will send to Sendmail (the previous mail server that came with macs prior to 10.3, i think), the OS redirects sendmail requests to Postfix automatically
     

Share This Page