copy data from field 1 to field 2

Tips submitted by PHPMaker users
Post Reply
qbicdesign

copy data from field 1 to field 2

Post by qbicdesign »

Sometimes when adding a record you need to grab the text you entered into the first field and put it into the second field. Here's the code to do that.
Please note this is for phpmaker v7.1, syntax for v8 is slightly different. Perhapsa an admin can confirm the differences.

Anyway, here goes:

  1. In Server Events/Client Scripts - Table-Specific - Add/Copy Page - Client Script
  2. Add the following javascript code (replacing the field names with your field names):

//script to copy typed value from sort_name to article_name
function copyIt() {
var x=document.getElementById("x_field1_name").value;
document.getElementById("x_field2_name").value=x;
}

  1. In the Row_Rendered event for your table, add the following (again replace the field name with yours):
    //function to copy typed value from field1 to field2
    global $Page;
    if ($Page->PageID == "add") {
    $this->field1_name->EditAttrs["onchange"] = "copyIt()";
    }

IMPORTANT!!!

FOR STEP 2, IF FIELD1 ALREADY USES ANY AJAX FUNCTIONALITY (FOR AUTOFILL LOOKUPS ETC) YOU SHOULD USE THE JAVASCRIPT CODE BELOW INSTEAD

//script to copy typed value from field1 to field2
function copyIt() {
var x=document.getElementById("sv_x_field1_name").value;
if (x==null)
{
var x=document.getElementById("x_field1_name").value;
document.getElementById("x_field2_name").value=x;
}
else
{
document.getElementById("x_field2_name").value=x;
}
}

That's all.
HAVE FUN WITH IT!


PLondono
User
Posts: 7

Post by PLondono »

Thanks for the explanation, very clear.

Now, If the second field is in another table, how can we update the values?? thanks a lot


Post Reply