Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Email form from Site

  1. #1
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Talking Email form from Site

    Hi there,

    I've been trying to piece together the PHP for a contact form using the other threads as I know there are a few going on but I'm getting stuck at every turn.

    I'm new to PHP so I'm just trying to get this practice form to work for now but I have a larger form that I'm really trying to use.

    The practice form

    HTML Code:
    <html>
    <body>
    
    <form method='post' action='mailform_sender.php'>
      Email: <input name='email' type='text' /><br />
      Subject: <input name='subject' type='text' /><br />
      Message:<br />
      <textarea name='message' rows='15' cols='40'>
      </textarea><br />
      <input type='submit' />
      </form>
    
    </body>
    </html>
    And here's mailform_sender.php:
    PHP Code:
    <?php
    function spamcheck($field)
      {
    //eregi() performs a case insensitive regular expression match
      
    if(eregi("to:",$field) || eregi("cc:",$field)) 
        {
        return 
    TRUE;
        }
      else
        {
        return 
    FALSE;
        }
      }

    //if "email" is filled out, send email
    if (isset($_REQUEST['email']))
      {
      
    //check if the email address is invalid
      
    $mailcheck spamcheck($_REQUEST['email']);
      if (
    $mailcheck==TRUE)
        {
        echo 
    "Invalid input";
        }
      else
        { 
        
    //send email
        
    $email $_REQUEST['email'] ; 
        
    $subject $_REQUEST['subject'] ;
        
    $message $_REQUEST['message'] ;
        
    mail("webmaster@torema.com.br"$subject,
        
    $message"From: $email);
        echo 
    "Thank you for using our mail form";
        }
      }
    else
    //if "email" is not filled out, display the form
    {
      echo 
    "please fill out an email address";
      }
    ?>
    Note: The bit at the top is something I learnt from w3school.com to avoid people using the form to send spam.

    This form is working fine, and the echo is appearing.

    The changes I want to make to start with are:
    1. Return to the page i was on when the form has been sent and write the echo there.
    2. Add more fields to the form and send their values along with the message.


    Thanks to anyone that can help and teach me a little about PHP along the way.

    PS Feel free to start again from scratch if you find it easier to help that way. Thanks.
    Last edited by dog; 12-14-2006 at 02:03 PM. Reason: To add a PS at the bottom

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You can add more fields to the form easily, just as long as you call them in your PHP code. As far as the redirect, you could redirecting to the original page (lets call it test.php) with the exception that you add something to the end of it. The url would be test.php?send=yes or test.php?send=no.

    then in test.php, place this at the beginning.

    Code:
    $send = $_REQUEST['send'];
    
    if ($send == "yes") {
    
       echo 'Your message was sent successfully. You should recieve blahblahblah...';
    }
    
    elseif ($send == "no") {
    
       echo 'Your message was NOT sent, due to a problem either with the script or the server. Please try again later!';
    }
    
    else {
       //echo the contact form
    }
    This should set you in the right direction. Let me know if you need any further help on this.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    Hey man,

    Thanks, that worked great.

    There's another problem that I'm having:

    I'm able to send the form without any name or email. I was using an if else function to check for something in the email field (and the same thing for the name)
    PHP Code:
    if (isset($_REQUEST['email']))
    {
    //send form
    }
    else
    {
    echo 
    'the email needs to be filled out';

    but it doesn't seem to be having any effect.

    The full size form is going to have a lot of required fields, may be there's a better method all together to doing this. I'm also considering javascript but so far all the methods I've found involve flashing up an alert box if it's invalid. I'd prefer to warn them through something more CSS stylable.

    Any suggestions?

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    try

    Code:
    if ($_REQUEST['email'] == "" or $_REQUEST['name'] == "") {
    //error message
    
    echo 'All form fields are required!';
    }
    
    else {
    
    //send the email
    
    mail($to,$subject,$message,$from);
    
    header('Location: test.php?send=yes'); //redirect to success message!
    }
    That will check to see if email and name fields have anything entered in it.
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    That worked perfectly, thanks again.

    It's starting to make sense. When you posted the solusion above I thought, it's so obvious when you know how.

    I think I like PHP.

    I've also been looking at www.php.net. I think you sent me a link to some info on there Testing... it's a good site.

    Thanks for all the help!

  6. #6
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question Please check this code for sending emails from a form.

    So I was starting to think I know what I'm doing and I've written this code but there are errors I don't understand.

    The PHP takes details from a form on another page, check the required fields aren't empty and then sends a couple of emails.

    The problems, I think, start when I try to define the messages of the emails. $message1 = a load of really complicated mixed up code.

    Here's whole of the code:

    PHP Code:
    <?php

    $name 
    $_REQUEST['name'];
    $company $_REQUEST['company'];
    $position $_REQUEST['position'];
    $tel $_REQUEST['telephone'];
    $email $_REQUEST['email'] ; 
    $city $_REQUEST['city'];
    $state $_REQUEST['state'];
    $country $_REQUEST['country'];
    $interest $_REQUEST['interest'];
    $contactType $_REQUEST['contactType'];
    $timeTable $_REQUEST['timeTable'];
    $message $_REQUEST['message'];
    $site $_REQUEST['site'];

    //set country as Brazil is it is not anything else
    if ($_REQUEST['country'] == '')
    {
    $country 'Brasil';}

    else 
    {
    $country $_REQUEST['country'];}

    // if any of the required fields are blank, redirect to an error message on original page
    if ($name =="" or $company =="" or $position=="" or $tel=="" or $email =="" or $city=="" or $state=="" or $interest=="")
    {
        
    //...and redirect to an error message.
        
    $host  $_SERVER['HTTP_HOST'];
        
    $uri  rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        
    $extra 'form.php?required=show';
        
    header("Location: http://$host$uri/$extra");
        exit;
    }

    //otherwise send the emails and a redirect to a success note
    else
    {
        
    //define subjects and messages for each of the emails
        
    $subject1 'Fale Conosco - E-mail do Site';
        
    $subject2 'Contato com a Torema Brasil';

        
    //here is where the problems start!
        //set the message to be sent to the Torema
        
    $message1 'E-mail do Site da Pagina: Fale Conosco\r\n\r\n'

            'Nome: '
    .$name.'\r\n'
            'Cargo: '
    .$company.'\r\n'
            'Empresa: '
    .$company.'\r\n'
            'Cidade: '
    .$city.' / '.$state.

            
    //if the country is anything other than Brasil write it
            
    if ($country=='Brasil')
                { echo 
    '\r\n\r\n';}
            else
                { echo 
    $country'\r\n\r\n';}

            
    'Intressa: '.$interest.'\r\n'
            
            
    //if there is a message write it
            
    if ($message=='')
                { echo 
    '\r\n';}
            else
                { echo 
    'Mensagem: '.$message.'\r\n';}

            
    'Deseja Contato Por: '.$contactType.'\r\n'
            'Horario de Contato: '
    .$timeTable.'\r\n\r\n'

            'Fone: '
    .$tel.'\r\n'
            'E-mail: '
    .$email.'\r\n'
            'Site: www.'
    .$site.'\r\n';


        
    //set the thankyou message to be sent to user
        
    $message2 'Obrigado por o seu mensagem à Torema Brasil \r\n\r\n'
            'A gente vai ver blah blah... \r\n\r\n'
            'Support, Torema Brasil. \r\n\r\n'
    ;
                        
        
    //send the email to Torema
        
    mail("webmaster@torema.com.br"$subject1$message1"From: $email);

        
    //.send the email to the user
        
    mail('<'.$name.'>'$email$subject2$message2"From: no-reply@torema.com.br"); */

        
    //redirect to the original page and show a success note.
        
    $host  $_SERVER['HTTP_HOST'];
        
    $uri  rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        
    $extra 'form.php?send=yes';
        
    header("Location: http://$host$uri/$extra");
        exit;

    }

    ?>
    An error turns up for line 49 which is the second of these two:
    PHP Code:
    $message1 'E-mail do Site da Pagina: Fale Conosco\r\n\r\n'

    'Nome: '
    .$name.'\r\n' 
    Yet, I am sure it's not the only fault. If anyone can suggest what's wrong with it and how it could be improved that would awesome.

    Peace

    dog

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    There are a lot of semicolons ( ; ) missing from the ends of the statements, as well as some added single quotes in the variables. I don't have the time to go and correct all of the code right now, but as soon as I get back from my company's Christmas party, I would be more than happy to correct. Unless someone else on the forum does so.

    The advise I can give you right now is that you go back and edit the code to add the semicolons to the ends of the statements (echos, variables, etc) and make sure to take out any quotes that should not be there.

    Hope this helps!
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks teach!

    I'll give that a go. Have a good party!

  9. #9
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    I went through and cleaned up the code a little bit. I'm not going to place the corrections in red like I usually do, but if you compare the code below to the original, you should be able to tell what I added/edited.

    PHP Code:
    <?php

    $name 
    $_REQUEST['name'];
    $company $_REQUEST['company'];
    $position $_REQUEST['position'];
    $tel $_REQUEST['telephone'];
    $email $_REQUEST['email'] ; 
    $city $_REQUEST['city'];
    $state $_REQUEST['state'];
    $country $_REQUEST['country'];
    $interest $_REQUEST['interest'];
    $contactType $_REQUEST['contactType'];
    $timeTable $_REQUEST['timeTable'];
    $message $_REQUEST['message'];
    $site $_REQUEST['site'];

    //set country as Brazil is it is not anything else

      
    if ($_REQUEST['country'] == '') {

             
    $country 'Brasil';

       }

       
       else {
       
             
    $country $_REQUEST['country'];
       
       }


    // if any of the required fields are blank, redirect to an error message on original page

    if ($name =="" or $company =="" or $position=="" or $tel=="" or $email =="" or $city=="" or $state=="" or $interest=="") {
        
    //...and redirect to an error message.

        
    $host  $_SERVER['HTTP_HOST'];
        
    $uri  rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        
    $extra 'form.php?required=show';
        
    header('Location: http://'.$host$uri.'/'.$extra);
        exit;
    }

    //otherwise send the emails and a redirect to a success note
    else {
        
    //define subjects and messages for each of the emails
        
        
    $subject1 'Fale Conosco - E-mail do Site';
        
    $subject2 'Contato com a Torema Brasil';

        
    //set the message to be sent to the Torema
        
    $message1 'E-mail do Site da Pagina: Fale Conosco\r\n\r\n';
        
    $message1 .= 'Nome: '.$name.'\r\n';
        
    $message1 .= 'Cargo: '.$company.'\r\n';
        
    $message1 .= 'Empresa: '.$company.'\r\n';
        
    $message1 .= 'Cidade: '.$city.' / '.$state;

            
    //if the country is anything other than Brasil write it

            
    if ($country=='Brasil') { 

                 echo 
    '\r\n\r\n';
            
            }
              
            else { 

                 echo 
    $country.'\r\n\r\n';
            
            }

         
    $message1 .= 'Intressa: '.$interest.'\r\n';
            
            
    //if there is a message write it
            
            
    if ($message=='') { 

                  echo 
    '\r\n';

            }
            
            else { 

               echo 
    'Mensagem: '.$message.'\r\n';
        
            }

         
    $message1 .= 'Deseja Contato Por: '.$contactType.'\r\n';
         
    $message1 .= 'Horario de Contato: '.$timeTable.'\r\n\r\n';
         
    $message1 .= 'Fone: '.$tel.'\r\n';
         
    $message1 .= 'E-mail: '.$email.'\r\n';
         
    $message1 .= 'Site: www.'.$site.'\r\n';


        
    //set the thankyou message to be sent to user

        
    $message2 'Obrigado por o seu mensagem à Torema Brasil \r\n\r\n';
        
    $message2 .= 'A gente vai ver blah blah... \r\n\r\n';
        
    $message2 .= 'Support, Torema Brasil. \r\n\r\n';
                        
        
    //send the email to Torema
        
    mail("webmaster@torema.com.br"$subject1$message1"From: $email");

        
    //.send the email to the user
        
    mail('<'.$name.'>'$email$subject2$message2"From: no-reply@torema.com.br"); 

        
    //redirect to the original page and show a success note.

        
    $host  $_SERVER['HTTP_HOST'];
        
    $uri  rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        
    $extra 'form.php?send=yes';
        
    header('Location: http://'.$host$uri.'/'.$extra);
        exit;

    }

    ?>
    Let me know if this fixes the problem, or if you need any more help.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  10. #10
    Join Date
    Apr 2006
    Posts
    205
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default

    Hi thetestingsite,

    Thanks for the help.

    The code didn't work perfectly first time. I've changed a few things, some more necessary than others and now it's working fine. I'll try to summaries the changes I've made. If anyone requests it I'll post the whole of the working code.

    I'm using Cream/Vim and changing single (') to double (") quotation marks seems to please it. I can tell because of the way it colours the code.

    Also I've copied Mad Professor Mwinter's style of coding and used {$variable} in place of ".$variable." when placing these things, (are they called variables?) within the strings.

    In my original code I was creating a lot of if else statements to write one thing if there was something in a field and write another thing if there wasn't. I was using the echo function to write the output but I've changed this a little. Instead of writing one echo or another I've written one definition or another for a new variable (terminology?).

    I've made all of the changes more or less at the same time, which was careless as now it's hard to tell what was necessary and what was just different. I'll try to illustrate the changes in the two codes below.

    Before (with errors):
    PHP Code:
     $message1 .= 'Cidade: '.$city.' / '.$state;

            
    //if the country is anything other than Brasil write it

            
    if ($country=='Brasil') { 

                 echo 
    '\r\n\r\n';
            
            }
              
            else { 

                 echo 
    $country.'\r\n\r\n';
            
            } 
    After, (error free):
    PHP Code:
    //if the country is anything other than Brasil write it
    if ($country =='Brasil' or $country =='brasil') {

             
    $countryMes 'Brasil';

       }

       
       else {
       
             
    $countryMes $country;
       
       }

        
    $message1 .= "Cidade: {$city} / {$state}{$countryMes}.\r\n\r\n"
    Thanks a lot for the help. I still have some more questions, two issues really.

    You'll notice I'm using this php code that we've been working on, let's call it form-sender.php, to validate form.php. If someone fills out all of the required fields save one and submits the browser moves to form-sender.php and back to form.php?send=error:required_fields_blank for them to read the echo explaining that not all of the required fields were filled. This way the from has been reloaded and all the fields that were filled out get blanked. Not very convenient! Can anyone suggest another way of using the PHP to check the required fields have content without leaving the page?

    The second issue is more urgent but also more simple. When the form is filled out correctly, gets sent and the user arrives on form.php?send=yes and sees and echo telling the it's all good I'd also like to trigger some javascript. Can anyone suggest a trigger?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •