我遇到了一个复选框问题:它没有更新到数据库。
$emailnotification = new Zend_Form_Element_Checkbox('emailnotification ', 'emailnotification', array(
'checkedValue' => 1,
'uncheckedValue' => 0,
) );
$emailnotification->setLabel('emailnotification');
$emailnotification->setValue(1);
$this->addElement($emailnotification);
并且在Controller上我有动作更新以下代码以进行更新:
if($this->_request->isPost())
{
$formData = $this->getRequest()->getPost();
if($form->isValid($formData))
{
$contact = new Admin_Model_DbTable_Contact();
$data = array();
$data['idContact'] = $idContact;
$data['firstname'] = $form->getValue('firstname');
$data['lastname'] = $form->getValue('lastname');
$data['emailnotification'] = $form->getValue('emailnotification');
if($contact->editContact($data))
{
echo json_encode(array(
"response" => true,
"message" => "Contact " . $data['firstname'] . " " . $data['lastname'] . "a été modifié"
));
exit();
} else {
echo json_encode(array(
"response" => false,
"errorMessage" => "Il y a eu une erreur dans l'edition de Contact."
) );
exit();
}
}
}
function editcontact:
public function editContact(array $data) { if(!empty($data)) { if($this->update($data, array('idContact = ?' => $data['idContact'])) > 0 ) { return true; } return false; } return false; }
on .phtml
$('#editContact').submit(function(event)
{
var formId = $(this).attr('id');
// Stop full page load
event.preventDefault();
//Request
var data = {
// contact's properties
firstname : $("#firstname").val(),
lastname : $("#lastname").val(),
emailnotification : $("#emailnotification").val(),
batnotification : $("#batnotification").val()
};
// Send
$.ajax({
url: $('#'+formId).attr('action'),
dataType: 'json',
type: 'POST',
data: data,
success: function(data, textStatus, XMLHttpRequest)
{
if (data.response == true)
{
alert(data.message);
//upContent('userManagement/index/','');
}
else
{
alert(data.message);
}
并且对我不起作用总是消息未定义而不更新数据库
提前致谢
答案 0 :(得分:0)
我发现您的代码存在一些问题,并且可以简化保存数据,因此我将举例说明如何设置editContact
方法。显然应该在表单中进行验证。请记住,数据库表和字段名称可能与您的环境不同。
// Admin_Model_DbTable_Contact
public function editContact( array $data )
{
Zend_Registry::get( 'db' )->update(
'contact_table',
$data,
'contact_id = ' . intval( $data['idContact']
);
return Zend_Registry::get( 'db' )->lastInsertId();
}
您还可以稍微优化控制器代码:
// In controller action
if($this->_request->isPost() && $form->isValid( $formData ) )
{
$contact = new Admin_Model_DbTable_Contact();
if( $contact->editContact( $form->getValues() + array( 'idContact' => $idContact ) ) ) {
{
echo json_encode(array(
"response" => true,
"message" => "Contact {$form->getValue( 'firstname' )} {$form->getValue( 'lastname' )} a été modifié"
) );
} else {
echo json_encode(array(
"response" => false,
"errorMessage" => "Il y a eu une erreur dans l'edition de Contact."
) );
}
exit();
}