Sanitize a field before submitting a form?

Any tips and tricks that has to with the ADDT that doesn't fit into one of the other categories
piripacchio
Posts: 37
Joined: 2010-04-28 08:34

Sanitize a field before submitting a form?

Post by piripacchio » 2016-12-05 12:52

Hi all,
I have a form with 2 fields: field 1 is a text field; field 2 is a hidden field.
I need to replicate field 1 on field 2 but applying some sort of cleaning to the field 2.
Example:
inserting "My field is clean àèì" in field 1 I should get ""My-field-is-clean-aei".
I already created a function to clean a string (attechad below) but I cannot find a way to use it along ADDT update/insert record server behaviour.

CLEANSTRING:

Code: Select all

<?php  
function CleanString($string)
{
    $strResult =  str_ireplace("à", "a", $string);
    $strResult =  str_ireplace("á", "a", $strResult);
    $strResult =  str_ireplace("è", "e", $strResult);
    $strResult =  str_ireplace("é", "e", $strResult);
    $strResult =  str_ireplace("ì", "i", $strResult);
    $strResult =  str_ireplace("í", "i", $strResult);
    $strResult =  str_ireplace("ò", "o", $strResult);
    $strResult =  str_ireplace("ó", "o", $strResult);
    $strResult =  str_ireplace("ù", "u", $strResult);
    $strResult =  str_ireplace("ú", "u", $strResult);
    $strResult =  str_ireplace("ç", "c", $strResult);
    $strResult =  str_ireplace("ö", "o", $strResult);
    $strResult =  str_ireplace("û", "u", $strResult);
    $strResult =  str_ireplace("ê", "e", $strResult);
    $strResult =  str_ireplace("ü", "u", $strResult);
    $strResult =  str_ireplace("ë", "e", $strResult);
    $strResult =  str_ireplace("ä", "a", $strResult);
    $strResult =  str_ireplace("'", " ", $strResult);
    $strResult = preg_replace('/[^A-Za-z0-9 ]/', "", $strResult);
    $strResult = trim($strResult);
    $strResult = preg_replace('/[ ]{2,}/', " ", $strResult);
    $strResult = str_replace(" ", "-", $strResult);
    return $strResult;
}
?> 
Can someone help me?

TIA

TOny

User avatar
Fred
Site Admin
Posts: 491
Joined: 2010-02-15 12:10
Location: Armagh, Northern Ireland
Contact:

Re: Sanitize a field before submitting a form?

Post by Fred » 2016-12-07 21:02

TOny look at this http://www.interaktonline.info/article/ ... ating.html
Another way would be to simply grab the $POST variable and change it as required

http://www.interaktonline.info/article/ ... email.html

Here is a bit of actual code I am using to grab a range dynamic checkbox values and append each value to the previous one.
The result is added to a $_POST variable and send to interakt to process.

Code: Select all

if(!empty($_POST['Options'])) {
	$tour_options; // Sets a temp variable
    foreach($_POST['Options'] as $check) {
            echo $tour_options = $tour_options.'<br />'.$check;// Add the first $check to the tem variable, then append the new $check each time
    }
	$_POST['Options'] = $tour_options;// Set the original $_POST[var'] = the temp variable and send for processing.
}

piripacchio
Posts: 37
Joined: 2010-04-28 08:34

Re: Sanitize a field before submitting a form?

Post by piripacchio » 2016-12-12 16:06

Hi Fred,
I'm sure this will work.
thank you very much for your help.
Tony

piripacchio
Posts: 37
Joined: 2010-04-28 08:34

Re: Sanitize a field before submitting a form?

Post by piripacchio » 2016-12-12 17:15

just to let other know the solution:
I simply included the CleaString function before the custom trigger:

Code: Select all

// cleanstring and custom trigger
function CleanString($string)
{
    $strResult =  str_ireplace("à", "a", $string);
    $strResult =  str_ireplace("á", "a", $strResult);
    $strResult =  str_ireplace("è", "e", $strResult);
    $strResult =  str_ireplace("é", "e", $strResult);
    $strResult =  str_ireplace("ì", "i", $strResult);
    $strResult =  str_ireplace("í", "i", $strResult);
    $strResult =  str_ireplace("ò", "o", $strResult);
    $strResult =  str_ireplace("ó", "o", $strResult);
    $strResult =  str_ireplace("ù", "u", $strResult);
    $strResult =  str_ireplace("ú", "u", $strResult);
    $strResult =  str_ireplace("ç", "c", $strResult);
    $strResult =  str_ireplace("ö", "o", $strResult);
    $strResult =  str_ireplace("û", "u", $strResult);
    $strResult =  str_ireplace("ê", "e", $strResult);
    $strResult =  str_ireplace("ü", "u", $strResult);
    $strResult =  str_ireplace("ë", "e", $strResult);
    $strResult =  str_ireplace("ä", "a", $strResult);
    $strResult =  str_ireplace("'", " ", $strResult);
	$strResult =  str_ireplace("_", " ", $strResult);
    $strResult = preg_replace('/[^A-Za-z0-9 ]/', "", $strResult);
    $strResult = trim($strResult);
    $strResult = preg_replace('/[ ]{2,}/', " ", $strResult);
    $strResult = str_replace(" ", "-", $strResult);
    return $strResult;
}

//start Trigger_Custom trigger (BEFORE)
function Trigger_Custom(&$tNG) {
$tNG->setColumnValue("field_2", CleanString($tNG->getColumnValue('field_1')));
}
//end Trigger_Custom trigger
Works like a charm ;).

tony

Post Reply