This topic is locked
[SOLVED]

 Default value from database

8/7/2013 12:42:45 PM
PHPRunner General questions
S
Stucco author

Hi,
I need to set a checkbox value default based on a database query in combination with the selection of another field. I wrote this by pre-populating a JS array on the add page, with all possible defaults, and it works great. The default value is set immediately upon selection of the second field. Unfortunately, the Inline Add fails to work at this point.
The code added to the Add Page -> Before Display event is

echo "<script language=\"JavaScript\">";

echo "var event_violations = new Array();";

$sql = "SELECT id, violation FROM options_event WHERE visible = 1;";

$rs=CustomQuery($sql);

while ($data = db_fetch_array($rs)){

echo "event_violations[$data[id]] = $data[violation];";

}

echo "</script>";


Then on the Add Page -> Javascript Onload Event I added:

document.getElementById("value_event_1").addEventListener(

'change',

function() {document.getElementById("value_violation_1").checked=event_violations[this.value];},

false

);


The Inline Add throws no JS errors, but appears completely blank. Here is a pic of what it looks like after a few adds.

Sergey Kornilov admin 8/7/2013

The proper way to work with default values is as follows:

  1. Set default value to some session variable like $_SESSION["violation"] via 'Edit as' settings dialog
  2. Move your PHP code to Add page: BeforeProcess event and populate those session variables there
    Sample code:

$sql = "SELECT id, violation FROM options_event WHERE visible = 1";

$rs=CustomQuery($sql);

while ($data = db_fetch_array($rs)){

$_SESSION["event_violations".$data[id]] = $data[violation];

}
S
Stucco author 8/8/2013

Hi,
I don't think the requirement is possible to meet with this method. The default for the checkbox, call it Field B, is related to the selection of another field on the form, Field A.
When Field A is chosen, Field B will change, based on the value of Field A. It is much like the dependent Lookup fields functionality, but for a default value.
Here is a video of the functionality. Violation default depends on the value of Event.
Video

Sergey Kornilov admin 8/8/2013

I see, this doesn't have anything to do with "default value".
Try 'Autofill' option, designed specifically for this purpose.
The approach you implemented already should work as well, hard to tell what exactly is wrong.

S
Stucco author 8/26/2013

Hi, I have tried reconfiguring it to use autofill (did not know about this feature!), but it is failing when trying to write to a checkbox. The value for the checkbox is 0 or 1, and is set by another checkbox elsewhere in the system. I replace the checkbox with a text box and it works well.
I am using PHPRunner 5.3.
Thank you