I've finally got back to this.  I just cannot get it to do anything
other than submit the form.  When I click the 'Preview' button, the
form submits, I get a success message, and go back to the index page -
which is exactly what happens if I submit the form using the regular
Submit button.

I'd really appreciate any help - I'm new to Ajax, so I'm sure I'm just
doing something stupid, but I don't know where to start looking for
it.

Thanks!
Sharon



Here's what I have so far:


PAGES_CONTROLLER.PHP:

class PagesController extends AppController {

 var $helpers = array('Html', 'Form', 'Javascript', 'Time', 'Fck',
'Ajax');

... $uses, some functions etc ...

 function admin_edit($id = null) {
  $this->checkSession();
  $this->layout = 'admin';
        if (!$id && empty($this->data)) {
   $this->Session->setFlash('Invalid page requested',
'flash_failure');
   $this->redirect('/admin/pages/index');
   exit();
        }
        if (!empty($this->data)) {
         if ($this->Page->save($this->data)) {
    $this->Session->setFlash('Changes saved', 'flash_success');
    $this->redirect('/admin/pages/index');
    exit();
         }else {
    $this->Session->setFlash('Unable to save changes.  Please correct
errors below', 'flash_failure');
   }
        }else {
   $pagelist = $this->Page->find('list', array('conditions' =>
array('visible' => 1, 'parent_id' => 0, 'title !=' => 'Home')));
   $this->set('pagelist', $pagelist);
         if (empty($this->data)) {
          $this->data = $this->Page->read(null, $id);
         }
  }
 }

 function admin_preview($id = null) {
  if (!empty($this->data)) {
   $this->Page->save($this->data);
   $page = $this->Page->read(null, $id);
  }else if (isset($id)) {
   $page = $this->Page->read(null, $id);
  }else {
   return null;
  }
  $this->set('page', $page);
  $this->render('view');

 }

}

IN VIEWS/PAGES/ADMIN_EDIT.PHP (I've left out a few divs, inputs etc.
to make it shorter):

<div class="pages form">
 <?php echo $form->create('Page');?>
 <fieldset>
 <?php
  echo $form->input('id');
  echo '<div class="input textarea">';
  echo $form->label('Content') . '<br/>';
  echo $form->input('content', array('type' => 'textarea', 'rows' =>
20, 'cols' => 50, 'label' => ''));
  echo $fck->load('Page/content', 'Cake');
  echo '</div>';

  echo '<input type="submit" value="Preview Page" id="preview_btn" /
>';
  echo '<input type="submit" value="Save Changes" />';
  echo '</form>';
?>
</fieldset>
</div>
<div id="page_modal"><div id="page_modal_content"></div></div>

<script language="text/javascript">
$('#preview_btn').click(function(e) {
  $(this).parents('form').ajaxSubmit({
    url: '/admin/pages/preview',
    error: function(XMLHttpRequest, textStatus, errorThrown)
{
     alert(textStatus);
    },
    success: function(responseText){
     $
('#page_modal').jqmShow().find('#page_modal_content').html(responseText);
    }
  });
  e.preventDefault();
});
</script>

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to