我正在研究zend框架中的一个小部件,我无法获得单选按钮值来更新数据库。有人可以帮忙吗也许有人看到我不喜欢的东西。
提前感谢。
以下是无线电html的示例:
<div class="vote_block">
<form class="data_form">
<ul class="input_radio_group">
<li>
<input id="mediaRating1" class="radioBtnClass" type="radio" title="Poor" value=".1" name="rating">
<label class="label_radio">Poor</label>
<div class="clear"></div>
</li>
</ul>
<button id="rateVoteButton" class="button"><span><?php echo $this->translator('send_button'); ?></span></button>
</form>
我只发了一个单选按钮,这样你就可以了解我的情况。
以下是页面上的Javascript:
<script type="text/javascript">
$(document).ready(function(){
$('#rateVoteButton').click(function() {
var rating = Array();
$(this).parents('.rating_block').find('li input').each(function() {
if ($(this).attr('checked') == true) {
rating.push($(this).attr('value'));
});
if (rating.length > 0) {
var data = $.toJSON({
'set_id': '<?php echo $this->set->set_id; ?>',
'rating': '<?php # echo $this->rating; ?>'
});
}
}
});
});
现在的小部件页面就像控制器一样:
protected function _prepareShow()
{
$setId = $this->_request->getParam('set_id');
$rating = $this->_request->getParam('rating');
$conn = XXX_Db_Connection::factory()->getSlaveConnection();
$setDao = XXX_Model_Dao_Factory::getInstance()->setModule('media')->getSetDao();
$setDao->setDbConnection($conn);
$data = Zend_Json::encode(array('set_id' => $setId, 'rating' => $rating));
$this->_view->assign('rating', $rating);
$this->_view->assign('data', $data);
}
protected function _prepareResult()
{
$setId = $this->_request->getParam('set_id');
$rating = $this->_request->getParam('rating');
$conn = XXX_Db_Connection::factory()->getMasterConnection();
$setDao = XXX_Model_Dao_Factory::getInstance()->setModule('media')->getSetDao();
$setDao->setDbConnection($conn);
$setDao->increaseRating($rating);
$data = Zend_Json::encode(array('set_id' => $setId, 'rating' => $rating));
$this->_view->assign('rating', $rating);
$this->_view->assign('data', $data);
}
最后但并非最不重要的是sql语句:
public function increaseRating($rating)
{
$sql = sprintf("UPDATE " . $this->_prefix . "media_set
SET rating = rating + '%s'
WHERE set_id = '%s'",
mysql_real_escape_string($rating->rating),
mysql_real_escape_string($rating->set_id));
mysql_query($sql);
return mysql_affected_rows();
}
当选择收音机并按下按钮时,我会在地址栏中的链接后获得?rating = .1但数据库中没有更新。
任何建议都会有所帮助。再次感谢。