Insert variable in href in php with mysql

0

I am trying to pass the value of the Company_ID in my select from my href, but to no avail. Can someone give me an address please? 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 id of the company that's in this View. It is stated 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 it has 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>
    
asked by anonymous 06.12.2017 / 12:01

1 answer

0

Try something simpler, this mix of js html and php was a bit confusing. See the excerpt, I have adapted only the question stated in the statement, but of course you 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 the href with id in the page load and use variations of it to use with select . You do not 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: Do not forget that PHP is a Server Side language, that is,   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 it will always be good for your maintenance   and if another developer needs to tinker with your code. Another thing,   consider using jQuery, it will make your life a lot easier.

    
06.12.2017 / 12:21