Fork me on GitHub

Watch

GitHub Core

Show your support for Zikula! Sign up at Github account and watch the Core project!




GitHub Modules

Forum Activity

Forum feed

» Visit forum | » View latest posts

syntax for multi checkbox selections in form  Bottom

  • I having problems figuring out how to pass multiple checkbox selections to an API function that will do an insert to the DB. I can structure it where it the results of one checkbox is inserted into the DB.

    I found these previous threads useful, but it doesn't quite solve my problem:
    http://forums.postnu…ht=form+multiselect
    and:
    http://forums.postnu…e=viewtopic&t=14531

    I'm dealing with 3 functions.

    1. name_user_view - which contains the form.
    2. name_user_add - which gets accepts the data from the form and passes it to the API function
    3. name_userapi_add - which inserts the checkbox data into the form.

    function 1 contains the following relevant pieces:

    Code

    $output->FormStart(pnModURL('Salesrank', 'user', 'watchlistadd'));
       
    $output->FormHidden('authid', pnSecGenAuthKey());
    $output->FormHidden('userid', pnUserGetVar('uid'));
    ..........
    $output->SetOutputMode(_PNH_RETURNOUTPUT);
                $columns = array();
                $columns[] = $output->FormCheckbox('prodid', false, $item['data']);
                $columns[] = $output->Text($item['mfg']);
    .......        
        $output->TableEnd();
        $output->FormSubmit(_SALESRANKBUTTONADD);
        $output->FormEnd();
    ........


    Function 2 looks like this:

    Code

    function salesrank_user_watchlistadd($args)
    {
        // Get parameters. It seems to me I need an array here...
        list($userid,
             $prodids) = pnVarCleanFromInput('userid',
                                              'prodid'));
                                           
        extract($args);
    ......
        // Load API.  
        if (!pnModAPILoad('SalesRank', 'user')) {
            pnSessionSetVar('errormsg', _LOADFAILED);
            return $output->GetOutput();
        }

        // The API function is called.  
       
        $wlid = pnModAPIFunc('SalesRank',
                            'user',
                            'watchlistadd',
                            array('userid' => $userid,
                                  'prodids' => $prodids));

    ........


    Function 3 in userapi looks like this:

    Code

    function salesrank_userapi_watchlistadd($args)
    {

        // Get arguments from argument array
        extract($args);
    foreach ($prodids as $prodid) {
        ......

        // Get datbase setup
        list($dbconn) = pnDBGetConn();
        $pntable = pnDBGetTables();

        $watchlisttable = $pntable['salesrank_watchlist'];
        $watchlistcolumn = &$pntable['salesrank_watchlist_column'];

        // Get next ID in table
        $nextId = $dbconn->GenId($watchlisttable);

        // Add item
        $sql = "INSERT INTO $watchlisttable (
                  $watchlistcolumn[wlid],
                  $watchlistcolumn[userid],
                  $watchlistcolumn[prodid],
                  $watchlistcolumn[wstatus],
                  $watchlistcolumn[wdatestamp])
                VALUES (
                  $nextId,
                  "
    . pnVarPrepForStore($userid) . ",
                  "
    . pnvarPrepForStore($prodid) . ",
                  'A',
                  "
    . $dbconn->DBTimestamp(time()) . ")";
    //  echo "sql --->" . $sql;
        $dbconn->Execute($sql);
    .......


    I get an

    Quote

    Invalid argument supplied for foreach()
    error message.

    I can't figure out how to pass and accept checkbox array data. Your suggestions and pointers would be appreciated.

    Your help is very much appreciated.

    Steve
  • let's say I have this form:

    What kind of pet do you have?

    _dog
    _cat
    _fish
    _other

    If a user selects dog and cat how do I pass the both results to the API function that inserts the data into the DB?

    Steve
  • Don't know if this is the best way, but it is pretty simple.

    First in my formcheckbox code I put a [] after my checkbox name.

    Code

    $columns[] = $output->FormCheckbox('prodids[]', false, $item['prodid']);


    In my user function that receives the form data, I defined the name as an array...

    Code

    $prodids = array();
        // Get parameters
        list($userid,
             $prodids) = pnVarCleanFromInput('userid',
                                              'prodids');


    And then before the call to the apifunction that will insert the data in my DB , I did a foreach..

    Code

    foreach ($prodids as $prodid) {    
        $wlid = pnModAPIFunc('SalesRank',
                            'user',
                            'watchlistadd',
                            array('userid' => $userid,
                                  'prodid' => $prodid));
    }


    It works fine. Would be open to any suggestions on improvements..

    Steve

This list is based on users active over the last 60 minutes.