This topic is locked

writing fields to mysql db after javascript validation

8/16/2006 2:41:42 PM
PHPRunner General questions
D
drh author

Hi all,
I am having problems getting fields to write to the mysql database after using custom validation using javascript.
I have a text field called field1. When the text field gets focus I call an external javascript program to validate and format the contents of the field while each character is entered. In the _add.php program I inserted this line into the section for field1 entry.
<snip>

$i=array_search("field1",$fieldlist);^M

$value=$defvalues["field1"];^M

// is field editable^M

if(GetEditFormat("field1") != EDIT_FORMAT_HIDDEN)^M

echo "\r\n<tr><td class=shade>".Label("field1")." </td><td>";^M

$addable=0;^M

if(!IsAutoIncrementField("field1"))^M

$addable=1;^M

^M

if($addable)^M

{^M
// commented out the next 2 lines because the javascript creates the text field box and field is not required

// echo BuildEditControl("field1",$value,GetEditFormat("field1"),MODE_ADD);^M

// echo GetLegendIcon("field1");^M

?>

<input name="phone" size="15" value="" style="color:gray;"onkeyup="f20FormatNumber(this,'(~) ~-~~~~','Blur');" >
<?php

}^M

if(GetEditFormat("phone") != EDIT_FORMAT_HIDDEN)^M

echo "</td></tr>";^M
<snip>
The code in red is what I changed. When I type into field1 the javascript is in control, however when I submit the record, field1 does not get written to the db.
Sorry I am such a newbie at javascript. I have been looking for an answer in javascript tutorials, but have not found what I am looking for.
I just need to know how to make the javascript write the contents of field1 to the phprunner generated app. form so it gets written to the db when I click on "SAVE" button.
Thanks for any help or suggestions.
Dave

Admin 8/18/2006

Dave,
you need to modify PHP code generated by PHPRunner. Do not add a new input field, just modify one PHPRunner created.

D
drh author 8/23/2006

Sergey,

thanks for getting back to me. I am not explaining what I wish to accomplish very well. If you could, please browse to this website http://www.vicsjavascripts.org.uk/FormComp...ormatNumber.htm.
This is a site with quite a few javascript examples on it. Scroll down and enter a phone number into the phone field to see how it works. This is what I want to have happen when the phone field has focus and the user begins to type in the data.
I have the part where the user sees the formatting as he/she is typing, but I cannot get the field contents to write to the database (mysql).
Thanks again Sergey.
Dave

D
drh author 8/25/2006

Hi Admin and all,
I keep reading your reply and am not sure what is meant by editing the php code to format the data. It seems that the phprunner generated formating code all happens after the data is entered or saved and not while the data is being entered.
I am thinking I need to add some special type of formating for this in include/_functions.php and pass a custom format type to the BuildEditControl function which will call the javascript program to do the formating as the user types.
I know this can be done, just not exactly sure how. I still do not want to create 3 fields for the phone number. Guess I am just stubborn.
Thanks for anymore help given.
Dave

D
drh author 8/25/2006

Just to add a few more notes. If I can get this to format the phone number as the user enters in the data, just think what other types of fields this would be useful on. Date fields (the slashes would be automajically filled in as the user types in the date), Dollar amts, u.s. social security numbers, and probably a bunch more field types.
I can get the field to format while the user is entering the data. It just will not write it to the database. hmmmm?
Thanks for any and all ideas.
Dave

D
drh author 9/13/2006

Hello all forum members,
I finally got it to work but not without the expert advice I recieved from Sergey. Thanks Sergey. I will try to explain how to make this work. What I wanted was to format the data with dashes and parens as the user was entering so that they didn't have to add the dashes and/or parens. And yet they could see the properly formatted data as they where entering. For instance, I have a US phone number field. When the user enters the field and begins typing, immediately a "(" is displayed and then the digits the user is entering. After the third digit is entered a ")" and a space are displayed. then after the 6th digit is entered, a "-" is displayed. I did the same for a US social security number.
May be a better way to do this, but it worked for me.
I Decided to store the () and dashes in the database. So I increased the field size on the phone #'s from 10 to 14 and ssn from 9 to 11 in the database.
Next I went to http://www.vicsjavascripts.org.uk/FormComp...ormatNumber.htm and downloaded the script. Thanks Vic, great script. I READ the docs at the top of the script so I knew what it was doing and what I needed to change.
I then created a file in the ./include directory call myapp_js.php and copied Vic's script into it.
In ./include/.._functions.php I made the following edits:
Changed the max len setting for the phones to 14 and ssn to 11 in include/.._functions.php under "function GetEditParams"
Added this to the top of function.php

include("include/myapp_js.php");
Edited this section of code in the same file.
FROM
if($format==EDIT_FORMAT_TEXT_FIELD)^M

{^M

if(IsDateFieldType($type))^M

return '<input type="hidden" name="type'.$ifield.'" value="date'.EDIT_DATE_SIMPLE.'">'.GetDateEdit($field,$value,0,$ifield,$edit);^M

return '<input type="text" name="value'.$ifield.'" '.GetEditParams($field).' value="'.htmlspecialchars($value).'">';^M

}^M
TO
if($format==EDIT_FORMAT_TEXT_FIELD)

{

if(IsDateFieldType($type))

return '<input type="hidden" name="type'.$ifield.'" value="date'.EDIT_DATE_S

IMPLE.'">'.GetDateEdit($field,$value,0,$ifield,$edit);
if ($field=="Home_Phone" || $field=="Work_Phone" || $field=="Spouse_Work_Phone")

return '<input type="text" name="value'.$ifield.'" '.GetEditParams($field).' value="

'.htmlspecialchars($value).'";

onkeyup="f20FormatNumber(this,\'(~) ~-~~~~\',\'Blur\');" >';
if ($field=="SSN" || $field=="Spouse_SSN")

return '<input type="text" name="value'.$ifield.'" '.GetEditParams($field).' value="

'.htmlspecialchars($value).'";

onkeyup="f20FormatNumber(this,\'~--~~~~\',\'Blur\');" >';

if ($field=="Spouse_DOB")

return '<input type="text" name="value'.$ifield.'" '.GetEditParams($field).' value="

'.htmlspecialchars($value).'";

onkeyup="f20FormatNumber(this,\'//~~~~\',\'Blur\',[\'mm/dd/yyyy\',,]);" >';
// DRH 8-31-06 The following line must be the last after all the f20FormatNumber Calls

return '<input type="text" name="value'.$ifield.'" '.GetEditParams($field).' value="

'.htmlspecialchars($value).'">';

}
It works perfectly. I haven't got the DATE format to work yet, not sure I am going to. Sergey recommended I not, but I am still slightly interested in it. Time allowing, I might take another stab at it.
I am not the best note taker, but I don't believe I left out any steps. This worked great for me.
Thanks to all that read my post, and special thanks to those who responded.
Dave