OnChange Event when Edit Forms

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
alinapia
User
Posts: 12

OnChange Event when Edit Forms

Post by alinapia »

How to create onchange event when user edit on text box, ex: textbox A + textbox B = textbox C. So the value on textbox C is automatically filled when A or B changed. Thank's


mobhar
User
Posts: 11741

Post by mobhar »

Simply put the following code under: "Client Scripts" -> "Table-Specific" -> "Edit Page" -> "Startup Script":

$("#x_A").on('change keyup paste mouseup', function() {
$("#x_C").val($("#x_A").val() + $("#x_B").val());
});

$("#x_B").on('change keyup paste mouseup', function() {
$("#x_C").val($("#x_A").val() + $("#x_B").val());
});


alinapia
User
Posts: 12

Post by alinapia »

Thank you Mobhar, I've tried your code, but nothing happen on textbox C, sorry I'm newbie in web programming. Am i missed something? Do I have to put equal sign after val C?


mobhar
User
Posts: 11741

Post by mobhar »

If the name of your fields are "A", "B", and "C" respectively, then PHPMaker will generate the ID of the textbox become: "x_A", "x_B", and "x_C".

So, make sure you have inspected the ID of textboxes, and they are the same with that above.


alinapia
User
Posts: 12

Post by alinapia »

It's working now, I missed the check mark on Edit Page, previously i put it on the inline edit. Now it's working but the result is not exactly I want, I want the value on textbox C is sum of the A and B. Ex: A=2, B=3 so the value of C=5, but now the value of C is 23. Please how to fix this?


danielc
User
Posts: 1601

Post by danielc »

Use javascript parseInt() function to convert the string to integer before your add:
var a = $("#x_A").val();
a = parseInt(a);
var b = $("#x_B").val();
b = parseInt(b);

Then,
$("#x_C").val(a+ b);


alinapia
User
Posts: 12

Post by alinapia »

it's working as expected now, after change the code from Mobhar :

$("#x_A").on('change keyup paste mouseup', function() {
$("#x_C").val(+$("#x_A").val() + +$("#x_B").val());
});

$("#x_B").on('change keyup paste mouseup', function() {
$("#x_C").val(+$("#x_A").val() + +$("#x_B").val());
});

Thank you.


siriok
User
Posts: 107

Post by siriok »

if field A is a float tye and field B is a integer.
can it calculate due to this code??
i' tried but it don't work


danielc
User
Posts: 1601

Post by danielc »

The code will work. Post your modified code for discussion.


siriok
User
Posts: 107

Post by siriok »

my field "sell" is a auto fill value (float type)
and field "amount" is for user to type in (integer type)
field "sumcost" is a result: (float type)
so my code is

$("#x_amount").on('change keyup paste mouseup', function() {
$("#x_sumcost").val(+$("#x_sell").val() * +$("#x_amount").val());
});

i put in in client script -> table-specific ->add/copy paste -->startup script
so do in the edit page too.
but when i run it don't matter. what wrong with it??


mobhar
User
Posts: 11741

Post by mobhar »

Are you sure the "sell" field has already had the correct value? If you change the multiply (*) sign with plus (+) sign, doesn't it work, too?


Post Reply