views:

105

answers:

1

I am using Zend Framework, and to illustrate what is working I added the saveAction() to this post and it works flawlessly. It will animate and change the class as I want. The function upload instead only change the text and ignore the javascript. I can do alerts that works, however jquery is not possible. I get error saying that $ is undefined. How could it be undefined in 1 case and not the other?

I am catching the upload form with ajax and just throw it into the #savecontainer.

Hope there's a solution, it seems to be a tiny issue somewhere, but I can't find it on my own. Thank you.

it looks like this:

$(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.";
  }
  }
}
A: 

why not using an ajax call instead of .submit? and in the php code, remove the js record and return data (array or ojbect) from the function and handle it back in the js script with a callback function which return that data

Kreker