【问题标题】:How do I return an unsaved variable in a PHP form?如何在 PHP 表单中返回未保存的变量?
【发布时间】:2016-09-21 10:47:02
【问题描述】:

已解决:感谢@riggsfolly 为我指明了正确的方向。我应该先看看 Drupal Forms API。添加了一个“item”类型的表单元素,它就像一个魅力:

$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.'),);

更新:我应该提到这是一个 Drupal 表单。对不起。

[PHP 新手] 我正在使用以下不是我编写的代码:

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;
}

我想在这个非常简单的表单中包含对用户的描述,说明他们在这里所做的事情。我试过添加这个:

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

以及尝试将整个表单移动到我在其他地方看到的另一种方法中,我使用$form .= 'My description info.'; 将其他行连接到表单中,但无济于事。

我要做的就是在选择之前在表单中包含一个语句,告知用户这是做什么的。

谢谢!

【问题讨论】:

  • 也许我今天需要更多的咖啡,但我不明白你的问题:/
  • $form 是一个数组 .= 是字符串连接

标签: php forms drupal-7


【解决方案1】:

$form 是一个数组,您需要做的就是向数组中添加另一个事件,例如

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;
}

当然,您随后必须查看处理此函数结果的代码,并针对此新出现做一些适当的事情。

【讨论】:

  • 也打败我吧:(
  • @RiggsFolly - 完美 - 这是我尝试过的其他方法。但是使用表单数据的函数显然对新出现没有任何作用。需要吗?如果使用了其他事件,但这个事件只是位于未检查和未使用的数组中,这是一个问题吗?
  • 未使用不是问题。但就像我在最后一句话中所说的那样。您现在必须更改显示页面的代码以使其使用此新消息
【解决方案2】:

如果我正确理解您的问题。您正在尝试向 $form 数组添加另一个值。

因为它是一个数组,所以你不能像 $form .= 'text'; 那样附加一个字符串到它上面;

如果 is 只是一个变量,你可以,例如:

$form = 'hello';
$form .= ' this is a form';
echo $form;

这将显示“你好,这是一个表单”

但是因为你的 $form 是一个数组,所以你必须添加这个字符串:

$form['string'] = 'your string'; 

想要类似的东西:

array(

    ['string'] => 'your string'

)

$form 的其余部分看起来像:

Array
(
[cid] => Array
    (
        [#title] => 
        [#type] => hidden
        [#required] => 1
        [#value] => 1
    )

[grading_method] => Array
    (
        [#title] => 
        [#type] => select
        [#default_value] => 1
        [#options] => Array
            (
                [0] => Percentage
                [1] => Points
            )

    )

[submit] => Array
    (
        [#type] => submit
        [#value] => 1
    )
[string] => 'your text string here'

)

【讨论】:

  • 感谢 virepo - 我只是不知道如何处理额外的事件。
  • 这取决于你需要它做什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
相关资源
最近更新 更多