Auto Increment Serverside with Intial Value on Edit

Auto Increment Serverside with Intial Value on Edit

vincmeistervincmeister Posts: 136Questions: 36Answers: 4

Hello Allan,

I want to create auto increment number on serverside on field tr_receiving_detail.dom_no
I'm using this code to populate the auto increment ($a) on the top of my php file

$sql = "SELECT dom_no FROM tr_receiving_detail GROUP BY dom_no ORDER BY dom_no DESC LIMIT 1";
    $result = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_assoc($result)) { 
        $a = $row["dom_no"] + 1;
    }

then i want to pass the $a value to field tr_receiving_detail.dom_no only if the value of tr_receiving_detail.dom_no is =0
If the value of tr_receiving_detail.dom_no >0 then $a will ignored.

On the client side,
editor only show tr_receiving_detail.dom_no with disable field. I'm using Edit button to trigger the editor form.

My scenario is
1. get the current value by using $current_dom = $values['tr_receiving_detail']['dom_no'];
2. then check if the value 0 or not
3. If 0 ->setValue to $a
4. If >0 using the $current_dom

Can i use something like this before the Editor::inst , this code is not working for me

$current_dom = $values['tr_receiving_detail']['dom_no'];
    if ($current_dom > 0) {
        $new_dom = Field::inst( 'tr_receiving_detail.dom_no' );
    } else {
        $new_dom = Field::inst( 'tr_receiving_detail.dom_no' )
                    ->setValue($a);
    }

Please help, thank you

Danny

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi Danny,

    then i want to pass the $a value to field tr_receiving_detail.dom_no only if the value of tr_receiving_detail.dom_no is =0

    At what point to you want to pass it? Is this on set, or when getting the data? If on set (which I suspect from the above) use the preEdit server-side event.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Hi Allan,

    on editor's open (edit action), I want to set the init value on form field (client-side) using $a.

    if tr_receiving_detail.dom_no = 0 then value set to $a
    if tr_receiving_detail.dom_no > 0 then value using default value

    at this point, user can see the next auto increment number ($a) before user click update button

    in case user canceled the action, nothing will happen
    in case user click the update button, tr_receiving_detail.dom_no will update to value of $a (this is what i mean with ->setValue )

    Before ask this question, i'm using client-side to pouplate auto increment, but
    1. The auto increment not update after update button clicked. must refresh the page first
    2. I read somewhere on this forum, it's not safe to create auto increment on client-side

    this is my code before

    trDeliveryOrderEditor.on( 'preOpen', function ( e, mode, action ) {
                    //get last dom_no
                    <?php
                        $sql = "SELECT dom_no FROM tr_receiving_detail GROUP BY dom_no ORDER BY dom_no DESC LIMIT 1";
                        $result = mysqli_query($conn, $sql);
                        while($row = mysqli_fetch_assoc($result)) { 
                        ?>
                            var next_dom = <?php echo $row["dom_no"] + 1;
                        }
                        $conn->close();
                    ?> ;
                    if (trDeliveryOrderEditor.val( 'tr_receiving_detail.dom_no') < 1){
                        trDeliveryOrderEditor.val( 'tr_receiving_detail.dom_no',next_dom );
                        trDeliveryOrderEditor.disable( 'tr_receiving_detail.dom_no' );
                        
                    } else {
                        trDeliveryOrderEditor.disable( 'tr_receiving_detail.dom_no' );
                    }
                    
                });
    

    illustration: see the DO's column (tr_receiving_detail.dom_no)

    please advise, thank you

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You would have to make an Ajax call to the server. You can't embed PHP into the Javascript - you need to use $.ajax (or similar) to make an Ajax call to the server, find out the value and then update the fields as required.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Hi Allan,

    Thank you for the clue.
    This is my code and works. Is it the correct way? :)

    trDeliveryOrderEditor.on( 'preOpen', function ( e, mode, action ) {
                    $.get( "function/load-dom.php", function( data ) {
                        var dom_no =  data
                        trDeliveryOrderEditor.disable( 'tr_receiving_detail.dom_no' );
                        if (trDeliveryOrderEditor.val( 'tr_receiving_detail.dom_no') < 1){
                            trDeliveryOrderEditor.disable( 'tr_receiving_detail.dom_no' );
                            trDeliveryOrderEditor.val( 'tr_receiving_detail.dom_no',dom_no )
                        }
                        });
                }); 
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Looks fine to me :-).

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    thanks again Allan, you're the best !

This discussion has been closed.