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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help please with PHP contact form

    I am editing a "Contact Us" PHP script, I know nothing about PHP so it's just trial and error. Right now after I fill out the form and hit send, it sends to the proper email address but everything included in the form disappears.

    I get this error:
    notice: Undefined index:cf_name in contact.php on line 2

    notice: Undefined index:cf_email in contact.php on line 3

    notice: Undefined index:cf_message in contact.php on line 4
    Here is the PHP script:
    PHP Code:
    <?php
    $field_name 
    $_POST['cf_name'];
    $field_email $_POST['cf_email'];
    $field_message $_POST['cf_message'];

    $mail_to 'WITHHELD';
    $subject 'Message from a site visitor '.$field_name;

    $body_message 'From: '.$field_name."\n";
    $body_message .= 'E-mail: '.$field_email."\n";
    $body_message .= 'Message: '.$field_message;

    $headers 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";

    $mail_status mail($mail_to$subject$body_message$headers);

    if (
    $mail_status) { ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for the message. We will contact you shortly.');
            window.location = 'index-4.html';
        </script>
    <?php
    }
    else { 
    ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to gordon@template-help.com');
            window.location = 'contact_page.html';
        </script>
    <?php
    }
    ?>
    Here is the relevant form code:
    Code:
     <h2>Contact Form</h2>
                       <form action="contact.php" method="post" id="contacts-form" class="contact-form">
                         <fieldset>
                         <div class="rowElem">
                            <label>Name:</label>
                            <input type="text" class="text" name="cf_name">
                         </div>
                         <div class="rowElem">
                            <label>E-mail:</label>
                            <input type="email" class="text" name="cf_email">
                         </div>
                         <div>
                            <label>Message:</label>
                            <textarea cols="1" rows="1" name="cf_message">
                            </textarea>
                         </div>
                         <div class="button"><a href="#" onClick="document.getElementById('contacts-form').submit()">send</a><a href="#" onClick="document.getElementById('contacts-form').reset()">clear</a></div>
                         </fieldset>
                      </form>
    I feel like the fix is really simple, but I am not a coder so I can't find the solution. Hopefully one of you brilliant individuals will be able to. Thanks in advance!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Enclose the above php code inside the following block
    PHP Code:
    if(isset($_POST['cf_name'])){
    ...

    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I will try this and report back ASAP! Thanks for the reply!

  • #4
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK I tried it and the error message disappears, but it no longer sends any email. Also it never redirects back to the contact page. What's wrong? The new code looks like this:
    PHP Code:
    <?php
    if(isset($_POST['cf_name'])){
    $field_name $_POST['cf_name'];
    $field_email $_POST['cf_email'];
    $field_message $_POST['cf_message'];

    $mail_to 'blah@blah.com';
    $subject 'Message from a site visitor '.$field_name;

    $body_message 'From: '.$field_name."\n";
    $body_message .= 'E-mail: '.$field_email."\n";
    $body_message .= 'Message: '.$field_message;

    $headers 'From: '.$field_email."\r\n";
    $headers .= 'Reply-To: '.$field_email."\r\n";

    $mail_status mail($mail_to$subject$body_message$headers);

    if (
    $mail_status) { ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for your interest in Sigma Lambda Beta. We will contact you shortly.');
            window.location = 'index-4.html';
        </script>
    <?php
    }
    else { 
    ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to gordon@template-help.com');
            window.location = 'contact_page.html';
        </script>
    <?php
    }
    }
    ?>
    Thanks
    Last edited by firepages; 04-29-2012 at 04:59 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Just a thought: replace the # in the link href to javascript:void(0);

    Just curious as to why use an anchor to submit the form instead of a submit button??

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,051
    Thanks
    10
    Thanked 94 Times in 92 Posts
    your original code in your first post works fine for me in firefox/apache2/php5
    is there more code involved which we are not seeing there ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Also I would remove your actual email address from your posts unless you want to get a lot spam.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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