Insert variable in href in php with mysql

Asked

Viewed 290 times

0

I am trying to pass on the value of Id_company in my href select, but without success. Can someone please direct me? This is my href:

<div class="box-footer clearfix">
          <a href="<?=site_url('dashboard/financeiros/excelFinanceiro?empresa=[document.getElementById("empresa").value]')?>" class="btn btn-sm bg-blackzag btn-flat pull-left">Gerar Excel</a>
        </div>

I’m trying to capture the company id that’s on this view. He is declared in some points.

<h4>FILTRAR:</h4>
             </div>                         
        <div class="col-lg-3 col-xs-12">                         
        <div class="form-group">
            <select class="form-control select2" id="empresa" style="width: 100%;" onchange="funcFiltra(1);">
              <option value="0" selected="selected">Empresa</option>
              <?php foreach($empresas as $v):?>
                  <option value="<?=$v->getEmpresasId()?>"><?=$v->getNome()?></option>
              <?php endforeach;?> 
              <option value="0">Todos</option>
            </select>
          </div>
        </div>

And there’s this Function that makes the filter:

<script>

funcCarregaCombo();

function funcCarregaCombo() {
    document.getElementById("empresa").value = <?php echo $_SESSION['ID_EMPRESA']; ?>;
}

function funcFiltra(opcao) {
var idItem = opcao;
var idIndex = "";

switch(opcao) {
  case 1:
      idIndex = document.getElementById("empresa").value;
    break;

  case 2:
      idIndex = document.getElementById("data").selectedIndex;

      // Selecionado TODOS - ComboBox Tempo
      if (idIndex == 0) {
        location.href = site_url+'dashboard/financeiros/relatorio/';
      }
    break;

  default:
    break;
}

//alert("idItem = " + idItem);
//alert("idIndex = " + idIndex);

if ((idItem != null) || (idItem != "")) {
  location.href = site_url+'dashboard/financeiros/filtro/'+idItem+'/'+idIndex;
}
}
</script>
  • I don’t understand, id will be passed on href is this?

  • That. When I do so with fixed value it works: <a href="<?= site_url('Dashboard/financial/excelFinanceiro???' company=12')? >" class="btn btn-Sm bg-blackzag btn-flat pull-left">Generate Excel</a> .

1 answer

0

Try something simpler, got a little confused this mixture of js html and php. See the excerpt, I adapted only the question stated in the statement, but of course can and will adapt...

<a href="dashboard/financeiros/excelFinanceiro?empresa=<?php echo $_SESSION['ID_EMPRESA']; ?>" id="link_empresa" class="btn btn-sm bg-blackzag btn-flat pull-left">Gerar Excel</a>
...
<select class="form-control select2" id="empresa" style="width: 100%;" onchange="setLink(this)">
    <option value="0">Empresa</option>
    <?php foreach($empresas as $v):?>
        <option <?php echo ($_SESSION['ID_EMPRESA'] == $v->getEmpresasId()) ? "selected" : ""; ?> value="<?=$v->getEmpresasId()?>" > <?=$v->getNome()?> </option>
    <?php endforeach;?> 
    <option value="0">Todos</option>
</select>
...
<script>
function setLink(selectObject) {
    var link = "dashboard/financeiros/excelFinanceiro?empresa="+selectObject.value;  
    document.getElementById("link_empresa").href = link; 
}
</script>

Example 2: You can set href with id in the page load and use variations of it to use with select. No need to exclude the other functions you already have, test include and change according to the examples.

window.onload = function () {
    var link = "dashboard/financeiros/excelFinanceiro?empresa=<?php echo $_SESSION['ID_EMPRESA']; ?>";  
    document.getElementById("link_empresa").href = link; 
}

Note: Don’t forget that PHP is a Server Side language, that is, it runs on the server side, processes the data and sends it formatted to the browser. JS is a Client Side language, running on the client side and giving more dynamism to the application. It’s great to make them work together, but try to simplify will always be good for your maintenance and if another developer needs to tweak your code. Something else, consider using jQuery, it will make your life a lot easier.

  • I tried to do it that way, but you made a mistake at another point in my Financial Controller. Can I create a variable in the <script>, pass the company id and call in the current href, without touching the structure of the rest of the code?

  • You can, but it will keep the confusion playing values from one corner to the other, I will edit the answer with an example

Browser other questions tagged

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