This topic is locked

Default value for text field from SQL Query

12/20/2021 1:33:17 PM
PHPRunner General questions
K
kasni author

I have 6 fields, 5 fields from user input and 1 field default value from SQL query and want to insert to table when user click SAVE. Suppose the default value should be hidden text box, but don't know how to do it with phprunner.

img alt

using lookup wizard I can get default value for checkbox/radio button but user have to click it, so no auto default value. I also tried Javascript on load -> $("input[data-field='sem']").click(); but the checkbox did not auto tick.

img alt

so how to apply this concept to text field?

img alt

I read the manual but I can't figure out how to insert value from SQL query for the default text field.

So I try different approach using the BeforeAdd event,
This is roughly from what i have been trying to do. AND i don't know where to place this code.

~Testing no.1~
$rs = DB::Query("SELECT sem as c FROM semester WHERE CURTIME() BETWEEN mula AND akhir AND student_id= " . $_SESSION["UserID"]);
$data = $rs.fetchAssoc();
$count = $data["c"];

$data = array();
$data["sem"] = $count;
DB::Insert("laporan", $data );

~Testing no.2~
$zip = DB::DBLookup("SELECT sem FROM semester WHERE CURTIME() BETWEEN mula AND akhir AND student_id= " . $_SESSION["UserID"]);

$data = array();
$data["sem"] = $zip;
DB::Insert("laporan", $data );

this SQL query will get value for what semester the student are in for this particular month/year

If some one could guide me to a documentation on step by step to do this would be really appreciated. Thank you.

T
thamestrader 2/2/2022

I think you have almost got to the solution. Stick with the Before Add event, keep the SQL query to obtain the required information and than use it to populate the variable in the $values array. I don't see any need to use DB Insert, just make sure the fields you are defaulting are included on the view being used. I've pasted in code from my applciation that does precisely the same thing as you require.

See code below which I have placed in the Before Record Added Event, this populates fields in the detail record about to be added with default data. Some are fixed values others variable that come from a Master record. The detail fields voucher_type, order_status and agency_ref have to be defined in the table or custom view being used in order to be in the $value array, but they are not displayed on the add page.

$values["voucher_type"] = "Agency";
$values["order_status"] = "RQ";

//Get Agency Ref from Person table
$data = $pageObject->getMasterRecord();
$values["agency_ref"] = $data["agency_ref"];

If you want to prefill fields before the Add page is displayed use the Processrecord values Event for the code.

$values["Agency_id"] = $_SESSION["AG_id"];
$values["collect_deliver"] = "Collection";

In this example I am prefilling with a constant I am also using data held in a session variable that was populated when the user logged on that was obtained from their User record using the After Successful Login event of the Login Page.

$_SESSION["AG_id"] = $data["AG_id"];