我正在构建一个管理模块并需要处理一个表单,但在将变量发送到 POST 时遇到问题,我只是被重定向到仪表板。表格看起来像这样:
<form name="notes" action="<?php echo Mage::helper("adminhtml")->getUrl("foo/index/processnotes/");?>" method="post">
<input type="hidden" name="another_form_key" value="<? echo $this->getFormKey(); ?>" />
我这样做是因为为管理员打开了安全 key 。它给了我一个像这样的网址:
foo/index/processnotes/key/4745f5fbb9c168778958d5d4a4c2c0ef/
在我的 Controller 中:
public function processnotesAction(){
$model = Mage::getModel('foo/process');
// I am not sure how I am supposed to send $_POST values here
}
并在 Package/foo/Model/Process.php
我希望能够在这里处理我的表单中的 POST 变量,但我看不出有什么问题,我只是被发送到仪表板。
<?php
class Package_foo_Model_Process extends Mage_Core_Model_Abstract
{
public function noteProcess() {
$test = $_POST['myValue'];
echo $test;
}
}
更新
阅读答案后,我想补充一点我的真实代码在做什么以及我如何使用 $_POST。我正在设置一个简单的示例来发布表单,但我意识到我在 Magento 中可能做错了很多事情。
$query="INSERT INTO `notes_value` ( `color`, `the_id`, `the_value` ) VALUES ";
$sku = $_POST['color'];
array_pop($_POST);
foreach ($_POST AS $key => $value) {
$values[] = "('$sku', '$id', '$value')";
}
$query .= implode(', ', $values) . ';';
我的梦想是我可以 $resource->getConnection('core_write');
将其实际插入数据库,但对目前的进展情况不太乐观。
最佳答案
通常当您因为无法识别您的 key 而被重定向到仪表板时。我通常会使用以下语法
<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
此外,我倾向于禁用管理 key 以验证这是否是您的问题。
接下来,永远,永远在 Magento 中使用 $_POST
,所有变量都可以访问(干净且安全)。 POST 信息可以全局访问(不仅仅是在 Controller 中),但是您仍然可以像这样从 Controller 中设置模型中的变量:
public function processnotesAction(){
$model = Mage::getSingleton('foo/process');
$model->setData('postdata', $this->getRequest()->getPost() );
}
然后从您的模型访问它们:
$this->getData();
$this->getMyPostedField();
或者你可以在你的模型中使用它
Mage::app()->getRequest()->getPost();
Mage::app()->getRequest()->getParam('myargument');
getPost
也可以采用参数来获取单个 POST 值 - 但它不会获取 GET 值。 getParam
将返回类似的 GET 和 POST 值。
关于php - 在 magento 管理模块中使用 Controller 和模型提交表单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9726680/