Hi,

I use form api to generate a form, capture output from post variables and then output this variable true theme() function. But if i output it thru theme() it doesn't get displayed while if i do simple echo it does. Why does theme() noy display the contents of this ($_POST['edit']['urly']) variable?

Here is the code i use for capturing and displaying variable:

1. This one doesn't display/echo contents of $_POST['edit']['urly']

 $urly=$_POST['edit']['urly'];
 $output .= 'XX'. $urly . 'XX';
 print theme('page',$output);

2. This one DOES display/echo contents of $_POST['edit']['urly'] (but it's not themable outout)

 $urly=$_POST['edit']['urly'];
 echo $urly;

Can any1 tell me how to display contents of $_POST['edit']['urly'] thru theme function?

Comments

nevets’s picture

Is this a custom node type (using form, submit, insert, update hooks) or a custom form?

From which function are you trying to do the print theme('page', $output)? This does make a difference as it will no do what you expect if is in the submit function for a custom form.

If you are just trying to make sure things are set properly you could use drupal_set_message($output);

hexa’s picture

I wanted to keep it as short as possible ;-)

i don't really fully understand what are you asking for so i'll try to write just necessary info.

I'm writing a module and making drupal aware of it like this:

//Every module's needs.
function import_news_help($section) {
 $output = '';
  switch ($section) {
    case 'admin/modules#description':
     $output = ("This module synchronyses news from foobar.com");
    break;
  }
 return $output;
}//function import_news_help

Then i generate menu items like this:

//Add entry to menu for access to settings page
function import_news_menu($may_cache) {
 $items = array();
   if ($may_cache) {
    $items[] = array(
    'path' => 'import_news_settings',
    'title' => 'import news settings',
    'callback' => 'import_news_my_settings',
    //'callback arguments' => '',
    'access' => user_access('can set import news'),
    // 'weight' => '0',
    'type' => MENU_NORMAL_ITEM
    );
    $items[] = array(
    'path' => 'import_news',
    'title' => 'import news',
    'callback' => 'import_news_go',
    //'callback arguments' => '',
    'access' => user_access('can import news'),
    // 'weight' => '0',
    'type' => MENU_NORMAL_ITEM
    );
   }//end if($may_cache)
 return $items;
}//function hook_menu

Now i'm trying to implement settings function (method?) import_news_my_settings() like this:

function import_news_my_settings(){
  //clear $output
 $output = '';
 //let's capture variables if they are set already
 $output .= 'Current settings: ;-)<br>';
 $output .= 'Yahoo news URL: ';
 //$output .= '<code>' .  print_r ($_POST) . '

'; //debug line
//echo $_POST['edit']['urly']; //debug line
$urly=$_POST['edit']['urly'];
//echo $urly; //debug line
$output .= 'XX'. $urly . 'XX'; //debug line
variable_set('importNewsURLyahoo',$_POST['edit']['urly']);
$output .= variable_get('importNewsURLyahoo','Variable never been set before.');
$output .= '

';

//form for variable setting.
//'action' => $_SERVER['REQUEST_URI'],
$output .= 'You can change setting here.
';
$forma[urly] = array(
'#type' => 'textfield',
'#title' => t('Enter Yahoo news URL'),
'#default_value' => 'http://www.yahoo.org/',
'#maxlenght' => '333',
);
$forma['submit'] = array('#type' => 'submit', '#value' => t('Save new settings.'));

//let's add this form to output
$output .= drupal_get_form('my_form', $forma);

//print out the page
print theme('page',$output);

}//function import_news_my_settings

This method should display site for settings, as well as be able to set those settings. I'm trying to be consistent with drupal module development requirements so i'm using form api and theme function.

I've also taken a look at drupal_set_message in api.dru..org and besides noticing that it's evil ;-) (located in includes/bootstrap.inc, line 666) :-) don't understand how it can help me.

i hope this additional information is enough for someone to tell me why theme() function doesn't want to display my POST captured variables :-(

Tnx for help.

nevets’s picture

When you submit your form, the code gets called twice (I suspect this has do to with the way the form API works), so you need to make sure the post variable is set before saving. You could also use the my_form_submit($form_id, $form_values) function to save the value. [Side note on form_id, it really should start with your module name to avoid a name clash].
Also a minor point in 4.7 it is probably better to do a return $output than print theme('page', $output).

The following version of your function gets the current value by calling variable_get (which is also used to get the default value). In addition it checks to make sure the value is set before saving it.

<?php
function import_news_my_settings(){
  //clear $output
$output = '';

if ( isset($_POST['edit']['urly']) ) {
	variable_set('importNewsURLyahoo',$_POST['edit']['urly']);
}

//let's capture variables if they are set already
$output .= 'Current settings: ;-)<br>';
$output .= 'Yahoo news URL: ' . variable_get('importNewsURLyahoo','http://www.yahoo.org/') . '<br />';

//form for variable setting.
//'action' => $_SERVER['REQUEST_URI'],
$output .= 'You can change setting here.<br>';
$forma['urly'] = array(
'#type' => 'textfield',
'#title' => t('Enter Yahoo news URL'),
'#default_value' => variable_get('importNewsURLyahoo','http://www.yahoo.org/'),
'#maxlenght' => '256',
);
$forma['submit'] = array('#type' => 'submit', '#value' => t('Save new settings.'));

//let's add this form to output
$output .= drupal_get_form('my_form', $forma);

return $output;

}//function import_news_my_settings
hexa’s picture

Thank you once more.
I'll try and implement your code.