Log in

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



shanedt707
11-14-2016, 01:47 PM
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?

Dealmightyera
11-14-2016, 02:08 PM
www.dynamicdrive.com/forums/showthread.php?39595-Contact-form-using-php

shanedt707
11-14-2016, 02:32 PM
www.dynamicdrive.com/forums/showthread.php?39595-Contact-form-using-php

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?

DyDr
11-14-2016, 03:32 PM
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.

shanedt707
11-14-2016, 03:40 PM
Thanks for the reply.

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



<?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 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?

DyDr
11-14-2016, 09:22 PM
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.