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

Thread: PHP form Help.

  1. #1
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP form Help.

    hello there,

    I'm very new new to this so forgive me. I have a very simple form for a mailing list here.

    I need to know exactly what should go in the php file and how to link it with the form. I'm completely new so a step by step type thing would be amazing.

    I'd like the forms contents to be emailed to mailinglist@onlyjoe.co.uk with the subject ADD TO ONLYJOE MAILING LIST and i'd also like the option for a success page.

    Here's the html for the form incase you need it.
    Code:
    <center><form action="php file goes here" method="post">
     <font face="courier new" color="Black"><b>Email:</b></font> <input type="text" name="email" /><input type="submit" name="submit" value="Join Mailing List" />
    </form></center>
    Please help,

    Thanks for your time.

    Andy

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    This is how I'd do it:

    The code below is the file that you specify in the "action='php file goes here'" bit.

    PHP Code:
    <?php

    /* Step 1 - Check to see whether the user has submitted the form */

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

        
    /* Step 2 - Check that the user has input something for their email */
        
        // Using $email so it's easier to write
        // The trim() function takes off any whitespace from the string
        
    $email trim($_POST['email']);
        
        if(!empty(
    $email)) {
            
            
    /* Step 3 - Email isn't empty, send email */
            
            // The email address to send to
            
    $to 'mailinglist@onlyjoe.co.uk';
            
            
    // Message subject
            
    $subj 'ADD TO ONLYJOE MAILING LIST';
            
            
    // Message content
            
    $msg $email ' would like to be added to your mailing list.';
            
            
    $headers "From: Mailing List <mailing@onljoe.com>";
            
            
    /* Step 4 - Send the email */
            
    if(mail($to$subj$msg$headers)) {
                
                
    /* Redirect to success page if mail sent */
                
    header('Location: success.html');
                
            } else {
                
                
    /* Redirect to a different page or do something else if it fails */
                
    header('Location: error.html');
                
            }
            
        }
        
    }

    ?>
    Please not: This does not do any kind of email validation, this is more of an example of the process of sending the email. You should be able to find an email validation function with google, or I can post one if you don't have any luck.

    Hope this helps you out.

  3. #3
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    At the moment, using your exact php code with my error and success urls it keeps directing me to the error page.

    Do you kno why this might be?
    Last edited by andyg113; 02-08-2011 at 10:42 PM. Reason: Incorrect.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Did you change the code at all? That error means there is a typo-- a missing ( or { or something like that. But I don't see one in the original code. What does line 5 look like for you?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Did you change the code at all? That error means there is a typo-- a missing ( or { or something like that. But I don't see one in the original code. What does line 5 look like for you?
    At the moment, i'm using the exact php code with my error and success urls it now keeps directing me to the error page.

    Do you kno why this might be?

    feel free to test here.

    Apologies for the weird edit above by the way.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You cannot view PHP code on a live site. Is there a possibility that you changed something while changing the URLs? Again, I don't see an error in the code above.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    This is the exact code i'm using.

    PHP Code:
    <?php

    /* Step 1 - Check to see whether the user has submitted the form */

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

        
    /* Step 2 - Check that the user has input something for their email */
        
        // Using $email so it's easier to write
        // The trim() function takes off any whitespace from the string
        
    $email trim($_POST['email']);
        
        if(!empty(
    $email)) {
            
            
    /* Step 3 - Email isn't empty, send email */
            
            // The email address to send to
            
    $to 'mailinglist@onlyjoe.co.uk';
            
            
    // Message subject
            
    $subj 'ADD TO ONLYJOE MAILING LIST';
            
            
    // Message content
            
    $msg $email ' would like to be added to your mailing list.';
            
            
    $headers "From: Mailing List <mailinglistonlyjoe@gail.com>";
            
            
    /* Step 4 - Send the email */
            
    if(mail($to$subj$msg$headers)) {
                
                
    /* Redirect to success page if mail sent */
                
    header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Thanks.html');
                
            } else {
                
                
    /* Redirect to a different page or do something else if it fails */
                
    header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Error.html');
                
            }
            
        }
        
    }

    ?>

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That's very strange. I still don't see an error. What happens if you add a space after the ifs?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    That's very strange. I still don't see an error. What happens if you add a space after the ifs?
    I added a space after the ifs heres the code:
    PHP Code:
    <?php

    /* Step 1 - Check to see whether the user has submitted the form */

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

        
    /* Step 2 - Check that the user has input something for their email */
        
        // Using $email so it's easier to write
        // The trim() function takes off any whitespace from the string
        
    $email trim($_POST['email']);
        
        if (!empty(
    $email)) {
            
            
    /* Step 3 - Email isn't empty, send email */
            
            // The email address to send to
            
    $to 'mailinglist@onlyjoe.co.uk';
            
            
    // Message subject
            
    $subj 'ADD TO ONLYJOE MAILING LIST';
            
            
    // Message content
            
    $msg $email ' would like to be added to your mailing list.';
            
            
    $headers "From: Mailing List <mailinglistonlyjoe@gmail.com>";
            
            
    /* Step 4 - Send the email */
            
    if (mail($to$subj$msg$headers)) {
                
                
    /* Redirect to success page if mail sent */
                
    header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Thanks.html');
                
            } else {
                
                
    /* Redirect to a different page or do something else if it fails */
                
    header('Location: http://web.me.com/grimshawa/onlyjoe.co.uk_Coming_Soon/Error.html');
                
            }
            
        }
        
    }

    ?>
    Still no joy.

    (sorry)

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Your website says "an error has occurred" but does not let us know any details.

    Maybe Schmoopy will have an idea about how to fix it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •