Menu
The JavaScript tutorial I was reading acted like all you had to do to validate your form was to alert the users of an error when they clicked the submit button. Is that all there is to it? Don’t you need some way to cancel the post method in the form?
<i>
</i><script language="JavaScript">
function checkFieldsProfileUpdate() {
missinginfo = "";
if (document.companyUpdate.company.value == "") {
missinginfo += "n - 'Company Name' was left blank! n";
}
if (document.companyUpdate.email.value == "") {
missinginfo += "n - 'Email' Must be filled in!";
}
if (missinginfo != "") {
missinginfo ="You did not fill in all required fields: n___________________________________________________n" +
missinginfo + "n___________________________________________________" +
"n n Please re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
</script>
<form name="companyUpdate" action="" method="post" enctype="multipart/form-data" onSubmit="return checkFieldsProfileUpdate();">
company
<label>
<input type="text" name="company" id="company">
</label>
<br>
email
<label>
<input type="text" name="email" id="email">
</label><br>
<input name="Submit" value="Submit" type="submit">
</form>
[CODE]function formIsEmpty {
// assign the form fields to variables
var title = document.getElementById('title');
var url = document.getElementById('url');
var button = document.getElementById('button');
var email = document.getElementById('email');
// if the any of the fields are empty, alert the user
if(title == " ") || (url == " ") || (button == " ") || (email == " ") {
alert('All fields are required!');
}
}[/CODE]
[code=html]<form id="AffiliateForm" method="post" action=" " onSubmit="formIsEmpty();">
<input name="title" id="title" type="text" class="formbox">
<input name="url" id="url" type="text" class="formbox">
<input name="button" id="button" type="text" class="formbox">
<input name="email" id="email" type="text" class="formbox">
<input name="send" id="send" type="submit" value="Send Owl">[/code]
if(title == " ") || (url == " ") || (button == " ") || (email == " ")
if(title == "") || (url == "") || (button == "") || (email == "")
if(title == NULL) || (url == NULL) || (button == NULL) || (email == NULL)
if(empty($_POST['title']) || empty($_POST['url']) || empty($_POST['button']) || empty($_POST['email']))
$title=$_POST['title']
if(empty($title) || empty($url) || empty($button) || empty($email))
function formIsEmpty {
// assign the form fields to variables
var title = document.getElementById('title');
var url = document.getElementById('url');
var button = document.getElementById('button');
var email = document.getElementById('email');
// if the any of the fields are empty, alert the user
if(title == " ") || (url == " ") || (button == " ") || (email == " ") {
alert('All fields are required!');
return false; <br/>
} else {
return true
}
}
if (formisempty()) { // process data
... }
<?php
if formisempty() {
//do the processing
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style type="text/css">
</style>
<script type="text/javascript">
function formIsEmpty(f){
var r = true,e = document.getElementById("error");
removeChildren(e);
if (f.title.value==="") {
e.appendChild(createP("Title Field is empty."));
r=false;
}
if (f.url.value==="") {
e.appendChild(createP("URL Field is empty."));
r=false;
}
if (f.button.value==="") {
e.appendChild(createP("Button Field is empty."));
r=false;
}
if (f.email.value==="") {
e.appendChild(createP("Email Field is empty."));
r=false;
}
if (!/^w+(.w+)?@w+(.w+)?.w{2,3}$/.test(f.email.value)) {
e.appendChild(createP("Email is in an invalid form."));
r=false;
}
return r;
}
function createP(m) {
var t = document.createElement("p");
t.appendChild(document.createTextNode(m));
return t;
}
function removeChildren(e) {
while (e.hasChildNodes()) {
e.removeChild(e.firstChild);
}
}
</script>
</head>
<body>
<form name="AffiliateForm" method="post" action="" onSubmit="return formIsEmpty(this);">
Title: <input name="title" type="text" class="formbox"><br>
URL: <input name="url" type="text" class="formbox"><br>
Button: <input name="button" type="text" class="formbox"><br>
Email: <input name="email" type="text" class="formbox"><br>
<input name="send" type="submit" value="Send Owl">
<div id="error">
</div>
</form>
</body>
</html>
[code=php]<?php // process the form
if (array_key_exists('send', $_POST) and !empty($_POST['send'])) {
$to = '[email protected]' ;
$subject = 'Rebirth Affiliation Request' ;
// list required and expected fields
$expected = array('title', 'url', 'button', 'email');
// set the required fields
$required = array('title', 'url', 'button', 'email');
// create an empty array for missing fields
$missing = array();
// assume there is nothing to suspect
$suspect = false;
// create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';
// process post variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
// go ahead only if all required fields OK
if (empty($missing)) {
$title = $_POST['title'];
$url = $_POST['url'];
$button = $_POST['button'];
$email = $_POST['email'];
// time to build the message!
$message = "Title: $titlenn";
$message .= "URL: $urlnn";
$message .= "Button: $buttonnn";
$message .= "Email: $emailnn";
/* Rebirth has fallen victim to
viagra and cialis spammers. As
to why, I do not know. This next
bit of code is used in order to
keep them from annoying the crap
out of me. */
$lowerCase = strtolower($message);
if (strpos($lowerCase, "viagra") || strpos($lowerCase, "cialis")) {
// *grins* let's send them to a special page
header("Location: http://www.uhrebirth.com/not_so_fast/male_meds.html");
exit;
}
else {
// time to send the message
$mailSent = mail($to, $subject, $message);
}
}
if ($mailSent) {
// missing is no longer needed
unset($missing);
}
}
?>[/code]
[CODE]<!--
function formIsEmpty() {
// if the any of the fields are empty, alert the user
if(AffiliateForm.title.value.length == 0) {
alert('All fields are required!');
return true;
}
return false;
}
//-->
/* NOTE: The reason I only have it checking for one field
is because so far I've just been testing the script. Once I
get everything working like it's supposed to, I'll add the
other fields in. */[/CODE]
0.1.9 — BETA 5.17