27 Replies Latest reply on Oct 1, 2014 6:02 PM by uxk

    Basic HTML form and php

    Norwich66 Level 1

      I have been trying for days to set up a basic contact form and link it to a php script. I've downloaded various free examples but keep coming back with errors.

       

      Please, can someone direct me to a good, full tutorial on how to create a basic HTML form in Dreamweaver CC 14, and how to obtain a secure php script.

       

      Thanks.

        • 1. Re: Basic HTML form and php
          osgood_ Level 8

          This one is basic, Name, Email, Comments - has validation and spam security.

           

          Insert the below section at the top of the page where your from will be located: (Change XXXX's in  $to = "XXXXX@XXXXXXX.XX.XX'; to your email address)

           

          <?php

          if (isset($_POST['submit'])) {

              // get name

          $name = trim($_POST['name']);

          // get email address

          $email = trim($_POST['email']);

          // get comments

          $comments = stripslashes(trim($_POST['comments']));

          // check antiSpam field

          $antiSpam = trim($_POST['antiSpam']);

          if (!empty($antiSpam)) {

              exit;

          }

          $to = "XXXXX@XXXXXXX.XX.XX";

          $subject   = "Comments from website";

          $headers  = "From: $email\r\n";

          $headers .= "Reply-To: $email\r\n";

          $message = "Name: $name\n\n";

          $message .= "Email Address: $email\n\n";

          $message .= "Comments: $comments\n\n";

          mail($to, $subject, $message, $headers);

          $sent = "Mail was sent successfully";

          }

          ?>

           

           

          Insert the below section in the <head></head> section of the page where your form is located: (for validation by jQuery)

           

          <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

          <script>

           

          $(document).ready(function() {

          $('#submit').click(function()

          {

              var name = $('#name').val();

          if (name == null || name == "" ) {

              alert('Please provide your name');

              return false;

          }

          // check for valid email address

          var email = document.getElementById('email');

          var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

          if (!filter.test(email.value)) {

          alert('Please provide a valid email address');

          email.focus;

          return false;

          }

              var comments = $('#comments').val();

          if (comments == null || comments == "" ) {

              alert('Please provide your comments');

              return false;

          }

          });

          });

          </script>

          <style>

          #antiSpam {

              display: none;

          }

          </style>

           

          This is the form code  itself: (Change  - "form_submit.php" to the name of the document that your form is in - obviously must be a .php document to work) (style form with css)

           

          <?php

          if(isset($sent)) {

              echo "<h3>$sent</h3>";

          }

              ?>

          <form id="mailForm" method="post" action="form_submit.php">

          <p>

          <label for="name">Name</label><br>

          <input type="text" id="name" name="name">

          </p>

          <p>

          <label for="email">Email Address</label><br>

          <input type="text" id="email" name="email">

          </p>

          <p>

          <label for="address">Address</label><br>

          <input type="text" id="address" name="address">

          </p>

          <p>

          <label for="comments">Comments</label><br>

          <textarea id="comments" name="comments"></textarea>

          </p>

          <input type="text" id="antiSpam" name="antiSpam">

          <input type="submit" name="submit" id="submit" value="submit">

           

          </form>

          1 person found this helpful
          • 2. Re: Basic HTML form and php
            Norwich66 Level 1

            Thank you so much, osgood_, i appreciate it.

            • 3. Re: Basic HTML form and php
              osgood_ Level 8

              Norwich66 wrote:

               

              Thank you so much, osgood_, i appreciate it.

               

               

              If you need any more help with it then post back.

              • 4. Re: Basic HTML form and php
                MurraySummers Level 8

                 

                 

                This is the form code  itself: (Change  - "form_submit.php" to the name of the document that your form is in - obviously must be a .php document to work) (style form with css)

                 

                <?php

                if(isset($sent)) {

                    echo "<h3>$sent</h3>";

                }

                    ?>

                <form id="mailForm" method="post" action="form_submit.php">

                 

                 

                Actually, if you make the form tag like this, then you will never have to specify the filename -

                 

                <form id="mailForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">


                The page will always submit to itself whatever the filename. I believe the same thing will happen if you just leave the action attribute blank, but like doing it this way just in case...

                • 5. Re: Basic HTML form and php
                  osgood_ Level 8

                  MurraySummers wrote:

                   

                   

                   

                  This is the form code  itself: (Change  - "form_submit.php" to the name of the document that your form is in - obviously must be a .php document to work) (style form with css)

                   

                  <?php

                  if(isset($sent)) {

                      echo "<h3>$sent</h3>";

                  }

                      ?>

                  <form id="mailForm" method="post" action="form_submit.php">

                   

                   

                  Actually, if you make the form tag like this, then you will never have to specify the filename -

                   

                  <form id="mailForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">


                  The page will always submit to itself whatever the filename. I believe the same thing will happen if you just leave the action attribute blank, but like doing it this way just in case...

                   

                  You're right on both counts..........old habits die hard.

                  • 6. Re: Basic HTML form and php
                    Rob Hecker2 Adobe Community Professional & MVP

                    I have often read that using PHP_SELF is a XSS vulnerability; however, as long as the parameters are being sanitized, there should be no problem.

                    • 7. Re: Re: Basic HTML form and php
                      MurraySummers Level 8

                      Thanks for the tip, Rob. I found this recommendation on stack overflow -

                       

                      $phpSelf = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_URL);

                      Then use $phpSelf instead of the $_SERVER['PHP_SELF'].

                      The reason is that $_SERVER['PHP_SELF'] simply executes what's in the address bar on submit, and the address bar's content can be diddled by the visitor before submitting the form, hence SECURITY RISK.

                      1 person found this helpful
                      • 8. Re: Basic HTML form and php
                        Rob Hecker2 Adobe Community Professional & MVP

                        Murry, I was unaware of that. Thank you. It's certainly the solution for security.

                         

                        PHP_SELF is indeed handy, but I don't use if for a reason unrelated to security. My websites are all based on a single page - index.php, so the page never changes but the parameters change a lot, and I must carefully control their order. The urls use a slash to split parameters, so they look like this:

                         

                        /en/blog/2566   or

                        /en/calendar/256/2014-05-06

                         

                        explode() is then used to build an indexed array from the parameters

                         

                        The moral of this story is that sometimes you don't want to carry over the parameters that PHP_SELF provides. In such cases SCRIPT_NAME could be used

                        1 person found this helpful
                        • 9. Re: Basic HTML form and php
                          dmreid Level 1

                          "This is great, thanks! 

                           

                          I have tested it here and although I receive an email with the contents, the "your email was received successfully" doesn't appear.  So then I entered a different page hoping that it might work on it... but again it doesn't... any thoughts?

                           

                          Thanks in a advance

                          Donna

                          • 10. Re: Basic HTML form and php
                            osgood_ Level 8

                            dmreid wrote:

                             

                            "This is great, thanks!

                             

                            I have tested it here and although I receive an email with the contents, the "your email was received successfully" doesn't appear.  So then I entered a different page hoping that it might work on it... but again it doesn't... any thoughts?

                             

                            Thanks in a advance

                            Donna

                             

                            What code are you using?

                             

                            Paste the form code/page and the php in the forum and someone may be able to help you.

                            • 11. Re: Basic HTML form and php
                              dmreid Level 1

                              silly me... the link to my page didn't get added in:  http://www.1800gotblinds.com/contact.php

                               

                              Thanks

                              Donna

                              • 12. Re: Basic HTML form and php
                                osgood_ Level 8

                                dmreid wrote:

                                 

                                silly me... the link to my page didn't get added in:  http://www.1800gotblinds.com/contact.php

                                 

                                Thanks

                                Donna

                                 

                                You will need to paste the php code you are using to process the form in the forum as that can't be seen in your linked page.

                                • 13. Re: Basic HTML form and php
                                  dmreid Level 1

                                  the name of the page is:  http://www.1800gotblinds.com/contact.php

                                   

                                  I tried using the contact.php to have go to it with the ... we received your email, etc. however it didn't work on that page.  So then I created a php page called sentform which I was hoping would say .... we received your email and give them a link to go back to the website.  Thanks in Advance, Donna

                                   

                                  in the header:

                                  <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

                                  <script>

                                   

                                  $(document).ready(function() {

                                  $('#submit').click(function()

                                  {

                                      var name = $('#name').val();

                                  if (name == null || name == "" ) {

                                      alert('Please provide your name');

                                      return false;

                                  }

                                  // check for valid email address

                                  var email = document.getElementById('email');

                                  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

                                  if (!filter.test(email.value)) {

                                  alert('Please provide a valid email address');

                                  email.focus;

                                  return false;

                                  }

                                      var comments = $('#comments').val();

                                  if (comments == null || comments == "" ) {

                                      alert('Please provide your comments');

                                      return false;

                                  }

                                  });

                                  });

                                  </script>

                                  <style>

                                  #antiSpam {

                                      display: none;

                                  }

                                   

                                  then where the form is:

                                  <?php

                                  if (isset($_POST['submit'])) {

                                      // get name

                                  $name = trim($_POST['name']);

                                  // get email address

                                  $email = trim($_POST['email']);

                                  // get comments

                                  $comments = stripslashes(trim($_POST['comments']));

                                  // check antiSpam field

                                  $antiSpam = trim($_POST['antiSpam']);

                                  if (!empty($antiSpam)) {

                                      exit;

                                  }

                                  $to = "info@1800gotblinds.com";

                                  $subject   = "Comments from website";

                                  $headers  = "From: $email\r\n";

                                  $headers .= "Reply-To: $email\r\n";

                                  $message = "Name: $name\n\n";

                                  $message .= "Email Address: $email\n\n";

                                  $message .= "Comments: $comments\n\n";

                                  mail($to, $subject, $message, $headers);

                                  $sent = "Mail was sent successfully";

                                  }

                                  ?>

                                   

                                  <?php

                                  if(isset($sent)) {

                                      echo "<h3>$sent</h3>";

                                  }

                                      ?>

                                  <form id="mailForm" method="post" action="sentform.php">

                                  <p>

                                  <label for="name">Name</label><br>

                                  <input type="text" id="name" name="name">

                                  </p>

                                  <p>

                                  <label for="email">Email Address</label><br>

                                  <input type="text" id="email" name="email">

                                  </p>

                                  <p>

                                  <label for="address">Address</label><br>

                                  <input type="text" id="address" name="address">

                                  </p>

                                  <p>

                                  <label for="comments">Comments</label><br>

                                  <textarea rows="5" id="comments" name="comments"> </textarea>

                                  </p>

                                  <input type="text" id="antiSpam" name="antiSpam">

                                  <input type="submit" name="submit" id="submit" value="submit">

                                   

                                  </form>

                                  • 14. Re: Basic HTML form and php
                                    osgood_ Level 8

                                    It IS working in the contact.php file. The problem is the .Main class css style has been set to white in the Tablet css media query, so you are not seeing the results as the text is white. (if you look at the pages source code when viewed in a browser you can see "Mail was sent successfully" is set.)

                                     

                                     

                                    /* Tablet Layout: 481px to 768px. Inherits styles from: Mobile Layout. */

                                    .Main {

                                        color: #FFFFFF;

                                        width: 100%;

                                    }

                                     

                                    Restore the contact.php file and add the below selector to the Tablet css media query:

                                     

                                    .Main h3 {

                                        color: #000;

                                    }

                                    1 person found this helpful
                                    • 15. Re: Basic HTML form and php
                                      Dave Merchant MVP & Adobe Community Professional

                                      FYI, regexp-checking an email address is a lot more complex than the example shown here. A bunch more non-alpha characters are acceptable in the local-part (the "username") but there are specific rules about where they can appear: you can use full stops, but not two together. You can use (comments) but the braces must be closed... etc. Even spaces are permitted, provided the element is quoted. Then there's the topic of international domain names...

                                       

                                      See Email address - Wikipedia, the free encyclopedia for examples.

                                      • 16. Re: Re: Basic HTML form and php
                                        Randalex

                                        Hello, can you help me please?

                                        I think having followed the instructions, and then I made changes to the form, so changed also part of codes, which is the underlined in yellow.

                                        The result is that when from the Form file, I want to see on my local host, using Wampserver for testing in Google Chrome, you get the following view, which is in line with what I want:

                                        Appears to be well and I very happy, but when I fill the form and select "submit", your browser tells me the following:

                                        The entered codes are as follows:

                                        1. 1.      Insert the below section at the top of the page where your from will be located: (Change XXXX's in  $to = "XXXXX@XXXXXXX.XX.XX'; to your email address)

                                         

                                        <?php

                                        if (isset($_POST['submit'])) {

                                            // get name

                                        $name = trim($_POST['name']);

                                        // get email address

                                        $email = trim($_POST['email']);

                                        // get comments

                                        $comments = stripslashes(trim($_POST['comments']));

                                        // check antiSpam field

                                        $antiSpam = trim($_POST['antiSpam']);

                                        if (!empty($antiSpam)) {

                                            exit;

                                        }

                                        $to = "desarrollawxyz@gmail.com";

                                        $subject  = "Comments from website";

                                        $headers  = "From: $email\r\n";

                                        $headers .= "Reply-To: $email\r\n";

                                        $message = "Name: $name\n\n";

                                        $message .= "Correo electronico: $email\n\n";

                                        $message .= "Comments: $comments\n\n";

                                        mail($to, $subject, $message, $headers);

                                        $sent = "Mail was sent successfully";

                                        }

                                        ?>

                                         

                                        The above, are all the codes that are on that page, is stored in the project folder.

                                        I then followed these instructions:

                                        1. 2.      Insert the below section in the <head></head> section of the page where your form is located: (for validation by jQuery)
                                        2. 3.      This is the form code itself: (Change  - "form_submit.php" to the name of the document that your form is in - obviously must be a .php document to work) (style form with css)

                                        The result is this set of codes, which are all that are in the file:

                                         

                                        <!doctype html>

                                        <html>

                                        <head>

                                        <meta charset="utf-8">

                                        <title>Servicios médicos</title>

                                        </head>

                                         

                                        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

                                        <script>

                                         

                                        $(document).ready(function() {

                                        $('#submit').click(function()

                                        {

                                            var name = $('#name').val();

                                        if (name == null || name == "" ) {

                                            alert('Please provide your name');

                                            return false;

                                        }

                                        // check for valid email address

                                        var email = document.getElementById('email');

                                        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

                                        if (!filter.test(email.value)) {

                                        alert('Please provide a valid email address');

                                        1. email.focus;

                                        return false;

                                        }

                                            var comments = $('#comments').val();

                                        if (comments == null || comments == "" ) {

                                            alert('Please provide your comments');

                                            return false;

                                        }

                                        });

                                        });

                                        </script>

                                        <style>

                                        #antiSpam {

                                            display: none;

                                        }

                                        #mailForm #Cabezon img {

                                                        width: 95%;

                                                        float: left;

                                        }

                                        </style>

                                         

                                        <?php

                                        if(isset($sent)) {

                                            echo "<h3>$sent</h3>";

                                        }?>

                                        <form id="mailForm" method="post" action="../Caja de busqueda/Cod Form Contact/enviartest.php">

                                          <p>p>

                                            <label></label>

                                             </p>

                                          </p>

                                          <div id="Cabezon"><img src="../Imágenes logo/Logos/Grupyd_editado-1.png" width="1087" height="162" alt=""/></div>

                                          <p> </p>

                                          <p>  </p>

                                          <div id="Cuestionario" style="font-style: normal; font-family: Impact, Haettenschweiler, 'Franklin Gothic Bold', 'Arial Black', sans-serif; background-color: rgba(173,244,243,1.00); width: 95%; float: left; text-align: center;">Formulario de contacto:</div>

                                            <p>

                                        <div id="vacia"></div>

                                        <p><strong><em>Agradecemos a usted la oportunidad de atenderle:</em></strong>   

                                        <p>

                                        <label for="name"><strong>Nombre:</strong></label>

                                        <input name="name" type="text" required id="name" size="82" maxlength="40">

                                        </p>

                                          <p>

                                        <label for="name"><strong>Teléfono:</strong></label>

                                        <input name="name" type="text" required id="name" size="81" maxlength="40">

                                        </p>

                                        <p>

                                        <label for="email"><strong>Correo electrónico:</strong></label>

                                        <input name="email" type="text" required id="email" size="70">

                                        </p>

                                          <p>

                                        <label for="name"><strong>Ciudad y departamento:</strong></label>

                                        <input name="name" type="text" required id="name" size="80" maxlength="50">

                                        </p>

                                        <p>

                                        <label for="comments"><strong>Servicios que le interesa conocer:</strong></label>

                                        <br>

                                        <textarea name="comments" cols="84" rows="1" maxlength="500" id="comments"></textarea>

                                        </p>

                                        <p>

                                        <label for="comments"><strong>Mensaje:</strong></label>

                                        <br>

                                        <textarea name="comments" cols="84" rows="1" maxlength="500" required id="comments"></textarea>

                                        </p>

                                        <input type="text" id="antiSpam" name="antiSpam">

                                        <input type="submit" name="submit" id="submit" value="submit">

                                         

                                        </form>

                                         

                                        Additionally, I made the links from the button of the general staff, to the form. Likewise did the link from the form the code of instructions No. 1 (1. ( Insert the below section at the top of the page where your from will be located...).

                                        When I look at the result of filling out the form, from the general template, and apply "submit", the result of Chrome, is the same.

                                        Please can tell me what is the problem? Could it be that test simulates living on the web, but is not really on a external server?... Thank you very much for your support.

                                        • 17. Re: Basic HTML form and php
                                          Nancy OShea Adobe Community Professional & MVP

                                          You can't really test e-mail scripts locally because wamp doesn't have the send mail module.  To test form processing scripts, upload to your real server.

                                           

                                           

                                          Nancy O.

                                          • 18. Re: Basic HTML form and php
                                            Randalex Level 1

                                            Thank you Nancy O

                                            • 19. Re: Basic HTML form and php
                                              uxk Level 1

                                              Hello,

                                              Curious to know whether I need to have a php running server in order to test "submitting" a form. If we are using a Mac, does it not have PHP already to test this submit form functionality. I am also not sure which value I should include into the following tag:

                                              <form action="    " method="post">

                                               

                                               

                                              Thank you

                                              • 20. Re: Basic HTML form and php
                                                Nancy OShea Adobe Community Professional & MVP

                                                You need a web server that supports PHP if you intend to use a PHP form processing script.

                                                The form's action attribute is where you reference the location of your PHP script on the server.

                                                 

                                                <form action="path/form-to-email.php" method="post">

                                                 

                                                 

                                                Nancy O.

                                                • 21. Re: Basic HTML form and php
                                                  uxk Level 1

                                                  Hello,

                                                  It means that I need to check if the Web domain that I buy support PHP. Once I obtain the domain name, upload my website to the server,

                                                  I am not sure how do I locate the attribute from my website contact page or email (contact us). Kindly advise

                                                  Kux

                                                  • 22. Re: Basic HTML form and php
                                                    Rob Hecker2 Adobe Community Professional & MVP

                                                    The web domain is unrelated to the server type. You need a web server that supports PHP. Pick a Linux server and you will be fine.

                                                     

                                                    Your webhost (with the web server) almost certainly will provide you with domain registration services, which makes things easier for you, but it is also possible for you to purchase a domain through a service that does not host your website.

                                                    1 person found this helpful
                                                    • 23. Re: Basic HTML form and php
                                                      Nancy OShea Adobe Community Professional & MVP

                                                      The web domain name *yourdomain.com* is separate from web hosting.  Your web hosting provider is the web server space to which you will publish your site online.  The server must provide support for whichever form-processing scripts you plan to use with your HTML form -- either php, asp, asp.net, or something else.

                                                       

                                                      Your e-mail address should go inside your form processing script where it will be hidden from robots.  If you publish your e-mail address inside your HTML code, robots will find it and spam you relentlessly with junk mail.  Nobody wants that.

                                                       

                                                       

                                                      Nancy O.

                                                      1 person found this helpful
                                                      • 24. Re: Basic HTML form and php
                                                        uxk Level 1

                                                        Sounds good. Another related question: How would I test whether a form is being submitted using the local host of my Mac computer, before I FTP my website online?

                                                        Please let me know about the procedure to get my Mac (Apache and PHP) to work or activated? I am currently on Mac OSX 10.9.4

                                                        Thank you

                                                        • 25. Re: Basic HTML form and php
                                                          osgood_ Level 8

                                                          uxk wrote:

                                                           

                                                          Sounds good. Another related question: How would I test whether a form is being submitted using the local host of my Mac computer, before I FTP my website online?

                                                          Please let me know about the procedure to get my Mac (Apache and PHP) to work or activated? I am currently on Mac OSX 10.9.4

                                                          Thank you

                                                           

                                                          You would set up a local testing environment on your local computer using a package like MAMP

                                                           

                                                          MAMP & MAMP PRO

                                                          1 person found this helpful
                                                          • 26. Re: Basic HTML form and php
                                                            Nancy OShea Adobe Community Professional & MVP

                                                            You can't test your form-processing script locally because local server's don't have the send mail module. Upload to your remote server to test forms.

                                                             

                                                            To work with PHP locally, install a testing server -- either MAMP or XAMPP for mac.

                                                             

                                                            XAMPP for Mac

                                                            http://www.apachefriends.org/en/xampp-macosx.html

                                                             

                                                            MAMP for Mac

                                                            http://www.mamp.info/en/downloads/index.html

                                                             

                                                            Setting up a PHP environment in Dreamweaver

                                                            http://www.adobe.com/devnet/dreamweaver/articles/setup_php.html

                                                             

                                                            Nancy O.

                                                            1 person found this helpful
                                                            • 27. Re: Basic HTML form and php
                                                              uxk Level 1

                                                              Thanks so much for the clarification.