Two forms on one page

Asked

Viewed 1,388 times

1

Good guys I have a view with two forms and both possess action="current_url()", i.e., the two will perform the action on the same page, the problem is that regardless of the button I click to send the form it always runs the two generating the same error messages, as if it recognizes only one form, wanted to know if you have any way to use two different forms and run them on the same page/method

<?php 
if (validation_errors() != '') {
        $mostra_box_msg = 'show';
    }
?>
<script type="text/javascript">
   $(document).ready(function() {
        $("#box-msg").<?php echo $mostra_box_msg ?>();
        $("#abre_resposta").click(function(){
            $("#box-msg").toggle("slow")
       });
   });
</script>

<h3>Pergunta:</h3>
<?php 
    echo "<p>".anchor("area_tecnico", "<i class='icon-circle-arrow-left'></i> Voltar", array("class"=>"btn btn-small"))."</p>"; 
    if ($this->session->flashdata('msgok') != "")   echo '<p class="text-success">'.$this->session->flashdata('msgok').'</p>';
?>

<blockquote>
  <p>Dados Gerais</p>
</blockquote>
<?php 
    $seguimento     =   $this->uri->segment(3);
    $dados  =   $this->db->query("SELECT 
i_chamada
, titulo
, DATE_FORMAT(dt_abertura, '%d/%m/%Y %H:%i') AS data_abertura
, DATE_FORMAT(dt_prevista, '%d/%m/%Y %H:%i') AS data_prevista
, DATE_FORMAT(dt_encerramento, '%d/%m/%Y %H:%i') AS data_encerramento
, status
, (SELECT nome FROM tecnicos AS z WHERE z.i_tecnico=a.i_tecnico) AS nome_tecnico
, (SELECT nome FROM areas AS z WHERE z.i_area=a.i_area) AS nome_area
FROM chamados AS a
WHERE i_chamada = $seguimento")->result_array();

    if ($dados[0]['status'] == 'N') {
        $status = 'Novo';
        $type   = 'info';
    } elseif ($dados[0]['status'] == 'E') {
        $status = 'Em Andamento';
        $type   = '';
    } elseif ($dados[0]['status'] == 'R') {
        $status = 'Resolvido';
        $type   = 'success';
    } elseif ($dados[0]['status'] == 'C') {
        $status = 'Cancelado';
        $type   = 'error';
    } else {
        $status = 'Aguardando Usuário';
        $type   = 'warning';
    }
?>
<table class="table">
    <thead>
    <tr>
        <th width="25%" class="text-center">Número de Identificação</th>
        <th width="25%" class="text-center">Técnico</th>
        <th width="25%" class="text-center">Status</th>
        <th width="25%" class="text-center">Área</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td class="text-center"><?php echo $dados[0]['i_chamada'] ?></td>
        <td class="text-center"><?php echo ($dados[0]['nome_tecnico'] == NULL ? '<span class="muted">Aguardando Resposta</span>' : $dados[0]['nome_tecnico']) ?></td>
        <td class="text-center"><span class="label label-<?php echo ($type == 'error') ? 'important' :  $type ?>"><?php echo $status ?></span></td>
        <td class="text-center"><?php echo $dados[0]['nome_area'] ?></td>
    </tr>
    </tbody>
</table>

<table class="table">
    <thead>
    <tr>
        <th width="25%" class="text-center">Assunto</th>
        <th width="25%" class="text-center">Data Abertura</th>
        <th width="25%" class="text-center">Data Prevista</th>
        <th width="25%" class="text-center">Data Encerramento</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td class="text-center"><?php echo $dados[0]['titulo'] ?></td>
        <td class="text-center"><?php echo $dados[0]['data_abertura'] ?></td>
        <td class="text-center"><?php echo ($dados[0]['data_prevista'] == NULL) ? '<a href="#ModalData" data-toggle="modal"><i class="icon-edit"></i></a>' : $dados[0]['data_prevista'] ?> </td>
        <td class="text-center"><?php echo ($dados[0]['data_encerramento'] == NULL) ? '<a href="#ModalData" data-toggle="modal"><i class="icon-edit"></i></a>' : $dados[0]['data_encerramento'] ?></td>
    </tr>
    </tbody>
</table>

<br>
<br>
<blockquote>
  <p>Mensagens</p>
</blockquote>

<table class="table table-bordered">
    <thead>
    <tr>
        <th width="15%" class="text-center">Enviado Por</th>
        <th width="50%" class="text-center">Mensagem</th>
        <th width="10%" class="text-center">Data e Hora</th>
        <th width="5%" class="text-center">Código</th>
    </tr>
    </thead>

    <tbody>

<?php 

    $interacoes = $this->db->query("SELECT
i_interacao
, DATE_FORMAT(dt_interacao, '%d/%m/%Y %H:%i') AS data_interacao
, descricao
, i_tecnico
, status
, (SELECT nome FROM tecnicos AS z WHERE z.i_tecnico=a.i_tecnico) AS nome_tecnico
, (SELECT nome FROM clientes AS z WHERE z.i_cliente=a.i_cliente) AS nome_cliente
FROM interacoes AS a
WHERE i_chamada = $seguimento")->result_array();

    foreach ($interacoes as $key) :

    if ($key['status'] == 'N') {
        //$status = 'Novo';
        $type   = 'info';
    } elseif ($key['status'] == 'E') {
        //$status = 'Em Andamento';
        $type   = '';
    } elseif ($key['status'] == 'R') {
        //$status = 'Resolvido';
        $type   = 'success';
    } elseif ($key['status'] == 'C') {
        //$status = 'Cancelado';
        $type   = 'error';
    } else {
        //$status = 'Aguardando Usuário';
        $type   = 'warning';
    }

    if ($key['i_tecnico'] == NULL) {
        echo '
            <tr>
                <td class="text-center">'.$key['nome_cliente'].'</td>
                <td class="text-center">'.$key['descricao'].'</td>
                <td class="text-center">'.$key['data_interacao'].'</td>
                <td class="text-center">#'.$key['i_interacao'].'</td>
            </tr>
        ';
    } else {
        echo '
            <tr class="'.$type.'">
                <td class="text-center"><b>'.$key['nome_tecnico'].'</b></td>
                <td class="text-center">'.$key['descricao'].'</td>
                <td class="text-center">'.$key['data_interacao'].'</td>
                <td class="text-center">#'.$key['i_interacao'].'</td>
            </tr>
        ';
    }

    endforeach;


?>

    </tbody>
</table>

<div style="clear: both; margin-bottom: 5px"></div>
<a id="abre_resposta" class="pull-right btn btn-mini btn btn-info"><i class="icon-plus icon-white"></i> Nova Resposta</a>
<br>
<br>
<div style="width:62%; margin:0 auto" id="box-msg">
<?php echo form_open(current_url(), 'class="form-horizontal"'); ?>
  <div class="control-group">
    <div class="controls">
        <?php 
            echo form_error('descricao', '<p class="text-error">', '</p>');
        ?>
    </div>
  </div>

  <div class="control-group">
    <label class="control-label" for="inputEmail">Status</label>
    <div class="controls">
        <select name="status">
            <option value="" selected="selected" disabled="disabled">Selecione um Status</option>
            <option value="N">Novo</option>
            <option value="E">Em Andamento</option>
            <option value="R">Resolvido</option>
            <option value="C">Cancelado</option>
            <option value="P">Pendente</option>
        </select>
    </div>
  </div>

  <div class="control-group">
    <label class="control-label" for="inputEmail">Mensagem</label>
    <div class="controls">
      <?php echo form_textarea(array('name'=>'descricao', 'value'=>set_value('descricao'), 'class'=>'input-xxlarge', 'rows'=>'2')); ?>
    </div>
  </div>

  <div class="control-group">
    <label class="control-label" for="inputEmail">Observação</label>
    <div class="controls">
      <?php echo form_textarea(array('name'=>'tecnico', 'value'=>set_value('tecnico'), 'class'=>'input-xxlarge', 'rows'=>'2')); ?>
    </div>
  </div>
  <div class="control-group">
    <div class="controls">
      <button type="submit" class="btn btn-primary">Enviar Resposta</button>
    </div>
  </div>
<?php echo form_close(); ?>
</div>

<!-- Modal -->

<?php 
    $hidden = array(
        'seguimento_hidden' => $this->uri->segment(3)
    );
?>

<div id="ModalData" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Alteração de Datas</h3>
  </div>
  <div class="modal-body">
      <?php 
        echo form_open(current_url(), 'class="form-horizontal"', $hidden);
      ?>
          <div class="control-group">
            <label class="control-label" for="inputEmail">Data e Hora Prevista</label>
            <div class="controls">
              <?php echo form_input(array('name'=>'dt_prevista', 'value'=>set_value('dt_prevista'), 'class'=>'date-time-mask')); ?>
            </div>
          </div>

          <div class="control-group">
            <label class="control-label" for="inputEmail">Data e Hora Encerrada</label>
            <div class="controls">
              <?php echo form_input(array('name'=>'dt_encerramento', 'value'=>set_value('dt_encerramento'), 'class'=>'date-time-mask')); ?>
            </div>
          </div>
  </div>
  <div class="modal-footer">
    <button class="btn btn-primary">Salvar Dados</button>
  </div>
    <?php echo form_close(); ?>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $('#ModalData').modal('show')
    });
</script>

The second form is inside a modal of bootstrap where I will only be used to update two database dates

  • 1

    Put the view code there to better understand the problem!

  • I’ve already updated and put the code there

  • Your code is very disorganized and outside of Codeigniter’s MVC approach. What errors appear?

  • there is no error, the question is how I use two distinct forms in a single controller method

  • @Jhonatanoliveira actually the code seems to be right, but he put it all together so we could see on the same page... at least it seems to be this rsrs

  • yes, actually the modal is off the page but both forms use the same controller and the same method to validate the fields and perform the actions

  • 2

    This merging was not clear to me, so the comment was out of the way. It would be interesting for each form to have a "name", maybe that’s why it’s running both Forms. Ah, and the second button lacks the "Submit" in its code @Japa

  • OK thanks, more how I use the name in the form and recover them in the controller to use a form_validation for each form?

  • 1
    • Checking that the sent form is not empty $this->input->post('form'); Or the fields individually; This example is legal: http://bavotasan.com/2009/processing-multiple-forms-on-one-page-with-php/
  • 1

    I think this example will serve exactly what I need!

Show 5 more comments

1 answer

2


I was able to solve as follows, I created an Hidden field in each form with the same name and different value for the identification in the controller

<input type="hidden" name="acao_form" value="cad-resposta" style="display:none;" />
<input type="hidden" name="acao_form" value="cad-datas" style="display:none;" />

and Validei controller which value was returned in Submit

  $acao_form = $this->input->post('acao_form');

  if ($acao_form == 'cad-resposta') {
     //cadastra uma coisa       
  }

  if ($acao_form == 'cad-datas') {
     //cadastra outra           
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.