przesyłanie zmiennej z formularza do controllera/modelu
Wyniki 1 do 5 z 5

Temat: przesyłanie zmiennej z formularza do controllera/modelu

  1. #1
    Przeglądacz
    Dołączył
    14-06-2010
    Wpisy
    33
    Punkty
    11

    Domyślny przesyłanie zmiennej z formularza do controllera/modelu

    Hej,
    próbuję dodać nowe pole w komponencie (com_abook - Alexandria Book Library). Stworzyłem sobie w formularzu odpowiednie pola (dwa multiple selecty z przyciskami dodaj, usun) i teraz chciałbym zapisać do bazy dane z jednego z tych selectów.

    Problem mam jedynie z tym, że przy wysyłaniu formularza controller pomija nowo dodane pola w formularzu... Sprawdzałem zarówno _POST jak i JRequest::get('post'). Tych pól tam po prostu nie ma... I przez to nie mogę wysłać tablicy z wartościami do modelu.

    Czy to jest gdzieś filtrowane? Bo już nie mam pojęcia jak to rozwiązać.

  2. Pani Reklamowa
    Pani Reklamowa jest aktywna
    Avatar Panny Google

    Dołączył
    19-08-2010
    Skąd
    Internet
    Postów
    milion
    Pochwał
    setki
  3. #2
    Przeglądacz faketa awatar
    Dołączył
    09-06-2009
    Wpisy
    53
    Punkty
    10

    Domyślny

    pokaż kod gdzie dodałeś nowe pola i to jak dokładnie je sprawdzasz

  4. #3
    Przeglądacz
    Dołączył
    14-06-2010
    Wpisy
    33
    Punkty
    11

    Domyślny

    views\book\tmpl\form.php

    dodałem ten drugi wiersz tabeli na wzór tego wyżej.

    <tr>
    <td valign="top" align="right" class="key">
    <label for="idauth">
    <?php echo JText::_( 'AUTHORS' ); ?>:
    </label>
    </td>
    <td>
    <table><tr><td>
    <?php echo $this->lists['idauth']; ?>
    </td>
    <td>

    <input value="&lt;&lt;&lt; <?php echo JText::_( 'ADD' ); ?>" onclick="javascript: addSelectedToList( 'adminForm', 'author', 'idauth' );delSelectedFromList( 'adminForm', 'author' );" type="button">
    <br>
    <input value="<?php echo JText::_( 'REMOVE' ); ?> &gt;&gt;&gt;" onclick="javascript:addSelectedToList( 'adminForm', 'idauth', 'author' );delSelectedFromList( 'adminForm', 'idauth' );" type="button">

    </td>
    <td>
    <?php echo $this->lists['author']; ?>
    </td></tr></table>
    </td>
    </tr>

    <tr>
    <td valign="top" align="right" class="key">
    <label for="idbookbook">
    <?php echo JText::_( 'BOOKS' ); ?>:
    </label>
    </td>
    <td>
    <table><tr><td>
    <?php echo $this->lists['bookbook']; ?>
    </td>
    <td>

    <input value="&lt;&lt;&lt; <?php echo JText::_( 'ADD' ); ?>" onclick="javascript:addSelectedToList( 'adminForm', 'allbooks', 'idbookbook' );delSelectedFromList( 'adminForm', 'allbooks' );" type="button">
    <br>
    <input value="<?php echo JText::_( 'REMOVE' ); ?> &gt;&gt;&gt;" onclick="javascript:addSelectedToList( 'adminForm', 'idbookbook', 'allbooks' );delSelectedFromList( 'adminForm', 'idbookbook' );" type="button">

    </td>
    <td>
    <?php echo $this->lists['allbooks']; ?>
    </td></tr></table>
    </td>
    </tr>


    views\book\view.html.php

    <?php
    defined( '_JEXEC' ) or die( 'Restricted access' );

    jimport( 'joomla.application.component.view' );

    class AbookViewBook extends JView
    {
    function display($tpl = null)
    {
    global $mainframe, $option;
    jimport('joomla.html.pane');
    $document = & JFactory::getDocument();
    $document->addStyleSheet('components/'.$option.'/css/'.$option.'.css');
    $book =& $this->get('Data');
    $lists['rating'] =& $this->get('Rating');
    $isNew = ($book->id < 1);
    $user = JFactory::getUser();
    $pane = & JPane::getInstance('sliders');

    $text = $isNew ? JText::_( 'NEW' ) : JText::_( 'EDIT' );
    $icon=$isNew?"bookadd":"bookedit";
    JToolBarHelper::title( JText::_( 'BOOK' ).': <small><small>[ ' . $text.' ]</small></small>', $icon );
    JToolBarHelper::save();
    JToolBarHelper::apply();
    if ($isNew) {
    JToolBarHelper::cancel();
    } else {
    // for existing items the button is renamed `close`
    JToolBarHelper::cancel( 'cancel', 'Close' );
    }
    //tiro fuori i dati dalla funzione geteditorslist scritta nel file models/jbok.php. basta richiamare Editorslist che è il return della funzione
    $editorslist =& $this->get('Editorslist');

    //tiro fuori i dati dalla funzione getauthorslist scritta nel file models/jbok.php. basta richiamare Authorslist che è il return della funzione
    $authorslist =& $this->get('Authorslist');
    $locationslist =& $this->get('Locationslist');
    $librarieslist =& $this->get('Librarieslist');

    $booksauthorslist =& $this->get('Booksauthorslist');
    $cat =& $this->get('Categorieslist');
    $top[] = JHTML::_('select.option','0' , JText::_('SELECT A CATEGORY'), 'id', 'treename');
    $categorieslist = array_merge($top, $cat);



    $allbookslist =& $this->get('AllBookslist');
    $bookbooklist =& $this->get('BookBooklist');
    //var_dump($bookbooklist);exit();
    //var_dump($allbookslist);
    $lists['allbooks'] = JHTML::_('select.genericlist', $allbookslist, 'allbooks[]', 'class="inputbox" size="20" multiple="multiple"','id', 'title');
    $lists['bookbook'] = JHTML::_('select.genericlist', $bookbooklist, 'idbookbook[]', 'class="inputbox" size="20" multiple="multiple"','id', 'title');


    //Get global parameters
    $params =& JComponentHelper::getParams( 'com_abook' );
    $imageconfig=& JComponentHelper::getParams('com_media');
    $img['img_path'] = $params->get( 'img_path' );
    //tendina con le immagini
    $lists['images'] = JHTML::_('list.images', 'image', $book->image, '', '/images/'.$img['img_path'].'/');

    // $lists può contenere tutti gli array che vuoi
    $lists['path'] = $img['img_path'];
    JHTML::addIncludePath(JPATH_COMPONENT.DS.'helpers' );
    $lists['files'] = JHTML::_('files.files', 'file', $book->file, '', 'images'.DS.$params->get('file_path').DS);
    //aggiungo all'array la prima voce "seleziona un editore"
    $topeditor[] = JHTML::_('select.option','0' , JText::_('SELECT A EDITOR'), 'id', 'name');
    $editorslist=array_merge($topeditor, $editorslist);
    $lists['ideditor'] = JHTML::_('select.genericlist', $editorslist, 'ideditor', 'class="inputbox" size="1"','id', 'name', $book->ideditor );

    //tutti gli autori
    $lists['author'] = JHTML::_('select.genericlist', $authorslist, 'author', 'class="inputbox" size="20" multiple="multiple"','id', 'name');
    //autori selezionati
    $lists['idauth'] = JHTML::_('select.genericlist', $booksauthorslist, 'idauth[]', 'class="inputbox" size="20" multiple="multiple"','id', 'name');

    $toplocation[] = JHTML::_('select.option','0' , JText::_('SELECT A LOCATION'), 'id', 'location');
    $locationslist=array_merge($toplocation, $locationslist);
    $lists['location'] = JHTML::_('select.genericlist', $locationslist, 'idlocation', 'class="inputbox" size="1"','id', 'location', $book->idlocation);

    $toplibraries[] = JHTML::_('select.option','0' , JText::_('SELECT A LIBRARY'), 'id', 'name');
    $librarieslist=array_merge($toplibraries, $librarieslist);
    $lists['library'] = JHTML::_('select.genericlist', $librarieslist, 'idlibrary', 'class="inputbox" size="1"','id', 'name', $book->idlibrary);

    //questa ha l'indentazone delle voci figlie
    //$lists['catid'] = JHTML::_('select.genericlist', $categorieslist, 'catid', '','id', 'treename', $book->catid );
    $lists['catid'] = JHTML::_('select.genericlist', $categorieslist, 'catid[]', 'multiple="multiple"','id', 'treename', explode(',',$book->catid));

    $published = ($book->id) ? $book->published : 1;
    $lists['published'] = JHTML::_('select.booleanlist', 'published', 'class="inputbox"', $published );

    $lists['editedby'] = JHTML::_('select.booleanlist', 'editedby', 'class="inputbox"', $book->editedby );

    $which_user = $isNew ? $user->id : $book->user_id;
    $lists['user_id'] = JHTML::_('list.users', 'user_id', $which_user, 1, NULL, 'name', 0 );

    $lists['access'] = JHTML::_('list.accesslevel', $book);

    JHTML::_('behavior.tooltip');

    $lists['credit']=JHTML::_('credit.credit');

    $this->assignRef( 'session', JFactory::getSession());
    $this->assignRef( 'params', $params);
    $this->assignRef( 'imageconfig', $imageconfig);
    $this->assignRef('book', $book);
    $this->assignRef('lists', $lists);
    $this->assignRef('pane', $pane);
    //passo la palla al template view/book/tpml/form.php
    parent::display($tpl);
    }
    }

    controllers\book.php

    <?php
    defined( '_JEXEC' ) or die( 'Restricted access' );

    class AbookControllerBook extends AbookController
    {
    function __construct()
    {
    //echo "controller: book; action: construct<br>";var_dump($_POST);var_dump(JRequest: :get('post'));exit();
    parent::__construct();

    // Register Extra tasks
    $this->registerTask( 'add' , 'edit' );
    $this->registerTask( 'apply', 'save' );
    $this->registerTask( 'accesspublic', 'access' );
    $this->registerTask( 'accessregistered','access' );
    $this->registerTask( 'accessspecial', 'access' );
    $this->registerTask( 'resethits', 'save' );
    $this->registerTask( 'resetrates', 'resetrates' );
    }

    function display()
    {
    //echo "controller: book; action: display<br>";var_dump($_POST);var_dump(JRequest::g et('post'));exit();
    global $mainframe, $option;
    JRequest::setVar( 'view', 'books' );
    parent::display();
    }

    function edit()
    {
    //echo "controller: book; action: edit<br>";var_dump($_POST);var_dump(JRequest::get( 'post'));exit();
    JRequest::setVar( 'view', 'book' );
    JRequest::setVar( 'layout', 'form' );
    JRequest::setVar('hidemainmenu', 1);
    // Checkout
    $model = $this->getModel('book');
    $model->checkout();

    parent::display();
    }

    function save()
    {
    //echo "controllers: book; action: save<br>";var_dump($_POST);var_dump(JRequest::get( 'post'));exit();

    JRequest::checkToken() or die( 'Invalid Token' );
    global $mainframe;
    $model = $this->getModel('book');
    $post = JRequest::get('post');
    $task = JRequest::getCmd( 'task' );
    //aggiungo anche l'id che è uguale al cid, perchè come campo hidden del form devo mettere per forza cid sennò non funziona il checkin
    $post['id'] = JRequest::getVar( 'cid', '', 'post', 'int' );
    $post['html'] = JRequest::getVar('html', '', 'post', 'string', JREQUEST_ALLOWRAW);

    $catid= JRequest::getVar('catid', array(), 'post', 'array');
    if (is_array( $catid)) {
    $post['catid'] = implode( ",", $catid);
    }

    $idbookbook= JRequest::getVar('bookbook', array(), 'post', 'array', JREQUEST_ALLOWRAW);
    //var_dump($idbookbook);exit();
    //var_dump($_POST);exit();

    if ($task=='resethits'){
    $post['hits'] = 0;
    }
    if ($returnid = $model->store($post)) {
    $model->storeauthor($returnid, $post['idauth']);
    $model->storebookbook($returnid, $post['idbookbook']);

    switch ($task){
    case 'resethits':
    case 'apply':
    $link = 'index.php?option=com_abook&controller=book&task=e dit&cid[]='. $returnid ;
    $msg .= JText::_( 'SUCCESSFULLY APPLY CHANGES TO BOOK' );
    break;
    case 'save':
    default:
    $link = 'index.php?option=com_abook&controller=book';
    $msg .= JText::_( 'SUCCESSFULLY SAVED CHANGES TO BOOK' );
    break;
    }
    } else {
    $msg = JText::_( 'AN ERROR HAS OCCURRED' );
    }

    $model->checkin();

    //var_dump($_POST);exit();

    $this->setRedirect($link, $msg);
    }

    function remove()
    {
    //echo "controller: book; action: remove<br>";var_dump($_POST);var_dump(JRequest::ge t('post'));exit();
    $model = $this->getModel('book');
    if(!$model->delete()) {
    $msg = JText::_( 'ONE OR MORE ITEMS COULD NOT BE DELETED' );
    } else {
    $msg = JText::_( 'ITEM(S) DELETED' );
    }

    $this->setRedirect( 'index.php?option=com_abook&controller=book', $msg );
    }

    function cancel()
    {
    //echo "controller: book; action: cancel<br>";var_dump($_POST);var_dump(JRequest::ge t('post'));exit();
    //sblocca il lucchetto
    $model = $this->getModel('book');
    $model->checkin();
    $msg = JText::_( 'OPERATION CANCELLED' );
    $this->setRedirect( 'index.php?option=com_abook&controller=book', $msg );
    }

    function access( )
    {
    //echo "controller: book; action: access<br>";var_dump($_POST);var_dump(JRequest::ge t('post'));exit();
    global $option;

    $cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
    $id = $cid[0];
    $task = JRequest::getVar( 'task' );

    if ($task == 'accesspublic') {
    $access = 0;
    } elseif ($task == 'accessregistered') {
    $access = 1;
    } else {
    $access = 2;
    }

    $model = $this->getModel('book');
    $model->access( $id, $access );

    $this->setRedirect('index.php?option='. $option .'&controller=book' );
    }

    function publish()
    {
    //echo "controller: book; action: publish<br>";var_dump($_POST);var_dump(JRequest::g et('post'));exit();
    $cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );

    if (!is_array( $cid ) || count( $cid ) < 1) {
    JError::raiseError(500, JText::_( 'SELECT AN ITEM TO PUBLISHED' ) );
    }

    $model = $this->getModel('books');
    if(!$model->publish($cid, 1)) {
    //echo "********** alert('".$model->getError()."'); window.history.go(-1); </script>\n";
    }

    $total = count( $cid );
    $msg = $total.' '.JText::_('BOOK PUBLISHED');

    $this->setRedirect( 'index.php?option=com_abook&controller=book', $msg );
    }

    function unpublish()
    {
    $cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );

    if (!is_array( $cid ) || count( $cid ) < 1) {
    JError::raiseError(500, JText::_( 'SELECT AN ITEM TO UNPUBLISHED' ) );
    }

    $model = $this->getModel('books');
    if(!$model->publish($cid, 0)) {
    //echo "********** alert('".$model->getError()."'); window.history.go(-1); </script>\n";
    }

    $total = count( $cid );
    $msg = $total.' '.JText::_('BOOK UNPUBLISHED');

    $this->setRedirect( 'index.php?option=com_abook&controller=book', $msg );
    }

    function resetrates()
    {
    $id = JRequest::getVar( 'cid', '', 'post', 'int' );
    $model = $this->getModel('book');
    if(!$model->reset_rates($id, 0)) {
    //echo "********** alert('".$model->getError()."'); window.history.go(-1); </script>\n";
    }

    $msg = JText::_('SUCCESSFULLY RESET RATES');

    $this->setRedirect( 'index.php?option=com_abook&controller=book&task=e dit&cid[]='.$id, $msg );
    }

    function orderup()
    {
    $model = $this->getModel('books');
    $model->move(-1);
    $this->setRedirect( 'index.php?option=com_abook&controller=book');
    }

    function orderdown()
    {
    $model = $this->getModel('books');
    $model->move(1);

    $this->setRedirect( 'index.php?option=com_abook&controller=book');
    }

    function saveorder()
    {
    $cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
    $order = JRequest::getVar( 'order', array(0), 'post', 'array' );
    JArrayHelper::toInteger($order, array(0));

    $model = $this->getModel('books');
    $model->saveorder($cid, $order);

    $msg = JText::_( 'NEW ORDERING SAVED');
    $this->setRedirect( 'index.php?option=com_abook&controller=book', $msg );
    }
    }

    tables\bookbook.php
    defined( '_JEXEC' ) or die( 'Restricted access' );

    class TableBookbook extends JTable
    {
    var $aid = null;
    var $idbook = null;
    var $idbookbook = null;

    function TableBookbook(& $db) {
    //echo "tables: bookbook<br>";var_dump($_POST);var_dump(JRequest:: get('post'));exit();
    parent::__construct('#__abbookbook', 'aid', $db);
    }
    }


    tables\bookbook2.php
    defined( '_JEXEC' ) or die( 'Restricted access' );

    class TableBookbook2 extends JTable
    {
    var $idbook = null;
    var $idbookbook = null;


    function TableBookbook2(& $db) {
    //echo "tables: bookbook2<br>";var_dump($_POST);var_dump(JRequest: :get('post'));exit();
    parent::__construct('#__abbookbook', 'idbook', $db);
    }
    }


    Wszystko co tu robiłem to było na wzór multiple selectów idauth i author. Niestety w kontrolerze nie pojawiają się te nowe selecty. Gdzieś czegoś nie dodałem. Ale nie mam już pomysłu czego i gdzie.

  5. #4
    Przeglądacz faketa awatar
    Dołączył
    09-06-2009
    Wpisy
    53
    Punkty
    10

    Domyślny

    W widoku nie ma żadnego formularza, i zapewne to jedynie część widoku. Być może przyda się także pokazać js, którego używają buttony.

  6. #5
    Przeglądacz
    Dołączył
    14-06-2010
    Wpisy
    33
    Punkty
    11

    Domyślny

    Dzięki za zainteresowanie tematem.

    Jak wspomniałeś o JS to zrozumiałem, że multiple select nie wyśle żadnych danych jeśli nie zostaną zaznaczone.
    W \views\book\tmpl\form.php wystarczyło dopisać do funkcji JS 'submitbutton' polecenie:
    allSelected(document.adminForm['idbookbook[]']);

    Cała reszta kodu była poprawna. Ech ;) Taka drobnostka, a kombinowałem 3 dni.
    Dzięki jeszcze raz.

    [solved]

Podobne tematy

  1. Wysłanie zmiennej przez link do formularza kontaktu
    przez mwaloos na forum Programowanie pod Joomla!
    Odpowiedzi: 1
    Ostatni post/autor: 13-10-2011, 21:15
  2. przesyłanie plików
    przez ziemko1 na forum Administracja - ogólne
    Odpowiedzi: 2
    Ostatni post/autor: 25-03-2009, 14:33
  3. Przesyłanie danych z formularza i porównanie ich z danymi z bazy MYSQL.
    przez krzysztof_z na forum Rozszerzenia - problemy z obsługą, zarządzaniem
    Odpowiedzi: 0
    Ostatni post/autor: 24-11-2008, 14:07
  4. Przesył zmiennej do index.php
    przez Subey na forum Programowanie pod Joomla!
    Odpowiedzi: 3
    Ostatni post/autor: 15-05-2007, 20:14
  5. przesyłanie plików ......... błąd
    przez fotografix na forum Administracja składnikami
    Odpowiedzi: 1
    Ostatni post/autor: 02-02-2007, 00:19

Reguły pisania

  • Nie możesz zakładać nowych tematów
  • Nie możesz dodawać wypowiedzi
  • Nie możesz dodawać załączników
  • Nie możesz poprawiać swoich postów
  •