Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Basic Form PHP

  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Location
    Somerset,UK
    Posts
    138
    Thanks
    10
    Thanked 1 Time in 1 Post

    Basic Form PHP

    Hi all.

    I have just written my first bit of PHP code for a basic contact form on a site that I am building. I basically follow a tutorial off the interweb.

    Does the code (HTML and PHP) look right or have I made any mistakes?

    PHP
    Code:
    <?php
      $name = $_REQUEST['name'] ;
      $phone = $_REQUEST['phone'] ;
      $email = $_REQUEST['email'] ;
      $company = $_REQUEST['company'] ;
      $query = $_REQUEST['query'] ;
    
      mail( "info@noiz-electronics.co.uk", "Contact request",
        $message, "From: $email" );
      header( "Location: http://www.noiz-electronics.com/thankyou.html" );
    ?>
    HTML
    Code:
     	<div id="formwrapper">
                
                <form method="post" action="sendmail.php">
    
                    <label>Name</label>
                    <input name="name" type="text" size="60" />
                    
                  <label>Telephone</label>
                    <input name="phone" type="text" size="60" />
                    
                    <label>Email</label>
                    <input name="email" type="text" size="60" />
                    
                    <label>Company Name</label>
                    <input name="company" type="text" size="60" />
                    
                    <label>Company Type</label>
                    <input name="type" type="text" size="60" />
                    
                    <label>Your Query</label>
                    <textarea name="query" cols="60" rows="6"></textarea>
                    <p/>
                    
                    <input name="Submit" type="button" class="btn" value="Submit" />
    
    
          		</form>
              </div>
    Now from what I can tell from my research I just upload the sendmail.php file to the server at the same level as the contact.html page and that should work right? As long as the server allows php?

    Sorry I know these are probably silly questions but this is my first attempt at PHP and I really want to get it working for this site.

    Thanks in advance to you lovely PHP gods!
    Hmmmm......scratchy head time......

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,366
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Your call to mail() will return a boolean result (true / false) that indicates whether the mail program accepted it.

    You should test for that by doing if (mail()) {//something} like this:

    PHP Code:
    if (mail($to$Subject$ETC))
       {
       
    //Do something
       
    }
    else
       {
       
    //Do something else
       

    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Regular Coder
    Join Date
    Apr 2010
    Location
    Somerset,UK
    Posts
    138
    Thanks
    10
    Thanked 1 Time in 1 Post
    Thank you for your reply tangoforce.

    Would it be possible for you to elaborate on what your saying? or point me in direction to find out. I'm not to sure what you mean.

    I have seen other php snippets with the if/else in them but I wasnt sure what they ment.

    EDIT
    Ok so I have come up with this. Now I'm guessing this will work as if no email or query is given the form will just reset and if the email is there it will submit. is that right?

    PHP Code:
    <?php
      $name 
    $_REQUEST['name'] ;
      
    $phone $_REQUEST['phone'] ;
      
    $email $_REQUEST['email'] ;
      
    $company $_REQUEST['company'] ;
      
    $query $_REQUEST['query'] ;
      
      
      if (empty(
    $email) || empty($query)) {
        
    header"Location: http://www.noiz-electronics.com/contact.html" );
      }
      else {

      
    mail"info@noiz-electronics.co.uk""Contact request",
        
    $message"From: $email" );
      
    header"Location: http://www.noiz-electronics.com/thankyou.html" );
      }
    ?>
    Thanks again
    Last edited by claire_t; 08-13-2012 at 03:53 PM.
    Hmmmm......scratchy head time......

  • #4
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post
    I can see a few issues here, but overall it's a good start.
    Your if/else syntax looks alright to me, but if the email or query is missing and it redirects, it won't tell the user why it redirected.
    You could make it tell the user why by making contact.html into contact.php, just change the extension - the HTML can stay, and then send a variable to it like this :
    PHP Code:
    header("Location: http://www.noiz-electronics.com/contact.php?noemail=1"); 
    Naturally you would have to put the corresponding code into the contact.php e.g.
    Code:
    	<div id="formwrapper">
                <?php if( @$_GET['noemail'] == 1 ){ echo "<b>No email entered</b>"; } ?>
                <form method="post" action="sendmail.php">
    The php tags are important here, and the @ symbol will suppress the error that would show if the value was empty. GET values are the ones sent in the URL bar hence using GET instead of REQUEST (on that note you could use POST quite safely in sendmail).

    Next you take in a total of 5 variables (name, phone, email, company and query) but only make use of 1 of them - namely email. Then you reference another variable, message, which doesn't exist at this point. My suggestion to make the message variable would be the following :
    PHP Code:
    else {
      
    mail"info@noiz-electronics.co.uk""Contact request"$message"From: $email" );
      
    $message "Name: $name\n";
      
    $message .= "Phone: $phone\n";
      
    $message .= "Company: $company\n";
      
    $message .= "Query:\n$query"
    To elaborate a little but on the things used in that snippet...
    The .= sign indicates add to the end of the variable. The \n indicates a newline, as this is email it will be sent in plaintext so \n is used rather than <br>, if you set it up to use HTML email then <br> would work too, but that's a whole other can of worms.

    Another suggestion would be to use if/else constructs to only add name, phone and company if they actually exist as they aren't required.

    And finally, in the form where you have all the <label> tags, make them into <label for="NAMEHERE"> where you replace NAMEHERE with the name of the <input> tag they belong to. E.g:
    Code:
    <label>Telephone</label>
    <input name="phone" type="text" size="60" />
    would become
    Code:
    <label for="phone">Telephone</label>
    <input name="phone" type="text" size="60" />
    I hope this makes sense, good luck!


  •  

    Posting Permissions

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