How to add a value in front of a field?

How to add a value in front of a field?

webpointzwebpointz Posts: 126Questions: 30Answers: 4

I have an editor table and when I add a new product, I want to be able to ensure that the customer code is added at the beginning of any product id.

Right now when I add a new product, I have a product id field. I want to prefix whatever is typed in this field by the user with the PHP $_SESSION['customercode'] (Example: MJK-).

I can pre-populate the field with this code as the "def:" value but the user could easily delete the text.

Is there a way to prevent the text default value from being deleted or is there some way to pre-prend the text to the field using preSubmit?

Any help would be appreciated.

This question has an accepted answers - jump to answer

Answers

  • webpointzwebpointz Posts: 126Questions: 30Answers: 4

    So I came up with a solution.

    First, in the PHP page, I call the JS file using the following syntax (I add the ?version=1 so that the JS file won't be cached changing the number before each test) :

    <script id="ccode" data-name="<?php echo $_SESSION['ccode']; ?>" src="js/table.table_products.js?version=1"></script>
    

    At the top of the JS file, I have the following code to retrieve the SESSION variable:

    var ccode = document.getElementById("ccode").getAttribute("data-name");
    

    Then I add the following after the editor code:

        editor.on( 'preSubmit', function ( e, data, action ) {
            // concatenate the customer code in front of the product id
            data.data['table_products']['productid'] = ccode+data.data['table_products']['productid'];
            alert('The Product ID named ['+data.data['table_products']['productid']+'] will be added to the system.');
        } );
    
  • webpointzwebpointz Posts: 126Questions: 30Answers: 4

    Just an update, I added the following wrapper inside the preSubmit so it only performs the concatenation on create and not edit or remove:

    if(action == 'create') {
      data.data['table_products']['productid'] = ccode+data.data['table_products']['productid'];
                alert('The Product ID named [ '+data.data['table_products']['productid']+' ] will be added to IMS.');
    }
    
  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    Yep, that's a good way to go.

    Colin

This discussion has been closed.