Creating a simple feedback form using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coldrex
    New Member
    • Nov 2007
    • 6

    Creating a simple feedback form using php

    Hi all,

    here my simple code:

    Code:
    <?php
      
    $name = $_REQUEST['name']; 
    $email = $_REQUEST['text']; 
    $comment = $_REQUEST['comment']; 
    
    mail( "mymail@xyz.xz", "Feedback Form",
    $name, $comment, "From: $email" );
    header( "Location: http://www.megawhite.au" );
    
    ?>
    I need to check, if ALL of the fields are filled. If yes - then go to www.megawhite.. if NOT - to some other www.

    I know, that it is somehow possible with "empty" command.. :rolleyes:

    Can You put me on the right way, please?

    Thanx.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by coldrex
    Hi all,

    here my simple code:

    Code:
    <?php
      
    $name = $_REQUEST['name']; 
    $email = $_REQUEST['text']; 
    $comment = $_REQUEST['comment']; 
    
    mail( "mymail@xyz.xz", "Feedback Form",
    $name, $comment, "From: $email" );
    header( "Location: http://www.megawhite.au" );
    
    ?>
    I need to check, if ALL of the fields are filled. If yes - then go to www.megawhite.. if NOT - to some other www.

    I know, that it is somehow possible with "empty" command.. :rolleyes:

    Can You put me on the right way, please?

    Thanx.
    You could use strlen()

    i.e.
    [php]
    if((strlen($nam e) > 0) && (strlen($text) > 0) && strlen($comment ) > 0)){
    // fields aren't empty - execute relevant code
    } else {
    // a field is empty - execute relevant code
    }
    [/php]
    That's just an easy and robust way of doing it :)

    Comment

    • coldrex
      New Member
      • Nov 2007
      • 6

      #3
      thanx, man :)

      so, now it looks like below:

      Code:
      <?php
        
      $name = $_REQUEST['name']; 
      $email = $_REQUEST['text']; 
      $comment = $_REQUEST['comment']; 
       
      if((strlen($name) > 0) && (strlen($email) > 0) && strlen($comment) > 0)){
         header ("Location: http://www.pleasefilltheform.com");
      } 
      
      else {
      mail("mymail@xyz.xz", "Feedback Form",
      $name, $comment, "From: $email");
      header ("Location: http://www.thankyoupage.com");
      }
      
      ?>
      but the thing is, that when i receive form results on email, in sender's field i see the mail, i indicated in my hosting options, e.g. i write in form: blabla@bla.com, but when the message arrives, i see not blabla@com., but info@mywebsite. com... ?

      why is it so, and how we can change it?

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Your question is not that much clear to me, but check this out.
        [CODE=php]$to = 'your_mail@doma in.com';
        $headers = "Reply-to: $email\n";
        $headers .= 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: Web Mailer <from_addreass@ goes_here.com>' . "\r\n";
        mail($to, "Mail Subject", 'Mail Body Mail Body Mail Body', $headers);[/CODE]

        Better to use these headers with your mail function

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by ajaxrand
          Your question is not that much clear to me, but check this out.
          [CODE=php]$to = 'your_mail@doma in.com';
          $headers = "Reply-to: $email\n";
          $headers .= 'MIME-Version: 1.0' . "\r\n";
          $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
          $headers .= 'From: Web Mailer <from_addreass@ goes_here.com>' . "\r\n";
          mail($to, "Mail Subject", 'Mail Body Mail Body Mail Body', $headers);[/CODE]

          Better to use these headers with your mail function
          Also looking at your code, it would seem you had it wrong.

          Doing this
          [php]
          if((strlen($nam e) > 0) && (strlen($email) > 0) && strlen($comment ) > 0)){
          header ("Location: http://www.pleasefillt heform.com");
          } else {
          mail("mymail@xy z.xz", "Feedback Form",
          $name, $comment, "From: $email");
          header ("Location: http://www.thankyoupag e.com");
          }
          [/php]
          would send the mail if the strlen function checks return false! i.e. sending the email if the fields havent been filled out.

          Swap it around
          i.e.
          [php]
          if(((strlen($na me) > 0) && (strlen($email) > 0) && strlen($comment ) > 0))){
          //strlen() returns TRUE, send mail.
          mail("mymail@xy z.xz", "Feedback Form",
          $name, $comment, "From: $email");
          header ("Location: http://www.thankyoupag e.com");
          } else {
          // email wasnt filled out properly! Redirect.
          header ("Location: http://www.pleasefillt heform.com");
          }
          [/php]

          Comment

          • coldrex
            New Member
            • Nov 2007
            • 6

            #6
            Originally posted by markusn00b
            [php]
            if(((strlen($na me) > 0) && (strlen($email) > 0) && strlen($comment ) > 0))){
            //strlen() returns TRUE, send mail.
            mail("mymail@xy z.xz", "Feedback Form",
            $name, $comment, "From: $email");
            header ("Location: http://www.thankyoupag e.com");
            } else {
            // email wasnt filled out properly! Redirect.
            header ("Location: http://www.pleasefillt heform.com");
            }
            [/php]
            thanx again, now this part is clear for me :) but what about those headers from Ajaxrand? as i uderstand i should write the addresses like variables to send the mail correctly?

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Originally posted by coldrexcoldrext hanx
              thanx again, now this part is clear for me :) but what about those hAjaxrandrom Ajaxrandudersta nd i uderstand i should write the addresses like variables to send the mail correctly?
              For the mail() function you can pass headers. Its only a enhancement for your script. first I thought that, you are having a problem with sending "FROM" header with the mail. Sorry If I got it on the wrong way. anyway have a look at these user comments on mail function anphpeaders in the php manual
              You can set the parameters for mail(......) function as variable or directly, No matter. but for better manageability of the mail function I am always passing variables, rather than typing the values. That's It.
              And also the headers, you need to learn more of them when time comes with HTML mails and mails with attachments.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Like ajax mentioned, the headers aren't completely necessary; if you just wanted to send a plain text email then you could leave them out, but if you wanted to send a html formatted email, then you would need to include these headers.

                :)

                Comment

                • coldrex
                  New Member
                  • Nov 2007
                  • 6

                  #9
                  i have not understood about those headers clearly - will they help to solve this "from" problem or no? :)

                  seems, i dont need them , as Markus said, I want just plain text results from my form.

                  Comment

                  • ak1dnar
                    Recognized Expert Top Contributor
                    • Jan 2007
                    • 1584

                    #10
                    Originally posted by coldrex
                    i have not understood about those headers clearly - will they help to solve this "from" problem or no? :)

                    seems, i dont need them , as Markus said, I want just plain text results from my form.
                    This "from" problem, what's wrong with your "From" header. Is it not displaying in the mail?

                    Comment

                    • coldrex
                      New Member
                      • Nov 2007
                      • 6

                      #11
                      yes, when i receive form results on my mail, i see in "from" field, the mail, i identified as my default hosting mail (in my hosting options). this is happening, inspite of writing the mail in "email" field.

                      Comment

                      • coldrex
                        New Member
                        • Nov 2007
                        • 6

                        #12
                        Originally posted by coldrex
                        yes, when i receive form results on my mail, i see in "from" field, the mail, i identified as my default hosting mail (in my hosting options). this is happening, inspite of writing the mail in "email" field.
                        are there any advices you can give?

                        Comment

                        Working...