【发布时间】:2017-06-18 13:13:56
【问题描述】:
我正在尝试在我的 Magento 网站中创建一个单词过滤器,基本上我有一个带有 textarea 和一个提交按钮的表单,如下所示:
<form id="answer_form_<?php echo $id;?>" class="form" method="post"
action="<?php echo Mage::getUrl('productquestions/productquestions/saveanswers',array('product_questions_id'=>$id));?>">
<textarea id="txt_send" class="input-text required-entry " name="content"
id="answer_content_<?php echo $id;?>" title="Content"></textarea>
<button id="btn_send" style="float: left;" type="submit" class="button"
title="Send Message"><span><span><?php echo $this->__('Send Message') ?></span></span></button>
</form>
我需要做的是在表单提交之前它被保存在数据库中时从textarea过滤单词,所以我创建了一些php函数并对其进行了调整。最终代码为:
function wordFilter($text) {
$filter_terms = array('\bass(es|holes?)?\b','\bshit(e|ted|ting|ty|head)\b');
$filtered_text = $text;
foreach($filter_terms as $word) {
$match_count = preg_match_all('/' . $word . '/i', $text, $matches);
for($i = 0; $i < $match_count; $i++) {
$bwstr = trim($matches[0][$i]);
$filtered_text = preg_replace('/\b' . $bwstr . '\b/', str_repeat("*", strlen($bwstr)), $filtered_text);
}
}
return $filtered_text;
}
if(isset($_POST['btn_send'])) {
$text = htmlentities($_POST['txt_send']);
$text = wordFilter($text);
}
到目前为止,我只添加了两个单词进行测试,当我使用这两个单词进行文本处理时,它会正常保存,而不会将它们更改为“*****”。我避免使用 JS,因为它是客户端。
谁能告诉我我错过了什么?
谢谢!
已编辑:
作为一个 Magento 插件。该操作将表单重定向到:
productquestions/productquestions/saveanswers',array('product_questions_id'=>$id)); 并根据 id 更改 url。例如:siteurl/index.php/productquestions/productquestions/saveanswers/product_questions_id/40
在此控制器页面中,我具有以下功能:
public function saveanswersAction()
{
$answers = $this->getRequest()->getPost();
$answerCollection = array();
$model = Mage::getModel('productquestions/answers');
$id = $this->getRequest()->getParam('product_questions_id');
$model->setData('product_questions_id',$id);
$model->setData('answers',$answers['content']);
$model->save();
$answerCollection[] = $model;
}
【问题讨论】:
标签: php forms function magento filter