Hello, 2 days I have been developing module and a function within, which provides processing operations for this form

function lingvist_add_words($prefix, $code = 0) {    
    $my_base = drupal_get_path('module', 'codelist');
    $msg = '';
    
    if ($code == 0) {
        return drupal_set_message('Incorrect request, please provide valid data.');
    }
    
    // form processing
    if (isset($_POST['code']) && is_numeric($_POST['code'])) {
        //... ...
        //... ...
        //... ...
        $code = check_plain($_POST['code']);

        $result = db_query("SELECT MAX(`code_id`) AS `id` FROM `codes` WHERE `code`=%d LIMIT 1", $code);

        $last_code_id = db_fetch_array($result);
        
        $msg .= $last_code_id['id'];
        //... ...
        //... ...
        //... ...
    }
    
    // HERE I GOT NOTHING in $msg

    //...
    //...
    //...
}

and drupal fires warning like

warning: mb_strlen() expects parameter 1 to be string, array given in /home/drupal/www/includes/unicode.inc on line 405.

I guess this is a little bug. Does anyone knows how to tackle this problem?

Thanks in advance

With best,
Sultan

Comments

kenuck’s picture

My guess would be this linte : $code = check_plain($_POST['code']);

Double check the value for $_POST['code'] and make sure it's not an array.

cheers

marcvangend’s picture

I think that cannot be the problem, because the check_plain is inside a if (isset($_POST['code']) && is_numeric($_POST['code'])). (can you http POST an array anyway?)

Line 405 of unicode.inc is the drupal_strlen() function (http://api.drupal.org/api/function/drupal_strlen/6). However I don't see where your code would call this function directly or indirectly. Can you paste the complete code, and maybe do a backtrace?

i-sultan’s picture

This is the full code of function

function lingvist_add_words($prefix, $code = 0) {    
    $my_base = drupal_get_path('module', 'codelist');
    $msg = '';
    
    $code = intval($cid);
    
    if ($cid == 0) {
        return drupal_set_message('Incorrect request, please provide valid data.');
    }
    
    if (isset($_POST['code']) && is_numeric($_POST['code'])) {
        $code = intval($_POST['code']);
        
        $result = db_query("SELECT MAX(`code_id`) AS `id` FROM `codes` WHERE `code`=%d LIMIT 1", $code);

        $last_id = db_fetch_array($result);
        
        $msg = '<b>'.$last_id['id'].'</b><br />';
    }
    
    drupal_add_css($my_base.'/css/override.css');

    drupal_add_js($my_base.'/js/addwords.js');

    $result = db_query("SELECT `code` FROM `codes` WHERE `code_id`=%d LIMIT 1", $cid);

    $row = db_fetch_array($result);
    
    $form = drupal_get_form('codes_build_form');
    $form = preg_replace('/\[:VALUE:\]/', $code, $form);
    $form = preg_replace('/\[:CAPTION:\]/', "List of codes", $form);
    
    return $msg.$form;
}
i-sultan’s picture

    return mb_strlen($text);

and var_dump($_POST['code']) says

string '4' (length=1)
i-sultan’s picture

Big thanks to You guys :) I have solved this problem, article closed :)

With best,

marcvangend’s picture

I'm glad you solved it. Can you tell us what the problem/solution was? That way you share your knowledge with the community. Thanks.

i-sultan’s picture

My function function lingvist_add_words as You could see call drupal_get_form function to get my form rendered. And by the way this function tries to handle form submission itself, here was the main problem because I had to separate form handler function from function which generates this form and never the less I traced all of the POST variables for an array type but I still can not localize what sometime really happens because of code

if (isset($_POST['code']) && is_numeric($_POST['code'])) {
        $code = intval($_POST['code']);
        
        $result = db_query("SELECT MAX(`code_id`) AS `id` FROM `codes` WHERE `code`=%d LIMIT 1", $code);

        $last_id = db_fetch_array($result);
        
        $msg = '<b>'.$last_id['id'].'</b><br />';
}

works properly and $_POST['code'] is not a array type, also I have been digging this problems alongside with my usual work schedule :)

marcvangend’s picture

In that case I think you should look a little better at the Form API. The FAPI allows you to define your own submit handler and submission validators. Start reading here: http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6.