Auto Increment Serverside with Intial Value on Edit
Auto Increment Serverside with Intial Value on Edit

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
Hi Danny,
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
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 $aif
tr_receiving_detail.dom_no
> 0 then value using default valueat 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
illustration: see the DO's column (


tr_receiving_detail.dom_no
)please advise, thank you
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
Hi Allan,
Thank you for the clue.
This is my code and works. Is it the correct way?
Looks fine to me :-).
Allan
thanks again Allan, you're the best !