使用Joomla 2.5获取$_POST数据

5

这是我的表单,看起来没问题,所以这不应该是个问题,我还删除了enctype以确保它不是原因。

    <form action="<?php echo JRoute::_('index.php?option=com_woo&task=hello.create'); ?>" enctype="multipart/form-data" method="post">
      <p>
        Project Name : 
        <input style="width:30%;" name="name" id="name"/>
        <input style="display:none;" id="user_id" name="user_id" value="<?php echo $user->id;?>"/>
        <input style="display:none;" id="county" name="county"/>
        <input style="display:none;" id="state" name="state"  />
      </p>
      <button type="submit" class="btn-green" id="select_county">Create Project</button>
    </form>

控制器内部Hello

    public function create()
    {
       $jinput = JFactory::getApplication()->input;
       $foo = $jinput->get('state', '', 'filter');
       print_r($foo);
       die;
    }

返回"NULL"

有什么想法吗?


根据您的输入,state字段已设置为display:none。因此,每当表单提交时,它会将空输入发送到state,这就是为什么您收到null而不是值的原因。 - Toretto
5个回答

8
你可以尝试这样做 -
$input = JFactory::getApplication()->input;
$post_array = $input->getArray($_POST);

4
$input = new JInput;
$name = $input->get('name', '', 'post');
$country = $input->get('country', '', 'post');
// etc.

然后您可以使用一系列的JInput类方法来实现特定的目的:

 // method      integer  getInt()       getInt($name, $default = null)    Get a signed integer.
 // method      integer  getUint()      getUint($name, $default = null)   Get an unsigned integer.
 // method      float    getFloat()     getFloat($name, $default = null)  Get a floating-point number.
 // method      boolean  getBool()      getBool($name, $default = null)   Get a boolean.
 // method      string   getWord()      getWord($name, $default = null)
 // method      string   getAlnum()     getAlnum($name, $default = null)
 // method      string   getCmd()       getCmd($name, $default = null)
 // method      string   getBase64()    getBase64($name, $default = null)
 // method      string   getString()    getString($name, $default = null)
 // method      string   getHtml()      getHtml($name, $default = null)
 // method      string   getPath()      getPath($name, $default = null)
 // method      string   getUsername()  getUsername($name, $default = null)

4
我认为使用JInput获取整个$_POST的最佳选项是:
JFactory::getApplication()->input->post->getArray();

如果您想从请求中获取特定的数组(例如称为“jform”),则可以使用以下代码:

JFactory::getApplication()->input->get('jform', array(), 'ARRAY');

0

您可以从特定的超级全局变量中获取值

$foo = $jinput->get->get('varname', 'default_value', 'filter');

$foo = $jinput->post->get('varname', 'default_value', 'filter');

$foo = $jinput->server->get('varname', 'default_value', 'filter');

欲了解更多信息,请访问使用JInput检索请求数据


-2

您可以尝试将表单操作更改为:

<?php echo JRoute::_('index.php?option=com_woo&view=hello&task=create');

由于您的任务被称为create而不是hello.create,因此这样做可能会更好...

然后我总是这样做的

$post = JRequest::get('post');
print_r($post['state']);

3
据信,自Joomla 1.7起,“JRequest”已被弃用。虽然您并没有错,但最好的做法是在Joomla 2.5及以上版本中使用类似以下代码的内容:$post = new JInput($_POST);。;) - Lodder
Lodders 正确。有关 JInput 的更多信息,请参见此处 http://docs.joomla.org/Retrieving_request_data_using_JInput - George Wilson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接