2010-06-19 52 views
1

我正在使用Zend框架,并说明什么是工作我添加了saveAction()这个职位,它的工作完美无瑕。它会根据我的需要动画和改变课程。函数上传,而不是只改变文本,并忽略JavaScript。我可以做警报,但jQuery是不可能的。我得到错误,说$是未定义的。怎么可能在1个案例中未定义,而不是另一个?当使用ajax和.attr('class','new_class')时出现jQuery问题

我正在用ajax捕捉上传表单,并将其放入#savecontainer中。

希望有一个解决方案,它似乎是一个小问题的地方,但我无法自己找到它。谢谢。

它看起来像这样:

$(document).ready(function() { 
    $('#newsForm').ajaxForm({ 
    target: '#savecontainer' 
    }); 
    $('#uploadForm').ajaxForm({ 
    target: '#savecontainer' 
    }); 
    $("#btn_save").click(function() { 
    $('#newsForm').submit(); 
    }); 
    $("#btn_upload").click(function() { 
    $('#uploadForm').submit(); 
    }); 
}); 


public function saveAction() 
{ 
    $this->_helper->layout->disableLayout(); 
    $db = new Admin_Model_DbAccess(); 
    if(isset($_POST['active'])) 
    $_POST['active'] = 1; 
    else 
    $_POST['active'] = 0; 

    if($_POST['id'] == 0){ 
    // If it is a new post 

    $data = array(
     'header' => $_POST['header'], 
     'message' => $_POST['message'], 
     'date' => time(), 
     'user' => Zend_Auth::getInstance()->getStorage()->read()->id, 
     'image' => $_POST['image'], 
     'active' => $_POST['active'], 
     'category' => $_POST['category'] 
    ); 
    if($db->addNews($data)){ 
     // set the css variables to saved 
     echo "<script type='text/javascript'> 
     $('#savecontainer').fadeOut(200).attr('class', 'savecontainer_success').fadeIn(400); 
     $('#news_id').attr('value', '".$db->lastInsertId()."'); 
    $('#upload_box').show('slide', {direction: 'up'}, 500); 
    $('#news_id_upload').attr('value', '".$db->lastInsertId()."'); 
     </script>"; 
     echo "Status: Added."; 
    }else{ 
     // set the css variables to failed 
     echo "<script type='text/javascript'> 
     $('#savecontainer').fadeOut(200).attr('class', 'savecontainer_fail').fadeIn(400); 
     </script>"; 
     echo "Status: Error."; 
    } 
    }else{ 
    $data = array(
     'header' => $_POST['header'], 
     'message' => $_POST['message'], 
     'image' => $_POST['image'], 
     'active' => $_POST['active'], 
     'category' => $_POST['category'] 
    ); 
    $db = new Admin_Model_DbAccess(); 
    if($db->updateNews($_POST['id'], $data)){ 
     // set the css variables to saved 
     echo "<script type='text/javascript'> 
     $('#savecontainer').fadeOut(200).attr('class', 'savecontainer_success').fadeIn(400); 
     </script>"; 
     echo "Status: Updated."; 
    }else{ 
     // set the css variables to failed 
     echo "<script type='text/javascript'> 
     $('#savecontainer').fadeOut(200).attr('class', 'savecontainer_fail').fadeIn(400); 
     </script>"; 
     echo "Status: Error."; 
    } 
    } 
} 
public function uploadAction(){ 
    $this->_helper->layout->disableLayout(); 

    if ($this->_request->isPost()) { 
    //Startup the adapter to upload 
     $adapter = new Zend_File_Transfer_Adapter_Http(); 
    //Define the upload path 
     define('UPLOAD_NEWS_IMAGE_PATH', APPLICATION_PUBLIC_PATH. "/img/news/"); 
    // Fixa upload path 
     $adapter->addValidator('Extension', false, array('jpg', 'jpeg' , 'gif' , 'png')) 
     ->addValidator('Count', false , array('min' => 0, 'max' => 0)); 

    $file = $adapter->getFileInfo(); 

    $adapter->receive(); 
    $messages = $adapter->getMessages(); 
    if(isset($messages['fileCountTooMany']) && !isset($messages['fileExtensionFalse'])){ 
    //If the file does exists (Everything went fine); 
    $fileinfo['ext'] = end(explode(".", $file['upload_0_']['name'])); 
    $uploaded_filename = $_POST['id'].".".$fileinfo['ext']; 
    // Change name to id.jpg for example 
    move_uploaded_file($file['upload_0_']['tmp_name'], UPLOAD_NEWS_IMAGE_PATH.$uploaded_filename); 
    // resize to 
    $full_thumb = Butikadmin_Model_PhpThumbFactory::create(UPLOAD_NEWS_IMAGE_PATH.$uploaded_filename); 
    $full_thumb->resize(960, 500); 
    $id = $_GET['id']; 
    if($full_thumb->save(UPLOAD_NEWS_IMAGE_PATH.$uploaded_filename)){ 
     // set the css variables to saved 
     echo "<script type='text/javascript'> 
      $('#savecontainer').fadeOut(200).attr('class', 'savecontainer_success').fadeIn(400); 
      $('#upload_box').fadeOut(500); 
     </script>"; 
     echo "Status: Uploaded."; 
    } 
    }else{ 
    // If the file is not right format 
    // set the css variables to saved 
     echo "<script type='text/javascript'> 
      $('#savecontainer').fadeOut(200).attr('class', 'savecontainer_fail').fadeIn(400); 
     </script>"; 
     echo "Status: Error."; 
    } 
    } 
} 

回答

0

为什么不使用Ajax调用,而不是.submit的? 并在php代码中,从函数中删除js记录并返回数据(数组或ojbect),并使用返回该数据的回调函数在js脚本中将其处理回去