Jump to content

How to submit a form to an e-mail


Guest nbdesign

Recommended Posts

Guest nbdesign

hi,i am new to php scripting. and i have looked through the php tutorial on w3schools siteand did not find the answer to my question.is it possible to send a name and last name or email address to a email address witha submit button, plus have this submit button take you to a web page?it's like asigning two different functions to one button! (i think, and i hope this has something to do with php)HELP! :)

Link to comment
Share on other sites

This is possible.This is the php file I use for these kind of things (I got this from someone a while back, so I can't really take credit for it :)):2 Steps:First you create a php file.File: formmail.php (code below)Then you create a form in the page you wish to display this (Code for form at bottom of post),1. File: formmail.php Code:

<?function parse_form($array) {   // reserverte ord   $reserved_keys[] = "require";   $reserved_keys[] = "recipient";   $reserved_keys[] = "subject";   $reserved_keys[] = "bgcolor";   $reserved_keys[] = "text_color";   $reserved_keys[] = "link_color";   $reserved_keys[] = "vlink_color";   $reserved_keys[] = "alink_color";   $reserved_keys[] = "title";   $reserved_keys[] = "missing_fields_redirect";   $reserved_keys[] = "env_report";   if (count($array)) {      while (list($key, $val) = each($array)) {         $reserved_violation = 0;         for ($ri=0; $ri<count($reserved_keys); $ri++) {            if ($key == $reserved_keys[$ri]) {               $reserved_violation = 1;            }         }         if ($reserved_violation != 1)            $content .= "\t$key: $val\n<br>";      }   }   return $content;}// Send innholdet som epostfunction mail_it($content, $subject, $email, $recipient) {	mail($recipient, $subject, $content, "From: $email\r\nReply-To: email\r\nX-Mailer: DT_formmail");}function build_body($title, $bgcolor, $text_color, $link_color, $vlink_color, $alink_color) {   if ($title)      echo "<title>$title</title>";   if (!$bgcolor)      $bgcolor = "#FFFFFF";   if (!$text_color)      $text_color = "#000000";   if (!$link_color)      $link_color = "#0000FF";   if (!$vlink_color)      $vlink_color = "#FF0000";   if (!$alink_color)      $alink_color = "#000088";   if ($background)      $background = "background=\"$background\"";   echo "<body bgcolor=\"$bgcolor\" text=\"$text_color\" link=\"$link_color\" vlink=\"$vlink_color\" alink=\"$alink_color\" $background>\n\n";}function print_error($reason,$type = 0) {   build_body($title, $bgcolor, $text_color, $link_color, $vlink_color, $alink_color);   if ($type == "missing") {      ?>      The form was not submitted for the following reasons:<p>     <ul><?     echo $reason."\n";     ?></ul>     Please use your browser's back button to return to the form and try again.<?   } else { // alle andre error      ?>      The form was not submitted because of the following reasons:<p>      <?   }   echo "<br><br>\n";   exit;}if (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $recipient)) {   print_error("<b>I NEED VALID RECIPIENT EMAIL ADDRESS TO CONTINUE</b>");}if ($require) {   $require = ereg_replace( " +", "", $require);   $required = split(",",$require);   for ($i=0;$i<count($required);$i++) {      $string = trim($required[$i]);      if((!(${$string})) || (!(${$string}))) {         if ($missing_fields_redirect) {            header ("Location: $missing_fields_redirect");            exit;         }         $require;         $missing_field_list .= "<b>Missing: $required[$i]</b><br>\n";      }   }   if ($missing_field_list)      print_error($missing_field_list,"missing");}if (($email) || ($EMAIL)) {   $email = trim($email);   if ($EMAIL)      $email = trim($EMAIL);   if (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $email)) {      print_error("your <b>email address</b> is invalid");   }   $EMAIL = $email;}$content = parse_form($HTTP_POST_VARS);if ($env_report) {   $env_report = ereg_replace( " +", "", $env_report);   $env_reports = split(",",$env_report);   $content .= "------ eviromental variables ------";   for ($i=0;$i<count($env_reports);$i++) {      $string = trim($env_reports[$i]);      if ($env_reports[$i] == "REMOTE_HOST")         $content .= "REMOTE HOST: ".$REMOTE_HOST."\n";      else if ($env_reports[$i] == "REMOTE_USER")         $content .= "REMOTE USER: ". $REMOTE_USER."\n";      else if ($env_reports[$i] == "REMOTE_ADDR")         $content .= "REMOTE ADDR: ". $REMOTE_ADDR."\n";      else if ($env_reports[$i] == "HTTP_USER_AGENT")         $content .= "BROWSER: ". $HTTP_USER_AGENT."\n";   }}mail_it($content, $subject, $email, $recipient);if ($redirect) {   header ("Location: $redirect");   exit;} else {   print "Takk for bestillingen\n";}?>

2. FormCode:

<form name="form1" method="post" action="http://some-url-for-your-webpage/formmail.php"> <input type=hidden name="recipient" value="youremail@somewhere.com"> <input type=hidden name="subject" value="Topic"> <input type=hidden name="required" value="Name,Text"> <input type=hidden name="redirect" value="http://tb31.homeip.net/takk.htm"> Name<br><input type=text name="Name" size="20"><br />Some Text:<br /> <textarea name="Text" rows="5" cols="50"></textarea><br> <br><input type="submit" name="Submit" value="Submit"> </form>

There, this should be pretty much cut and paste for you, just change a few things in the form(URL, email, and maybe the values you want to send.

Link to comment
Share on other sites

Hmm, looks very helpful for what im doing at the moment. Can you make any form you like with the same script (the formmail.php one)?Or is the code you have there specificly designed for what nbdesign is doing.alzable

Link to comment
Share on other sites

  • 1 year later...

this worked for me to, the only problem is it sends the form even if the required fields are blank, and before it gave me a header error so I had to comment out these lines : header ("Location: $redirect"); exit;} else also whats the function of these <input type=hidden name="redirect" value="http://tb31.homeip.net/takk.htm">and how to make the form only send if the fields are filled ?I tought I only had to change these -> <input type=hidden name="required" value="email, nome, address, addressnumber, complemento, CEP, cidade" />thats what i did on mine ;/

Link to comment
Share on other sites

this worked for me to, the only problem is it sends the form even if the required fields are blank, and before it gave me a header error so I had to comment out these lines : header ("Location: $redirect"); exit;} else also whats the function of these <input type=hidden name="redirect" value="http://tb31.homeip.net/takk.htm">and how to make the form only send if the fields are filled ?I tought I only had to change these -> <input type=hidden name="required" value="email, nome, address, addressnumber, complemento, CEP, cidade" />thats what i did on mine ;/
That's not a really good idea because it can easily be changed. Try using !empty() in a conditional to see if all required fields are filled out.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...