Jump to content

basic submit form php coding help pls


minicoopers

Recommended Posts

Hi guys,

 

my first post here so i'll try and keep things short and simple. I'm just in the process of creating my first ever website. I'm very computer and internet savvy but have never created a site till now. Anyway the site itself came together fairly easily but i've come stuck with creating some php code for some contact forms i want on my website. I designed the site in dreamweaver if that makes any difference? and i found some sendit.php code which i've worked from for my form i designed on my website. I've changed it around to suit my needs and thought i'd cracked it. Except whenever i fill in the form on my website and submit it, it says its been sent but i never ever get the email through? I imagine ive missed out or cocked up some very basic part of it but some help would be really greatly appreciated :) Here is my code anyway and if anyone could help me out or give me any pointers that would be fantastic! thanks in advance!

 

My code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<?

    // First we set up some parameters for the receiver

    $receiver_name = 'Daniel Davies';

    $receiver_email = 'sales@bmwminispares.co.uk';

    $mydomain = 'bmwminispares.co.uk';

 

 

    // This is for the sender's name

    if (! $_POST["sender_name"]){

    // The sender didn't give us their name

    $send_from = 'Anonymous';

    }else{

    $send_from = $_POST["sender_name"];

    }

 

    // This is for the sender's email

    if ($_POST["sender_email"] != ''){

    $send_from_email = $_POST["sender_email"];

    }else{

    // The sender didn't give us their email address

    $send_from_email = 'noreply@' . $mydomain;

    }

 

    // This is for the telephone number

    if (! $_POST["sender_telephone"]){

    // The sender didn't provide a telephone number

    $send_telephone = 'Email from ' . $mydomain;

    }else{

    $send_telephone = $_POST["sender_telephone"];

    }

 

    // This is for the message body

    if (! $_POST["message_body"]){

    // The sender didn't put anything in the body

    $send_body = 'NO_BODY';

    }else{

    $send_body = $_POST["message_body"];

    }

 

 

    // This allows us to set the from header, the reply to header, and the x-mailer

    $addl_Headers = "From: " . $send_from_email . "\r\n" . "Reply-To: " . $send_from_email . "\r\n" . "X-Mailer: PHP/" . phpversion();

 

    // Now we need to send the message

    $deliver = mail($receiver_email, $send_telephone, $send_body, $addl_Headers);

 

    // the PHP mail() function return true if the message was processed so we check for this below.

    if ($deliver == TRUE){

    echo "<p>Thank you, your message has been sent. We will get back to you as soon as possible.</p>";

    }else{

    echo "<p>I'm afraid we had a problem sending your request. Please e-mail us at <?=$receiver_email?>.</p>";

    }

    ?>

<body>

</body>

</html>

 

Link to comment
Share on other sites

hi, thanks for the speedy response! the site is now live and has been since late on yesterday evening. Reason i uploaded it is because its mainly a portal for people to get in touch with me, as i started off on ebay only and want to expand etc. I have tried the form on my laptop thats connected to the site online and from the machine here i made the site on also. still not getting anything through to the email thats what made me presume so of the code was wrong.

Link to comment
Share on other sites

It *looks* as though it should work. Have you tried echoing out the variables to make sure they hold the values you'd expect them to hold?

 

As an aside, one problem I see immediately is that every time the page loads, an email will be sent with the default values. That means every time a bot indexes the page, or a visitor goes to it and changes their mind, etc.

Link to comment
Share on other sites

it definately still isnt working which is annoying :( do you just mean editing the length of the fields etc a little bit? i can try and change those about a little and see if that makes any difference. As for the the problem with it sending default values everytime the page loads how do i go abouts changing that so that won't happen? Also i dont like the way once the form is submitted that it takes you to a different page to tell you that its been sent. I would prefer if possible that you just get a message at the bottom of the same page telling you that, do you know how i could do that? many thanks already

Link to comment
Share on other sites

Hi again, i think this is the code for the actual form itself on my website:

 

<p><form name="contact form" method="post" action="/sendit.php">

 

    <p>Name:

    <input name="sender_name" type="text" id="sender_name" />

    </p>

 

    <p>E-Mail:

    <input name="sender_email" type="text" id="sender_email" />

    </p>

 

    <p>Telephone:

    <input name="sender_telephone" type="text" id="sender_telephone" />

    </p>

 

    <p>Enquiry:<br />

    <textarea name="message_body" cols="50" rows="10" id="message_body"></textarea>

    </p>

    <p>

 

    <input type="submit" name="Submit" value="Submit" />

 

    <input type="reset" value="Reset" />

 

    </p>

    </form></label>

</form>

<p> </p>

<p> </p>

<p> </p

Link to comment
Share on other sites

I reworked the script to include basic field validation and error notification, and to make it so simply loading it doesn't trigger a mail send. It also now submits to itself as a self-contained script. I've tested this locally, and it works for me. I'm sure there are things I could have missed, but give it a try. If you have any problems, post them here.

 

 

<?php
if( isset($_POST['submitted']) ) {
// check for header injection attempt by spammers. kill it if found.
function SPAM_SCRUBBER($value) {
	$very_bad = array('to:', 'cc:', 'bcc:', 'content_type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:');
	foreach( $very_bad as $v ) {
		if( stripos($value, $v) !== false ) return '';
	}
	$value = str_replace(array("\r", "\n", "%0a", "%0d"), ' ', $value); // since line feeds are also necessary for header injection, strip them out. wordwrap() will be used instead.
	return trim($value);
}
$scrubbed = array_map('SPAM_SCRUBBER', $_POST);

// First we set up some parameters for the receiver
$receiver_name = 'Daniel Davies';
$to = "sales@bmwminispares.co.uk";
$mydomain = 'bmwminispares.co.uk';
$subject = "Form submission";

$errors = array(); // initialize an array to hold error messages

// This is for the sender's name
if ( empty($scrubbed['sender_name']) ) {
	// The sender didn't give us their name, so store an error in the $errors[] array
	$errors[] = "Name is a required field.";
} else {
	$send_from = $_scrubbed['sender_name'];
}

// This is for the sender's email
if ( empty($scrubbed['sender_email']) ) {
	// The sender didn't give us their email address, so set an error. This does not, however validate the address as valid.
	$errors[] = "Email address is required.";
} else {
	$send_from_email = $scrubbed['sender_email'];
}

// This is for the telephone number
if ( empty($scrubbed['sender_telephone']) ) {
	// The sender didn't provide a telephone number, so set an error.
	$errors[] = "Telephone number is required";
} elseif( !is_numeric($scrubbed['sender_telephone']) ) {
	$errors[] = "Telephone number may only contain numbers.";
} else {
	$send_telephone = $scrubbed['sender__telephone'];
}

// This is for the message body
if( strlen($scrubbed["message_body"]) < 8 ) {
	// The sender didn't put anything *useful* in the body, so set an error.
	$errors[] = "Please enter a valid comment in the \"Enquiry\" field.";
} else {
	$body = $scrubbed['message_body'];
	$body .= "\r\nTelephone number: " . $scrubbed['sender_telephone']; // concatenate the phone number to the body, rather than putting it in the subject line
	$body = wordwrap($body, 70); // wrap the text in the body of the email at 70 characters.
}


// This allows us to set the from header, the reply to header, and the x-mailer
$headers = "From: " . $send_from_email . "\r\n" . "Reply-To: " . $send_from_email . "\r\n" . "X-Mailer: PHP/" . phpversion();

// If there are no errors, it's OK to send the mail
if ( empty($errors) ) {
	if( mail($to, $subject, $body, $headers) ) { // display a success message. Could also be a good place for a header() redirect to a success page . . .
		echo "<p style=\"color:green;\">Thank you, your message has been sent. We will get back to you as soon as possible.</p>";
		$_POST = array(); // ON successful send, clear the POST array so the form is not sticky, and doesn't resend on refresh or browser back button.
	} else {
		echo "<p style=\"color:red;\">I'm afraid we had a problem sending your request. Please e-mail us at <a href=mailto:{$to}>{$to}</a>.</p>";
	}
}
}
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>';

if( !empty($errors) ) {
echo "<p style=\"color:red;\">The following errors occurred:<br /><ul>";
foreach( $errors as $val ) {
	echo "<li style=\"color:red;\">" . $val . "</li>";
}
echo "</ul></p>";
}
?>
<p><form name="contact form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<p>Name:
<input name="sender_name" type="text" id="sender_name" value="<?php if( isset($_POST['sender_name']) ) { echo $_POST['sender_name']; } ?>" />
</p>

<p>E-Mail:
<input name="sender_email" type="text" id="sender_email" value="<?php if( isset($_POST['sender_email']) ) { echo $_POST['sender_email']; } ?>" />
</p>

<p>Telephone:
<input name="sender_telephone" type="text" id="sender_telephone" value="<?php if(isset($_POST['sender_telephone']) ) { echo$_POST['sender_telephone']; } ?>"/>
</p>

<p>Enquiry:<br />
<textarea name="message_body" cols="50" rows="10" id="message_body"><?php if( isset($_POST['message_body']) ) { echo $_POST['message_body']; } ?></textarea>
</p>
<input type="hidden" name="submitted" value="TRUE"> <!-- added hidden field for IE bug workaround - earlier versions don't set the value of the submit button in the $_POST array if form is submitted with the [return] key -->
<p>

<input type="submit" name="Submit" value="Submit" />

<input type="reset" value="Reset" />

</p>
</form>
<p> </p>
<p> </p>
<p> </p

</body>
</html>

Link to comment
Share on other sites

wow!!! firstly thankyou! and this looks and sounds absolutely amazing!

 

noobish question here but does all this new code form an sendit.php file like i had before? or does all this code just go on my contactus.html page? or just part of it. Again i apologize for it being a really stupid question probably but i've only just started using dreamweave this last week. Thanks in advance! i cant wait to get this code in and working! :)

Link to comment
Share on other sites

argh! im getting all muddled up now :(

 

i tried to paste the code into dreamweave but its coming out all wrong and close together and not spaced correct etc. Do you have msn or anything where i could possibly chat to you over? mines: daniel.davies@lineone.net

 

really appreciate all your help so far, just struggling to implement this code now as i don't know where its going and when i put it in all my text fields are already filled in etc argh!  :shrug:

Link to comment
Share on other sites

If you really want to learn how to program properly, with a decent understanding of how everything work and why, dont use dreamweaver - now im not sayign its a bad tool and you should never use it, but for simple projects like these when your learning, you simply do not need the functionality of dreamweaver and it gets quite annoying.

 

I would reccommend a plain text editor, such as Notepad++. You will learn significantly faster if you have to do it all yourself, and you will have less problems.

 

---

Also the code pikachu submitted, would go over your original .php file (php cannot go into .html files unless you have custom mime type sets in the apache .conf).

 

-cb-

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.