sergiogomesdev
sergiogomesdev

Reputation: 115

How to pass multiple values from view to controller in PHP

I'm new in PHP and I'm trying to pass 2 integers to from my view to my controller. I'm using zend framework 2.

This is the way I was trying to pass the data to my action in controller through a button, but the values that I receive in my action are equal to 0, so it's wrong because I have values that not equal to 0.

   <td>
        <?php $url = $this->url('mediador', array('action' => 'associaCorrespondencia', 'idCompra' => $Compra[$i]['ID'], 'idVenda' => $Venda[$i]['ID'])); ?>
        <form action="<?php echo $url; ?>" method="post">
            <div>
                <input type="submit" name="del" value="Associar" />
            </div>
        </form>
   <td>

Here is how I receive the values in my action :

public function associaCorrespondenciaAction(){

    $idCompra = $this->params()->fromRoute('idCompra', 0);
    $idVenda = $this->params()->fromRoute('idVenda ', 0);

}

What Am I doing wrong? The values that I receive are ever equal to zero, and are not the same ones I went through parameter. Thanks in advance!

EDIT: My route config:

<?php

return array(
    'controllers' => array(
        'invokables' => array(
            'Mediador\Controller\Mediador' => 'Mediador\Controller\MediadorController',
        ),
    ),


   // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'mediador' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/mediador[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Mediador\Controller\Mediador',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'Mediador' => __DIR__ . '/../view',
        ),
    ),
);

Upvotes: 3

Views: 1062

Answers (1)

Progrock
Progrock

Reputation: 7485

You are using a the url zend view helper:

http://framework.zend.com/manual/current/en/modules/zend.view.helpers.url.html

To generate query string arguments from the view helper, you need to assign them as the third argument using the query key like this:

View url generation:

<?php
$url = $this->url(
    'mediador',
    array(
         'action' => 'associaCorrespondencia'
    ),
    array(
        'query' => array(
            'idCompra' => $Compra[$i]['ID'], 
            'idVenda' => $Venda[$i]['ID']
        )
    )
);
?>

Action:

$idCompra = $this->params()->fromQuery('idCompra', 0);
....

Upvotes: 5

Related Questions