使用ajax和.attr('class','new_class')时jQuery问题

时间:2010-06-19 11:39:41

标签: php zend-framework jquery

我正在使用Zend Framework,为了说明工作原理,我在这篇文章中添加了saveAction(),它完美无瑕。它将根据需要制作动画并更改课程。上传函数只改变文本并忽略javascript。我可以做有效的警报,但jquery是不可能的。我得到错误,说$是未定义的。怎么可能在1个案例中没有被定义而不是另一个?

我正在使用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.";
  }
  }
}

1 个答案:

答案 0 :(得分:0)

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