Variable on ADD

Variable on ADD

nskwortsownskwortsow Posts: 120Questions: 0Answers: 0
edited November 2012 in Editor
Hi,
When adding a record I need to insert the OwnerID (parent table). Should I use a HIDDEN field for this? If so: how do I insert the current session['OwnerID'] in the javascript (I use external .js libraries and don't like to mix PHP and JS).
Seems cumbersome to populate with a $('#OwnerID').val(); but if I have to, I will. Please point me in the right direction.
/N

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Sounds like a hidden field with the value of the owner id is what is needed? You can use the `set` method to set the required value on the item in the form. Then on the server intercept the submitted data and modify the database as required.

    Allan
  • nskwortsownskwortsow Posts: 120Questions: 0Answers: 0
    Hi Allan,

    Thanks, but please be more specific when it comes to the server side processing. I don't want to write my own code in parallel to your API if I can help it (gets messy).

    I can set the value, no problem, but when I send it for CREATING, what does my PHP look like?
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    edited November 2012
    Assuming I understand the problem correct, what you could do is something like this:

    [code]
    $editor = Editor::inst( $db, 'browsers' )
    ->fields(
    Field::inst( 'name' )->validator( 'Validate::required' ),
    Field::inst( 'dept' )->validator( 'Validate::required' )
    );

    if ( isset($_POST['action']) && $_POST['action'] === 'create' ) {
    // Adding a new record, so add a field that will be written
    $editor->field( Field::inst('owner') );
    $_POST['data']['owner'] = $_SESSION['id'];
    }
    else {
    // Otherwise editing, deleting or getting data. Just read the owner field
    $editor->field( Field::inst('owner')->set(false) );
    }

    $editor
    ->process( $_POST )
    ->json();
    [/code]

    The key thing here is that the fields for the Editor instance don't need to be static - they can be dynamically built - as is the case above.

    Allan
  • nskwortsownskwortsow Posts: 120Questions: 0Answers: 0
    Perfect, thanks.
This discussion has been closed.