This topic is locked
[SOLVED]

 Before record added event

10/24/2013 2:47:24 PM
PHPRunner General questions
N
nti author

In need of "Before record added" example...
Table: tblClients

Field: PhHome
Conditions: if field is left blank, insert: Unknown
Bad code example below:


if values("PhHome")= "" then

values["PhHome"]="Unknown";

end if


Many thanks in advance.

Sergey Kornilov admin 10/24/2013

Try to use PHP syntax, there is plenty of code samples in the manual.

if ($values["PhHome"]== "")

$values["PhHome"]="Unknown";
N
nti author 10/25/2013

Thank you Admin for reply.
Code below works in phpr 6.2 build: 14199
Code below does not work with phpr 7.0 build 18411
Any suggestions? or do I not have latest phpr 7.0 build?



if ($values["PhHome"]== "")

$values["PhHome"] = "Unknown";


Another question: is it best to utilize

end if

with if scripts ?

S
stiven 10/25/2013

you can try this? maybe it works



if($values["PhHome"] == ""){



$values["PhHome"] = "Unknown";
}




In need of "Before record added" example...
Table: tblClients

Field: PhHome
Conditions: if field is left blank, insert: Unknown
Bad code example below:


if values("PhHome")= "" then

values["PhHome"]="Unknown";

end if


Many thanks in advance.

N
nti author 10/25/2013

[quote name='Stivens' date='25 October 2013 - 05:44 PM' timestamp='1382741081' post='72522']

you can try this? maybe it works



if($values["PhHome"] == ""){



$values["PhHome"] = "Unknown";
}


Thanks for suggestion: Does not work in phpr 7.0... I'm sure phpr staff is fixing the problem if it needs to be fixed.

N
nti author 10/26/2013

Events: Before record added

Update: both below php scripts work in Ver 7.0 if Visual Editor FIELD SETTING "Prevent Duplicate values" is not checked.
If we are to: "Prevent Duplicate values" without enforcing "required field", we must have a method to add "data" to the datafield after filling out the form... otherwise "Prevent Duplicate values"

will not function due to "blank duplicate data" or empty field dup.



if ($values["PhHome"]== "")

$values["PhHome"]="Unknown";




if($values["PhHome"] == ""){



$values["PhHome"] = "Unknown";
}
Sergey Kornilov admin 10/26/2013

The only problem I can see is an incorrect syntax in your original code sample. There is no such thing as "end if" in PHP. All suggested code samples are correct.

N
nti author 10/26/2013

Thank you Sergy for reply.
Any thoughts on additional information I provided above ref: "Prevent Duplicate values" switch?

Sergey Kornilov admin 10/26/2013

It just doesn't make any sense to me. If you require user to enter a unique value - you should not allow leave this field empty. We do not have a built-in "Do not allow duplicates but allow empty values" option.
I think you need to implement custom validation. If field is empty - populate it with "Unknown", if not empty - check for uniqueness.
Here is the article that explains how to build your own validation routine:

http://xlinesoft.com/blog/2013/05/21/validation-in-phprunner-and-asprunnerpro-applications

What you looking for is "Server-side validation" section.

N
nti author 10/26/2013

Thank you for reply....



It just doesn't make any sense to me. If you require user to enter a unique value - you should not allow leave this field empty. We do not have a built-in "Do not allow duplicates but allow empty values" option.


Example: Adding New Client to Database: many clients/customers will have 1. "wifes cell phone" 2. "husband cell phone" 3. "land-line home phone" 4. "work phone" 5. "neighbors phone"....
some fields for phone numbers will be left blank if not applicable.
This makes it reasonable / required. "Do not allow duplicates but allow empty values" option.
And now we need a way to fill in the empty fields with "some data" so it does not activate duplicate record warning.

Sergey Kornilov admin 10/26/2013

We will consider adding this is a feature. For now you need to use your own validation per suggested article.

N
nti author 10/26/2013

For novice php users like me....



Here is the article that explains how to build your own validation routine:

http://xlinesoft.com...ro-applications

What you looking for is "Server-side validation" section.


Based on Admin suggestion, found and modified below script which works in Ver 7.0. for checking duplicate records and filling {form field} with word "Unknown" if field is left empty.
Most likely a much better method is avail... but this works for now.



// PhHome
$rs = CustomQuery("select PhHome from tblClients where PhHome = '"

. db_addslashes($values["PhHome"]) . "'");

if (db_fetch_array($rs))

{

$message = "Home Phone ".$values["PhHome"]."

already exists.
Do not add duplicate records.";

return false;

}

$message="";
if ($values["PhHome"]== ""){

$values["PhHome"] = "Unknown";

}

//////////////////////////////////////////////END
// PhCellMs

$rs = CustomQuery("select PhCellMs from tblClients where PhCellMs = '"

. db_addslashes($values["PhCellMs"]) . "'");

if (db_fetch_array($rs))

{

$message = "Mrs Cell Phone ".$values["PhCellMs"]."

already exists.
Do not add duplicate records.";

return false;

}

$message="";
if ($values["PhCellMs"]== ""){

$values["PhCellMs"] = "Unknown";

}

//////////////////////////////////////////////END
// PhCellMr

$rs = CustomQuery("select PhCellMr from tblClients where PhCellMr = '"

. db_addslashes($values["PhCellMr"]) . "'");

if (db_fetch_array($rs))

{

$message = "Mr Cell Phone ".$values["PhCellMr"]."

already exists.
Do not add duplicate records.";

return false;

}

$message="";
if ($values["PhCellMr"]== ""){

$values["PhCellMr"] = "Unknown";

}
return true;

////////////////////////////////////////////// END