Jump to content
Larry Ullman's Book Forums

Function To Create Form Inputs + Validation


Recommended Posts

Hello everyone,

I'm using a function to create all of my text inputs. The problem is with the final form validation which entails creating and sending the email. When I submit the form, an error message appears "Undefined index: first-name". This is most likely because I'm using, for example, $scrubbed[$label] and not $scrubbed['first-name']. I've also noticed that the confirmation message "Thank you for contacting me. I will reply some day." appears multiple times - once for each form input. Most likely the email is also sent multiple times.

Does anyone know how I should perform the final form validation?

Thank you for any help!

Sources:

CH10 create functions - Script 10.3 - sticky2.php
php  for the web visual quickstart guide 4e

CH13 security methods - Script 13.1 - email.php #2
phy and mysql for dynamic websites visual quickstart4e

<?php

function spam_scrubber($value) {

		// List of very bad values:
		$very_bad = array('to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:', 'multipart-mixed:', 'content-transfer-encoding:');
	
		// If any of the very bad strings are in 
		// the submitted value, return an empty string:
		foreach ($very_bad as $v) {
			if (stripos($value, $v) !== false) return '';
		}
	
		// Replace any newline characters with spaces:
		$value = str_replace(array( "\r", "\n", "%0a", "%0d"), ' ', $value);
	
		// Return the value:
		return trim($value);

	} // End of spam_scrubber() function.

        
        
function make_text_input($name, $label, $errormsg) {
    
if (isset($_POST[$label])) {
$scrubbed = array();
$errors = array();


$scrubbed[$label] = filter_var($_POST[$label], FILTER_SANITIZE_STRING);
if ( ! $scrubbed[$label]) {
$errors[$label] = $errormsg;
}


// Clean the form data:
	$scrubbed = array_map('spam_scrubber', $scrubbed);  

	// Minimal form validation:
	  if ($scrubbed['first-name']) {
	
		// Create the body:
		$body = "Name: {$scrubbed['name']}\n\n";

		// Make it no longer than 70 characters long:
		$body = wordwrap($body, 70);
	
		// Send the email:
		mail('your_email@example.com', 'Contact Form Submission', $body, "From: {$scrubbed['email']}");

		// Print a message:
		echo '<p><em>Thank you for contacting me. I will reply some day.</em></p>';
		
		// Clear $scrubbed (so that the form's not sticky):
		$scrubbed = array();
		$errors = array();
	
	} 



}	
        
print '<label>' . $name . ':</label> ';
if (isset($errors[$label])) echo $errors[$label];
echo'<input id="' . $label . '" name="' . $label . '"  value="';
if (isset($scrubbed[$label])) echo $scrubbed[$label]; 
echo'" />';
	
} 

make_text_input('First Name', 'firstname', 'please enter your first name' );
make_text_input('Last Name', 'lastname', 'please enter your last name');
make_text_input('Email', 'email', 'please provide your email address');


Link to comment
Share on other sites

Hi Larry,

 

thank you for replying to my post. Sorry, you are right about that, it should be 'firstname'. However, I did include my validation code

$scrubbed[$label] = filter_var($_POST[$label], FILTER_SANITIZE_STRING);
if ( ! $scrubbed[$label]) {
$errors[$label] = $errormsg;
}

Not sure if I should do something like this:

// Clean the form data:
    $scrubbed = array_map('spam_scrubber', $scrubbed);

    // Minimal form validation:
     if ($scrubbed[$label] == $scrubbed['firstname']) {

Do you think that it is possible to move the final validation code outside of the "make_text_input" function - the part between // Clean the form data and // Clear $scrubbed (so that the form's not sticky)? Otherwise the confirmation message and sending of email are repeated for each text input.

 

It would be great if this could work!

 

Thank you.

Link to comment
Share on other sites

Sorry I missed that. I would think your validation code *should* be separate from the make_text_input() function, as different fields will require different validations. But you could move the validation outside of that function and still make the function sticky.

Link to comment
Share on other sites

  • 1 month later...

Hi Larry and HartleySan,

 

would one of you know how I can make a variable available outside of the make_text_input function? For example, how would I be able to access the $filtered array outside of the function? The function should validate every text input and then assign said variable to the $filtered array. Then I can do something like this outside of the make_text_input function:

 

if (!empty($filtered['first-name']) && !empty($filtered['last-name'])) {

// create email body and send email

}

 

Thank you in advance.

function make_text_input($name, $label, $errormsg) {
    
if (isset($_POST[$label])) {
$filtered = array();
$errors = array();
$filtered[$label] = filter_var($_POST[$label], FILTER_SANITIZE_STRING);
if (!$filtered[$label]) {
$errors[$label] = $errormsg;
}
}
}
Link to comment
Share on other sites

Hi HartleySan,

 

 

Either return the $filtered array from the function, or create the $filtered array outside the function and pass it by reference to the function.

 

Thank you for your quick response, it appears to be working now. I must say that creating and using functions does trip me up sometimes, I guess I need to practise a lot.

 

Anyway, thanks again and I hope you're having a good week.

 

Cheers.

Link to comment
Share on other sites

 Share

×
×
  • Create New...