/    Sign up×
Community /Pin to ProfileBookmark

It works, but I’m sure there is a more efficient way.

… and it probably involves the use of function (which means I need a refresher or two…)

Anyways, its a form that submits to itself, checks that the 4 fields are filled out, then sends an email.

It’s a whole lot of if/not if statements, and the only thing that’s not in there that I would like to to validate the email syntax.

Any advise for a learning programmer?

[code=php]
<?php
header(“Cache-Control: no-cache, must-revalidate”); // HTTP/1.1
header(“Expires: Mon, 1 Jan 2000 00:00:00 GMT”); // Date in the past
?><!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=iso-8859-1″ />
<title>AIC-Wisconsin – Contact Us</title>
<link rel=”stylesheet” type=”text/css” href=”../admin/loginmodule.css”>
</head>

<body>
<h1>Contact Us</h1>
<?php
include_once (‘nav.php’);

if (isset($_POST[‘submit’])){
// form has been submitted

$name = $_POST[‘name’];
$email = $_POST[’email’];
$subject = $_POST[‘subject’];
$messageBody = $_POST[‘messageBody’];

if (!isset($name) || $name == “”){
$message = “Please enter your name.<br />”;
}

if (!isset($email) || $email == “”){
$message = “Please enter an email address.<br />”;
}

if (!isset($subject) || $subject == “”){
$message .= “Please enter a subject.<br />”;
}

if (!isset($messageBody) || $messageBody == “”){
$message .= “Please enter a message.<br />”;
}

if ($message == “”) {
//send mail

$now = date(‘l F jS Y h:i:s A’);
$dateString = “rn rn This emal was generated on $now”;

$to = “[email protected]”;
$from=”AIC-Wisconsin”;

$messageBody =wordwrap($messageBody, 100);
$messageBody .= $dateString;

$headers = “From: $from” . “rn” . “Reply-To: $email” . “rn” .
‘X-Mailer: PHP/’ . phpversion();

// Send Mail
$sendmail = mail($to, $subject, $messageBody, $headers);

if ($sendmail) {
$name = “”;
$email = “”;
$subject = “”;
$messageBody = “”;
$timestamp = “<p>Your email was sent at $now.</p>”;
}
else {
echo mysql_error();
}

}

else {
// dont send mail
$validate = mysql_error();
}

} // end: if (isset($_POST[‘submit’])){
else {
// form not submitted

} // end else

?>
<p style=”color:#C00;”><?php echo $message; ?></p>
<p><?php echo $validate; ?></p>

<form id=”contact” name=”contactForm” method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’] ?>”>
<table width=”400″ border=”0″ cellpadding=”2″ cellspacing=”0″>
<tr>
<td width=”112″><strong>Name</strong></td>
<td width=”188″><input name=”name” type=”text” class=”textfield” id=”name” size=”50″ value=”<?php echo $name; ?>” /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td><input name=”email” type=”text” class=”textfield” id=”email” size=”50″ value=”<?php echo $email; ?>” /><br />
<em>(your email address will be kept private)</em></td>
</tr>
<tr>
<td width=”112″><strong>Subject</strong></td>
<td width=”188″><input name=”subject” type=”text” class=”textfield” id=”subject” size=”50″ value=”<?php echo $subject; ?>” /></td>
</tr>
<tr>
<td valign=”top”><strong>Message</strong></td>
<td><textarea name=”messageBody” type=”text” class=”textfield” id=”message” cols=”47″ rows=”10″><?php echo $messageBody; ?></textarea></td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input type=”submit” name=”submit” value=”Submit” /></td>
</tr>
</table>
</form>
<p><?php echo $timestamp; ?></p>

</div></div>
</body>
</html>

[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Aug 15.2008 — You can validate the email syntax using a regexp (regular expression), but you cannot really remove any if statements. You can remove the empty else, but that's not going to make any difference.
Copy linkTweet thisAlerts:
@OctoberWindauthorAug 15.2008 — Oh, this?

[code=php]
else {
// form not submitted

} // end else
[/code]


Yea, I was building and testing progressivly from a previously working 2-page script, so that was my "catch all" <p>Not Submitted</p> notice.

Apparently $_POST['[B]submit[/B]'] != $_POST['[B]Submit[/B]'] >.>;
Copy linkTweet thisAlerts:
@FrankTheTankAug 15.2008 — 
Apparently $_POST['[B]submit[/B]'] != $_POST['[B]Submit[/B]'] >.>;[/QUOTE]


Nope - the index of an associative array is case sensitive.

Frank
Copy linkTweet thisAlerts:
@NogDogAug 15.2008 — You could do something like the following, then use the values in the $clean array within the rest of your script:
[code=php]
$clean = array(); // holds the clean form data
$errorMessage = ''; // holds any error message(s)
$requiredFields = array(
'name' => 'your name',
'email' => 'an email address',
'subject' => 'a subject',
'message' => 'a message'
);
foreach($requiredFields as $key => $value)
{
if(!isset($_POST[$key]) or trim($_POST[$key] === ''))
{
$errorMessage .= "Please enter $value. ";
}
else
{
$clean[$key] = trim($_POST[$key]);
if(get_magic_quotes_gpc())
{
$clean[$key] = stripslashes($clean[$key]);
}
}
}
[/code]
×

Success!

Help @OctoberWind spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...