Smartasc Smartasc - 2 months ago 11
PHP Question

How do I return an unsaved variable in a PHP form?

SOLVED: Thanks to @riggsfolly for pointing me in the right direction. I should have looked at Drupal Forms API to begin with. Added an form element of type 'item' and it worked like a charm:

$form['description'] = array('#type'=> 'item', '#title'=> t('To maintain consistency across your course, use the link below to select whether you plan to grade assessments based on points or percentages.'),);


UPDATE: I should have mentioned this is a Drupal form. Sorry.

[PHP Newbie] I am working with the following code I didn't write:

function grading_method_form($form, &$form_state){
$courseId = $_SESSION['courseId'];
$gradingMethodDetails = getCourseGradingMethod($courseId);
$form = array();
$form['cid'] = array('#title'=>NULL,'#type'=>'hidden','#required'=>TRUE,'#value'=>$courseId,);
$form['grading_method'] = array('#title'=>'','#type'=>'select','#default_value'=>$gradingMethodDetails['grading_method'],'#options'=>array(0=>'Percentage',1=>'Points'),);
$form['submit'] = array('#type'=>'submit','#value'=>t('Save Grading Method'));
return $form;
}


What I would like to include in this very simple form is a description for users about what they are doing here. I've tried adding this:

$form .= "To maintain consistency across your course, use the link below to select whether you plan to grade assessments based on points or percentages.";


as well as tried to move the whole form into another approach I've see elsewhere where I concatenate additional lines into the form using
$form .= 'My description info.';
to no avail.

All I want to do is to include a statement in the form before the select that informs the user what this is for.

Thanks!

Answer

$form is an array all you need to do is add another occurance to the array like

function grading_method_form($form, &$form_state){
    $courseId = $_SESSION['courseId'];
    $gradingMethodDetails = getCourseGradingMethod($courseId);
    $form = array();
    $form['cid'] = array('#title'=>NULL,'#type'=>'hidden','#required'=>TRUE,'#value'=>$courseId,);
    $form['grading_method'] = array('#title'=>'','#type'=>'select','#default_value'=>$gradingMethodDetails['grading_method'],'#options'=>array(0=>'Percentage',1=>'Points'),);
    $form['submit'] = array('#type'=>'submit','#value'=>t('Save Grading Method'));


    $form['new_message'] = "To maintain consistency across your course, use the link below to select whether you plan to grade assessments based on points or percentages.";

    return $form;
}

Of course you will then have to look at the code that deals with the result of this function and do something appropriate with this new occurance.

Comments