How to fill a text field using AJAX

English! place to talk about development, programming and coding
Post Reply
juanman80
Znuny newbie
Posts: 44
Joined: 11 Nov 2011, 10:30
Znuny Version: 5.0.15

How to fill a text field using AJAX

Post by juanman80 »

Hi all,

We are still using OTRS 3.0.
I want to fill TicketFreeText5 (a text field) with the phone number of the building selected in TicketFreeText6 (a select with all our possible locations).

In AgentTicketPhone.dtl, I’ve added (after dtl:block:TicketFreeText):

Code: Select all

<!--dtl:js_on_document_complete-->
<script type="text/javascript">//<![CDATA[
    $('#TicketFreeText6').bind('change', function (Event) {
        Core.AJAX.FormUpdate($('#NewPhoneTicket'), 'BuildingAJAXUpdate', 'TicketFreeText6', ['TicketFreeText5']);
    });
//]]></script>
<!--dtl:js_on_document_complete-->
In AgentTicketPhone.pm, I’ve added the Subaction 'BuildingAJAXUpdate'. It succesfully retrieves the phone number of the building, but fails when it has to response the AJAX call. I’ve tried two ways:

- First, I tried using Layout->JSONEncode:

Code: Select all

    $JSONContent = $Self->{LayoutObject}->JSONEncode( 
        Data => {
            TicketFreeText5 => $BuildingData{Phone},
        }
    );
- Second, I tried using Layout->BuildSelectionJSON (it isn’t a select field, but I’ve seen it used in such cases [although can’t remember where]):

Code: Select all

    $JSONContent = $Self->{LayoutObject}->BuildSelectionJSON(
        [
            {
                Name         => 'TicketFreeText5',
                Data         => $BuildingData{Phone},
                SelectedID   => $BuildingData{Phone},
            },
        ]
    );
Either way, the JSON string sent was

Code: Select all

{"TicketFreeText5":"3"}
, being 3 the phone number. So I though everything was ok, but the web browser showed ‘TicketFreeText5’ instead of ‘3’ in the text field.

Anybody can help me to correctly fill a text field using AJAX?

Kind regards,
OTRS 5.0.15 on CentOSLinux with MariaDB database connected to an Active Directory for Agents and Customers.
Post Reply