Jump to content

CONTACT FORM


khujo56

Recommended Posts

  • Replies 53
  • Created
  • Last Reply

This is always fun looking at how people do stuff the hard way.

 

If you are going to be using more forms like this, I would suggest a different method of error reporting than just echoing or exiting, bad habit and does not allow for full control.

 

I would actually suggest a validate function

 

<?php
function validate(&$error_msg) {
       $validate_array = array("email", "name", "message"); // add more here for verification
       $error_msg = "";
       foreach ($validate_array as $value) {
               switch ($value) {
                        case 'email':
                               // note requires a "is_email" function that you have to create
                               if (!isset($_POST[$value]) || empty($_POST[$value]) || !is_email($_POST[$value])) {
                                       $error_msg .= "Email is missing  or in properly formatted.<br />";
                               }
                        break;

                        case 'name':
                                if (!isset($_POST[$value]) || empty($_POST[$value])) {
                                        $error_msg .= "Name field is requried.<br />";
                                }
                        break;

                        case 'message':
                                if (!isset($_POST[$value]) || empty($_POST[$value])) {
                                        $error_msg .= "Message field is requried.<br />";
                                }
                        break;
               }
       }

       if (!empty($error_msg)) {
             return false;
       }

       return true;
}

function is_email($email) {
     return true; // for now you need to write the code here.
}

if (isset($_POST['submit'])) {
      if (!validate($error_msg)) {
             header('Location: http://www.placeiwanttoredirectto.com/page.php?error_msg=' . $error_msg);
             die();
      }

       // else everything is valid do processing here     
}
?>

 

Which in return would allow you expand easily and have it in a nice function could could be included via a functions file and should allow for you to use this in any other script with ease.

Link to comment
https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288885
Share on other sites

Alternativly, modify this to your hearts content.

Form.htm

<form method="POST" action="sendmail.php">
<div align="center">
 <center>
 <table border="0" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="400">
   <tr>
     <td width="122"><b>Name:</b></td>
     <td width="270"><input type="text" name="name" size="20"></td>
   </tr>
   <tr>
     <td width="122"><b>E-mail:</b></td>
     <td width="270"><input type="text" name="email" size="20"></td>
   </tr>
   <tr>
     <td width="122"><b>Message:</b></td>
     <td width="270"></td>
   </tr>
   <tr>
     <td width="396" colspan="2"><textarea rows="6" name="message" cols="47"></textarea></td>
   </tr>
   <tr>
     <td width="392" colspan="2"><input type="submit" value="Submit"></td>
   </tr>
 </table>
 </center>
</div>
</form>

 

 

sendmail.php

<?php
if(isset($_POST['name']) && empty($_POST['name']))
{
    $error[] = "Post your name";
}
if(isset($_POST['email']) && empty($_POST['email']))
{
    $error[] = "Post your name";
}
if(isset($_POST['message']) && empty($_POST['message']))
{
    $error[] = "Post your name";
}
if(is_array($error))
{
   echo("errors:<br>");
   foreach($error as $x => $i)
   {
       echo("- ".$i."<br>");
   }
}
else
{
    $to = "[email protected]";
    $subject = "subject";
    $message = $_POST['message'];
    $name = $_POST['name'];
    $from = $_POST['email'];
    mail($to, $subject, $message, "From: $name<$from>");
    
    echo("<script>alert(\"Thanks!\nE-mail has been sent!\");</script>");
    
    $url = "redirect_page.htm";
    header("Location: $url");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288887
Share on other sites

Problem with your function.

you could make an array quicker than running your function :/

 

This is always fun looking at how people do stuff the hard way.

 

If you are going to be using more forms like this, I would suggest a different method of error reporting than just echoing or exiting, bad habit and does not allow for full control.

 

I would actually suggest a validate function

 

<?php
function validate(&$error_msg) {
       $validate_array = array("email", "name", "message"); // add more here for verification
       $error_msg = "";
       foreach ($validate_array as $value) {
               switch ($value) {
                        case 'email':
                               // note requires a "is_email" function that you have to create
                               if (!isset($_POST[$value]) || empty($_POST[$value]) || !is_email($_POST[$value])) {
                                       $error_msg .= "Email is missing  or in properly formatted.<br />";
                               }
                        break;

                        case 'name':
                                if (!isset($_POST[$value]) || empty($_POST[$value])) {
                                        $error_msg .= "Name field is requried.<br />";
                                }
                        break;

                        case 'message':
                                if (!isset($_POST[$value]) || empty($_POST[$value])) {
                                        $error_msg .= "Message field is requried.<br />";
                                }
                        break;
               }
       }

       if (!empty($error_msg)) {
             return false;
       }

       return true;
}

function is_email($email) {
     return true; // for now you need to write the code here.
}

if (isset($_POST['submit'])) {
      if (!validate($error_msg)) {
             header('Location: http://www.placeiwanttoredirectto.com/page.php?error_msg=' . $error_msg);
             die();
      }

       // else everything is valid do processing here     
}
?>

 

Which in return would allow you expand easily and have it in a nice function could could be included via a functions file and should allow for you to use this in any other script with ease.

 

Its not a problem with the function. The function would work just fine. Just a different way of doing it. I generally only use validate functions because than I have one location to change all that information. I actually go a lot deeper in my personal code so one validate function can validate everything my site needs.

 

Just an extra idea, that does work.

 

The nicest thing about it, if I have certain fields like name and message that do not need any special validation I can easily do something like this:

 

<?php
function validate(&$error_msg) {
       $validate_array = array("email", "name", "message"); // add more here for verification
       $error_msg = "";
       foreach ($validate_array as $value) {
               switch ($value) {
                        case 'email':
                               // note requires a "is_email" function that you have to create
                               if (!isset($_POST[$value]) || empty($_POST[$value]) || !is_email($_POST[$value])) {
                                       $error_msg .= "Email is missing  or in properly formatted.<br />";
                               }
                        break;

                        case 'message':
                        case 'name':
                                if (!isset($_POST[$value]) || empty($_POST[$value])) {
                                        $error_msg .= ucwords($value) . " field is requried.<br />";
                                }
                        break;
               }
       }

       if (!empty($error_msg)) {
             return false;
       }

       return true;
}

function is_email($email) {
     return true; // for now you need to write the code here.
}

if (isset($_POST['submit'])) {
      if (!validate($error_msg)) {
             header('Location: http://www.placeiwanttoredirectto.com/page.php?error_msg=' . $error_msg);
             die();
      }

       // else everything is valid do processing here     
}
?>

 

                        case 'message':
                        case 'name':
                                if (!isset($_POST[$value]) || empty($_POST[$value])) {
                                        $error_msg .= ucwords($value) . " field is requried.<br />";
                                }
                        break;
               }

 

Which saves on typing time.

Link to comment
https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288903
Share on other sites

I didnt mean to say there is a problem with your function, as there wasn't

it's just i find arrays quicker and easier to do then messing with Swith, case and break.

:)

 

Switch/cases are very useful and easy to use. Either way its personal preference. I love the functionality and flexibility a switch/case can give you over just an array.

 

 

 

Generally, hotmail aol gmail and yahoo have spam filters that filter out emails. Usually emails coming from servers are filtered out as spam. Check the hotmail junk box. There is really no set solution other than making sure you set mail headers properly. www.php.net/mail

Link to comment
https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-288918
Share on other sites

well i tried my actual e-mail address associated with my website and i still don't get anything.

what could be the problem??

 

Sometimes servers do not like sending email address to [email protected] something to do with the relaying bit of it, unsure. I know my server will not send an email address to that address, so I have a gmail account setup which forwards all emails to my main account address and just use that for emails to be sent from my server.

Link to comment
https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-289120
Share on other sites

so i finally figured out that i did not have a php package for my hosting. finally switched over to php, it works fine but it does redirect to the assigned address. instead i get this message

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/n/r/jnr1975/html/mailer.php:31) in /home/content/j/n/r/jnr1975/html/mailer.php on line 34....it should redirect to the main page. below is the php script.

 

<?php

if(isset($_POST['name']) && empty($_POST['name']))

{

    $error[] = "Post your name";

}

if(isset($_POST['email']) && empty($_POST['email']))

{

    $error[] = "Post your e-mail address";

}

if(isset($_POST['message']) && empty($_POST['message']))

{

    $error[] = "Post your comments";

}

if(is_array($error))

{

    echo("errors:<br>");

    foreach($error as $x => $i)

    {

        echo("- ".$i."<br>");

    }

}

else

{

    $to = "[email protected]";

    $subject = "subject";

    $message = $_POST['message'];

    $name = $_POST['name'];

    $from = $_POST['email'];

    mail($to, $subject, $message, "From: $name<$from>");

   

    echo("<script>alert(\"Thanks!\E-mail has been sent!\");</script>");

   

    $url = "http://www.iheartvacation.com/";

    header("Location: $url");

}

?>

Link to comment
https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290177
Share on other sites

<?php
if(isset($_POST['name']) && empty($_POST['name']))
{
     $error[] = "Post your name";
}
if(isset($_POST['email']) && empty($_POST['email']))
{
     $error[] = "Post your e-mail address";
}
if(isset($_POST['message']) && empty($_POST['message']))
{
     $error[] = "Post your comments";
}
if(is_array($error))
{
    echo("errors:
");
    foreach($error as $x => $i)
    {
        echo("- ".$i."
");
    }
}
else
{
     $to = "[email protected]";
     $subject = "subject";
     $message = $_POST['message'];
     $name = $_POST['name'];
     $from = $_POST['email'];
     mail($to, $subject, $message, "From: $name<$from>");
     
     echo("<script>alert(\"Thanks!\E-mail has been sent!\");</script>");
     
if($to){
     $url = "http://www.iheartvacation.com/";
     header("Location: $url");
}
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290251
Share on other sites

HI

 

i tried that script..when it says that the info has been sent, instead of re-directing to the assigned page, i get this message instead :

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/n/r/jnr1975/html/mailer.php:33) in /home/content/j/n/r/jnr1975/html/mailer.php on line 37

Link to comment
https://forums.phpfreaks.com/topic/57653-contact-form/page/2/#findComment-290581
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.