Jump to content

Recommended Posts

Hello im new to PHP and i have problems getting PHP working for my websites contac form

 

Here the HTML form code

<form action="" method="post">
          <div class="form_settings">
            <p><span>Name</span><input class="contact" type="text" name="your_name" value="" /></p>
            <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="" /></p>
            <p><span>Message</span><textarea class="contact textarea" rows="8" cols="50" name="your_enquiry"></textarea></p>
            <p style="padding-top: 15px"><span> </span><input class="submit" type="submit" name="contact_submitted" value="submit" /></p>
          </div>
        </form>

Heres the PHP code

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'example@gmail.com';
$subject = 'Message from '.$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('Sent');
		window.location = 'kontakt.html';
	</script>
<?php
}
else { ?>
	<script language="javascript" type="text/javascript">
		alert('Error');
		window.location = 'kontakt.html';
	</script>
<?php
}
?>

I would be really thankful if someone could fix the PHP code for me and make it work with the html above.

Link to comment
https://forums.phpfreaks.com/topic/301064-email-contact-form-php-help/
Share on other sites

The name used in your input fields

<input class="contact" type="text" name="your_name" value="" />

...needs to match the text used in your $_POST variables.

$field_name = $_POST['cf_name'];

For the above field, try the following:

$field_name = $_POST['your_name'];

This is a help forum, not a we'll-guess-what-your-problem-is-and-then-fix-the-code-for-you-forum.

 

If you have a problem, you need to actually tell us what that is. You're also expected to actively work on the issue and not just hand over the code. What have you tried? What exactly do you not understand?

 

What I can tell you is that you need to start thinking about security. You can't just take user input from random people and insert it straight into the e-mail headers and the message, because this effectively turns your server into an open mail relay where anybody can send e-mails to arbitrary addresses. Use a proper mailer library like PHPMailer instead of the terrible mail() function.

Thx cyberRobot, i finaly got it working, slightly different code. Its very basic but works for me since im new to PHP

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $telefon = $_POST['telefon'];
    $liik = $_POST['liik'];
    $message = $_POST['message'];
	
    $email_message = "
	Nimi: ".$name."
	E-post: ".$email."
	Telefon: ".$telefon."
	Töö Liik: ".$liik."
	Töö kirjeldus: ".$message."
	";
	
	mail ("@outlook.com" , "Hinnapäring", $email_message);
	if (mail) { ?>
	<script language="javascript" type="text/javascript">
		alert('Saadetud');
		window.location = 'hinnaparing.html';
	</script>
<?php
}
else { ?>
	<script language="javascript" type="text/javascript">
		alert('Saatmisel esines viga');
		window.location = 'hinnaparing.html';
	</script>
<?php
}
?>

Im curious if its possible to change the sender to email typed in the form. Right now the web host sends me the message. I could Reply faster if it would refer the email as sender. If its possible can someone give me a head start what should i do?

Im curious if its possible to change the sender to email typed in the form. Right now the web host sends me the message.

 

You would use the additional_headers argument for mail(). More information can be found here:

http://php.net/manual/en/function.mail.php

 

Note that your original code utilized the argument.

 

With that said, you'll want to work on the security aspect of the code, as suggested by Jacques1. Adding a user-supplied address in the email header, for example, opens your script to email injection attacks. More information can be found here:

http://securephpwiki.com/index.php/Email_Injection

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.