Jump to content

Form submit


linardzb

Recommended Posts

Hi,

 

Im trying to submit the email through the online contact page http://www.origin-designs.co.uk/. I cannot receive emails through it, im not sure where is the problem with the code. It seems to be working fine for other people through fiddle or some other tool. I'm providing the code for the form too. any feedback welcome, cheers

 

<?php 
$error = false;
$sent = false;

if(isset($_POST['name'])) {
	if(empty($_Post['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
		$error = true;
	} else {

	$to = "linardsberzins@gmail.com";

	$name = trim($_POST['name']);
	$email = trim($_POST['email']);
	$comments = trim($_POST['comments']);

	$subject = "Contact Form";

	$messages =  "Name: $name \r\n Email: $email \r\n Comments: $comments";
	$headers = "From:" . $name;
	$mailsent = mail($to, $subject, $messages, $headers);

	if($mailsent) {
		$sent = true;
	}
}
}
?>

 

And my HTML is

<?php if($error == true) { ?>
    <p class="error"></p>
    <?php } if($sent == true) { ?>
    <p class="sent"></p>
    <?php } ?>	
    <div id="form">
    	<form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    		<fieldset>
    			<h4>Contact Me!</h4>
    			<label for="name">Name:</label>
    				<input type="text" name="name" id="name"/>
    				<label for="email"/>Email:</label>
    				<input type="text" name="email" id="email"/>
    				<label for="comments" id="comments">Comments:</label>
    				<textarea name="comments" id="" width="90%"></textarea>
    				<fieldset>
    					<input class="btn" type="submit" name="submit" class="submit" value="Send email"/>
    					<input class="btn" type="reset" value="Reset"/>            
     				</fieldset>	
    		</fieldset>
    	</form>

Link to comment
Share on other sites

I strongly recommend the use of a mailer class, like PHPmailer or something similar. Should make it a lot easier to send mail, reliably, without having to read up on a whole lot on the protocols, security measures, and other mail-related issues.

 

PS: I also hope that you're using my PHP_SELF sanitation snippet, to stop would-be attackers from pulling off HTML injection attacks.

Link to comment
Share on other sites

  • 3 weeks later...

Here's what you can use:

 

<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">


<p>Request Phone Call:</p>
Yes:<input type="checkbox" value="Yes" name="call"><br />
No:<input type="checkbox" value="No" name="call"><br />


<p>Website</p> <input type="text" name="website">


<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
<option value="Emergency">Emergency</option>
</select>
<br />


<p>Type</p>
<select name="type" size="1">
<option value="update">Website Update</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">New Products</option>
</select>
<br />


<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

 

 

 

 

And then the PHP...

 

 


<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "youremail@here.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

 

 

 

 

You're welcome.  ;)

Link to comment
Share on other sites

I would not use that code, as it's completely devoid of security. As such it would allow any spammer to hijack your code quite simply, and make it send out mass e-mails to everyone on the planet. While making it look like it's actually you who are doing the spamming.

There are multiple examples on a proper contact form, both here and everywhere else on the internet. I suggest doing some research on this, and learn how to properly secure it. I also recommend a look into PHPmailer, instead of using mail () directly.

 

The HTML code doesn't fare much better, I'm afraid, as it's quite invalid. Both from a semantic and from a standards point of view. If you're using HTML5, then I recommend something like this instead:

<form action="" method="post">
<fieldset>
	<label for="inp_name">Name</label>
	<input id="inp_name" type="text" name="name" required>

	<label for="inp_email">Email</label>
	<input id="inp_email" type="email" name="email" required>

	<label for="inp_phone">Phone</label>
	<input id="inp_phone" type="text" name="phone" required>

	<label>Request Phone Call:</label>
	<label class="inline" for="inp_phone_yes">Yes:</label>
		<input id="inp_phone_yes" type="checkbox" value="yes" name="call">
	<label class="inline" for="inp_phone_no">No:</label>
		<input id="inp_phone_no" type="checkbox" value="no" name="call">

	<label for="inp_website">Website</label>
	<input id="inp_website" type="url" name="website"">

	<label for="inp_priority">Priority</label>
	<select id="inp_priority" name="priority">
		<option value="Low">Low</option>
		<option value="Normal">Normal</option>
		<option value="High">High</option>
		<option value="Emergency">Emergency</option>
	</select>

	<label for="inp_type">Type</label>
	<select id="inp_type" name="type">
		<option value="update">Website Update</option>
		<option value="change">Information Change</option>
		<option value="addition">Information Addition</option>
		<option value="new">New Products</option>
	</select>

	<label for="inp_message">Message</label>
	<textarea id="inp_message" name="message" rows="6" cols="25"></textarea>
</fieldset>

<fieldset class="buttons">
	<input type="submit" name="submit" value="Send">
	<input type="reset" name="reset" value="Clear">
</fieldset>
</form>

 

The benefit of that code is that you can quite easily make it HTML 4 or XHTML compatible, by a few simple tweaks to remove the HTML5-only attributes.

Link to comment
Share on other sites

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.