Results 1 to 6 of 6

Thread: Desperately need help with PHP for a contact form - I just cannot get this right!

  1. #1
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Desperately need help with PHP for a contact form - I just cannot get this right!

    Hi guys

    I'm a total noob and have spent an incredible amount of time (to no avail) trying to get the contact form I have on my website working.
    I downloaded a website template and understand that I require a send_email.php page but cannot get it working!

    Here is the code for the html part of my website where the contact form resides


    HTML
    …………………………………………………………………………………………………………………………………………………….
    <form action="send_email.php" method="post">
    <fieldset>
    <p><input type="text" value="" placeholder="NAME" class="field"></p>
    <p><input type="email" value="" placeholder="EMAIL" class="field"></p>
    <p><input type="text" value="" placeholder="TITLE" class="field"></p>
    <p><textarea cols="2" rows="2" placeholder="MESSAGE"></textarea></p>
    <p><input type="submit" value="send" class="button"></p>
    </fieldset>
    </form>
    …………………………………………………………………………………………………………………………………………………….

    Can anyone provide me with what I would require for send_email.php to get this working?

  2. #2
    Join Date
    Nov 2016
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

  3. #3
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Dealmightyera View Post
    Hi

    Thank you for the link.
    I honestly have been going over replies like the one posted but don't understand how to change and adapt the code to apply to my html code that in the website template i downloaded.

    Please help if you can?

  4. #4
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    Unfortunately, an eight year old thread in a forum, that didn't explain any of the reasoning for the coding, isn't going to help you understand what is needed. It's also unlikely that the code in that thread will work on most web hosting today, since it is lacking a mail header with a from address in it; following the form example, of echoing $_SERVER['PHP_SELF'], is insecure against cross site scripting; and putting the submitted data into the message body, without any html entity conversion, will allow someone to try and take over the receiving email client/browser.

    If all you are trying to do is add a contact form to a site and you are not an experienced php programmer, you would need to look for a php 'form to email' OOP class. You should be able to find some that will let you define a list of form fields and their type, and some email configuration values, and it will produce the form and handle processing the form data. Edit: searching for 'php formmail generator' will find these type of scripts.

    If on the other hand, you are doing this as a learning exercise, posting questions in a php programming forum can get you help.

    To get you started, form fields need name='...' attributes. Only successful form fields that have names will be included in the submitted form data.

    If you need a list of tasks the form processing code needs to do, just ask and someone will post something using current best practices.
    Last edited by DyDr; 11-14-2016 at 03:41 PM.

  5. The Following User Says Thank You to DyDr For This Useful Post:

    shanedt707 (11-14-2016)

  6. #5
    Join Date
    Nov 2016
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reply.

    So i managed to get a hold of the following code for a send_email.php file:

    Code:
    <?php
    $field_name = $_POST['cf_name'];
    $field_email = $_POST['cf_email'];
    $field_message = $_POST['cf_message'];
    
    $mail_to = 'sam@example.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 the message. We will contact you shortly.');
    		window.location = 'contact_page.html';
    	</script>
    <?php
    }
    else { ?>
    	<script language="javascript" type="text/javascript">
    		alert('Message failed. Please, send an email to sam@example.com');
    		window.location = 'contact_page.html';
    	</script>
    <?php
    }
    ?>
    Now, when I fill out the form on my website I get a reply sent to my email but it looks like this Click image for larger version. 

Name:	screenshot.jpg 
Views:	77 
Size:	11.4 KB 
ID:	5973 - Anyway I can modify this php code so that I can get the filled out fields as described in the html further above to display instead of these blank fields?
    Last edited by jscheuer1; 11-14-2016 at 06:50 PM. Reason: format code

  7. #6
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    It would appear you are going this route -
    If on the other hand, you are doing this as a learning exercise, posting questions in a php programming forum can get you help.
    If so, for the first step of -
    To get you started, form fields need name='...' attributes. Only successful form fields that have names will be included in the submitted form data.
    The names you use in the form field name attributes must match what the $_POST['...'] variables are using.

Similar Threads

  1. php contact form with no thank you
    By nate51 in forum PHP
    Replies: 3
    Last Post: 08-16-2011, 02:18 PM
  2. CSS Contact form-help need
    By bogdancornel in forum CSS
    Replies: 0
    Last Post: 06-22-2010, 03:17 PM
  3. Contact form using php
    By biomike in forum PHP
    Replies: 13
    Last Post: 12-26-2008, 03:25 AM
  4. Replies: 2
    Last Post: 03-14-2007, 09:02 AM
  5. Replies: 16
    Last Post: 02-20-2007, 05:09 PM

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
  •