Skip to main content
Participant
October 11, 2012
Question

Need help with basic PHP for contact form, all the help is appriciated

  • October 11, 2012
  • 1 reply
  • 2420 views

Hi,

I'm new to the IT world so please bear with me. Ive been at this problem for 10 hours and I like finding answes on my own but I admit do failure.

I have this html code for the contact form;

<form class="contact_form" action="kontakt.php" method="post">

            <p><input type="text" required="required" id="contact_name" name="contact_name" class="text_input" value="" size="22"  />

            <label for="contact_name">Namn *</label></p>

           

            <p><input type="text" required="required" id="contact_company" name="contact_company" class="text_input" value="" size="22"  />

            <label for="contact_company">Företag *</label></p>

           

            <p><input type="email" required="required" id="contact_email" name="contact_email" class="text_input" value="" size="22"  />

            <label for="contact_email">Epost *</label></p>

           

            <p><textarea required="required" name="contact_content" class="textarea" cols="30" rows="5"></textarea></p>

           

            <p><button type="submit" class="button white"><span>Skicka</span></button></p>

            <input type="hidden" value="info@webelite.se" name="contact_to"/>

        </form>

Now the problem is that it apears  I suck at PHP, the best I could do was get the form to send emails but they where all blank.

This is all the php I could gather;

<?php

$contact_name = $_POST['contact_name'];

$contact_company = $_POST['contact_company'];

$contact_email = $_POST['contact_email'];

$contact_content = $_POST['contact_content'];

$mail_to = 'info@webelite.se';

$subject = 'Lilla form '.$field_name;

$body_message = 'From: '.$contact_name."\n";

$body_message .= 'E-mail: '.$contact_email."\n";

$body_message .= 'Message: '.$contact_content;

$headers = 'From: '.$contact_email."\r\n";

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

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

  

<?php

}

?>

I really need the PHP ready so please help me....

Thank you

This topic has been closed for replies.

1 reply

Jon Fritz
Community Expert
Community Expert
October 11, 2012

There are a lot of things you need to take into account with PHP forms, security being one of the biggest. If you don't know how to ensure your form is not used by hackers and spammers, you should probably use a premade script until you have a better grasp on PHP.

There are many available online for free, and they usually include a step-by-step tutorial to implement them.

I suggest Formmail at http://www.tectite.com

EDIT: One of the added benefits of using a pre-made script is you can have it up and running in a few minutes and can then take your time learning PHP to create your own script at your own pace.

Abel_1988Author
Participant
October 12, 2012

Thank you for your answer but if I dont do this then I wont understand how it works. I just want to know why its sends blank mails.

I only get;

From:

Subject:

David_Powers
Inspiring
October 12, 2012

Most of your code is OK, but there are two errors:

$subject = 'Lilla form '.$field_name;

The variable $field_name is not defined. This might cause problems with the way the mail is sent.

At the bottom of your code, you have this:

<?php

}

?>

Remove the opening PHP tag and the closing curly brace.

However, as Jon points out, your script is very vulnerable to hackers and spammers. You should never use the input of a form for the subject line or headers of an email without first checking it.

As long as your server is using PHP 5.2 or higher, you can use filter_input() to check the email like this:

$contact_email = filter_input(INPUT_POST, 'contact_email', FILTER_VALIDATE_EMAIL);