0

Hi,
I'm a newbie in php.. having some trouble getting a simple contact form to work, could someone help and explain what the problem and a possible solution to this:

contact.html

<form method="post" id="customForm" action="contact.php">
            <div>
				<label for="name">Name</label>
				<input id="name" name="name" type="text" />
				<span id="nameInfo">What's your name?</span>
			</div>
            <div>
				<label for="organisation">Organisation</label>
				<input id="organisation" name="organisation" type="text" />
				<span id="organisationInfo">Your organisation?</span>
			</div>
			<div>
				<label for="email">E-mail</label>
				<input id="email" name="email" type="text" />
				<span id="emailInfo">Valid E-mail please!</span>
			</div>
            <div>
				<label for="tel">Telephone</label>
				<input id="tel" name="tel" type="text" />
				<span id="telInfo">Contact telephone?</span>
			</div>
            <div>
				<label for="message">Message</label>
				<textarea id="message" name="message" cols="" rows=""></textarea>
			</div>
			<div>
				<input id="send" name="send" type="submit" value="Send" />
			</div>
		</form>

contact.php

<?php
$to = "admin@website.com" ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$organisation = $_REQUEST['organisation'] ;
$tel = $_REQUEST['tel'] ;
$headers = "From: $from";
$subject = "Contact form";
$fields = array();
$fields{"name"} = "name";
$fields{"email"} = "email";
$fields{"organisation"} = "organisation";
$fields{"tel"} = "tel";
$fields{"message"} = "message";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b)
{ 
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}
$headers2 = "From: admin@website.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible. If you have any more questions, please consult our website at www.website.com";
if($from == '')
{
print "You have not entered an email, please go back and try again";
} 
else
{
if($name == '')
{
print "You have not entered a name, please go back and try again";
}
else
{
if($organisation == '')
{
print "You have not entered a organisation, please go back and try again";
}
else
{
if($tel == '')
{
print "You have not entered a telephone, please go back and try again";
}
else
{
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{
print "Your message has been successfully sent!";
}
else
{
print "We encountered an error sending your mail, please notify admin@website.com";
}

}
} 
?>

The form was working until I added the 'telephone' and 'organisation' boxes, have played around with it.. with no luck. Probably easy one for the more experience php users.

Many thanks in advance!

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by dietdew12z
0

Lines 10-14 in your PHP file you're using braces instead of brackets for your array index? I didn't even know you could do that. Are you setting the array name associations there? Shows you how often I use arrays, huh?

Edited by dietdew12z: n/a

0

wow, figured it out.. forgot to close the else statements at the end.. silly me :D

0

I'm not sure which IDE your using, (Dreamweaver, Netbeans, Textmate etc)

But some IDE's such as Netbeans have braces highlighting, this allows you to easily check whether all of your opening braces have a closing brace which can be very helpful for instances like this.

May I also note, when dealing with arrays, use squared brackets like this.

$fields['organisation']

Another convention that many use is indentation, you have a staircase of IF statements, even with an editor and someone new to the script it's still quite hard to undertstand whether an IF statement should be inside another IF statement.

example indentation

<?php
// I prefer having the opening brace on a seperate line to the condition, this is personal preference

$x = 1;
$y = 1;
if($x === 1)
{
    if($y === 1)
    {
        echo 'Hello World';
    }
}
else
{
    echo 'x did not equal 1 or it was equal to 1 however it was not of the same type being an integer';
}

// the above i am sure you and many others would agree is more readable than the following
if($x === 1)
{
if($y === 1)
{
echo 'Hello World';
}
}
else
{
echo 'x did not equal 1 or it was equal to 1 however it was not of the same type being an integer';
}

?>

Edited by benjaminFowl87: n/a

0

A good way to avoid the stairstepped IF statements in this case would be to set a variable as your errormessage at the top of your code and then concatenate them as you go.

...partial...

$errmessage="";
if($name == '')
 {
 $errmessage.="You have not entered a name, please go back and try again<br />";
 }
if($organisation == '')
 {
 $errmessage.="You have not entered a organisation, please go back and try again<br />";
 }
if($tel == '')
 {
 $errmessage.="You have not entered a telephone, please go back and try again<br />";
 }
if ($errmessage!="")
 {
 echo $errmessage;
 }

That way they are all top level IF statements and it's easy to add/remove items.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.