This topic is locked
[SOLVED]

 Insert record into another table with button

11/22/2020 6:36:14 AM
PHPRunner General questions
A
alfonso authorDevClub member

I have used the Events->Add Page-> Before record added-> insert a record into another talbe. All is ok. The record is saved in TABLE A and inserted in TABLE B



$data = array();

$data["table_b_id"] = $values["table_a_id"];

$data["table_b_name"] = $values["table_a_name"];

DB::Insert("table_b", $data );


I wanted to know if it is possible not to insert the record in that way but by placing a button in the list in TABLE A. When I click on the button, what I want is for that record to be inserted in TABLE B

It's possible?

Thank you

A
acpan 11/22/2020

Yes, you can do that.
Button's Before Client event:

// skip to server event

return true;
Then Server Event:
$record = $button->getCurrentRecord();
And use DB API to insert into table B.
$data = array();

$data["table_b_id"] = $record["table_a_id"];

$data["table_b_name"] = $record["table_a_name"];

DB::Insert("table_b", $data );
Hope it helps.
ACP

A
alfonso authorDevClub member 11/22/2020

Thanks. It's ok
First I have created a button in list and put the code: https://monosnap.com/file/I3dkLiohAORTFpeVdawhU5A6ievjl4

Later I go to Events button and into "Server" i put code: https://monosnap.com/file/s1pgXws76ZS2UMvsVVKfkymkOeR2xJ

Then all is ok. But only one small question. At the bottom of the button appress "Undefined !!!": https://monosnap.com/file/eXtywqZAX9LQLYjMTEKZzafmhqEnhT
How can I disable that message and get a confirmation popup or similar?

Admin 11/22/2020

Make sure that you do not have any code in ClientAfter event.

A
alfonso authorDevClub member 11/23/2020

Perfect!