Load value to editor form field using custom button, depedent other field value

Load value to editor form field using custom button, depedent other field value

dd2137dd2137 Posts: 5Questions: 2Answers: 0

I need help creating a button in the editor form that will load a default value into the Y field based on the X field.

I have a php script that will return the correct load.key.php value

                formButtons: [
                    {
                        text: 'Load default key',
                        className: 'btn btn-primary btn-md',
                        action: function () {
                            $ .ajax ({
                            type: 'POST',
                            data: 'values% 5Bmeters.model% 5D = 32',
                            url: 'php / load-key.php',
                            });
                        },
                     },

Pressing the button starts the script, but I have a problem with loading the result into the field in the editor and loading the parameter into the php script, in this case "32"
I also do not know what should be returned json, currently it looks like this:

[{"meters.encryption_key": "00000000000000000000000000000000"}]

the field where I want to load the value is meters.encryption_key
the field from which I want to get the ID for the query is meters.model

Thanks in advance for your help, I just got started with javascript

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,286Questions: 1Answers: 10,425 Site admin

    I also do not know what should be returned json, currently it looks like this:

    It can be whatever you want - it is your Ajax call and you'll need to process it :). I'd suggest probably just an object with the value you want to set. You'll need to add a success callback function that will take the value from the JSON and put it into the Editor form - probably using field().val().

    the field from which I want to get the ID for the query is meters.model

    editor.field('meters.model').val();
    

    the field where I want to load the value is meters.encryption_key

    editor.field('meters.encryption_key').val(json.val);
    

    Allan

  • dd2137dd2137 Posts: 5Questions: 2Answers: 0
    edited August 2022

    Thanks for quick reply! :)

                        { 
                            text: 'Load default key', 
                            className: 'btn btn-primary btn-md',  
                            action: function() {
                                var model = editor.field('meters.model').val();
                                $.ajax({
                                dataType: "json",
                                type: 'POST',
                                data: 'values%5Bmeters.model%5D='+model,
                                url: 'php/load-key.php',
                                success : function (data) {
                                    editor.field('meters.encryption_key').val(data.val);
                                }
                                });
                            },
                         },
    

    already correctly loads the model into the query, but I'm having still trouble loading the value into the field

    The answer in the browser looks ok

    [{"key":"00000000000000000000000000000000"}]
    
  • allanallan Posts: 63,286Questions: 1Answers: 10,425 Site admin
    Answer ✓

    data.val isn't correct for that JSON return. You have it in an array and the property is key, so use:

    data[0].key
    

    Allan

Sign In or Register to comment.