How to pass the business id from angular to php?

2

Hello

I need to pass the business id from angular to api in php. How do I do? Here is the code:

Angular:

app.controller('UploadCtrl', ['$scope', '$window', 'Upload', function($scope, $window, Upload) {

var formData = new FormData();
var idempresa = localStorage.getItem('idempresa');
// Attach file

$scope.uploadExcel = function(){
  $scope.input.click();
}

$scope.input = document.createElement("INPUT");
$scope.input.setAttribute("type", "file");
$scope.input.addEventListener('change', function(){
  formData.append('file_xls', $scope.input.files[0]);

    $.ajax({
      url: 'http://localhost:8888/sistemas/webApps/fluxo_de_caixa/fluxojoin_2.0/php/importaArquivo.php',
      data: formData,
      type: 'POST',
      contentType: false,
      processData: false,
  });
});

}]);

PHP:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

header("Access-Control-Allow-Methods", "POST, PUT, OPTIONS");
header("Access-Control-Allow-Origin", "*.*");
header("Access-Control-Allow-Headers", "Content-Type");

include_once("con.php");

$pdo = conectar();

include_once("PHPExcel/Classes/PHPExcel.php");

$uploadDir = "uploadFile/";

$uploadfile = $uploadDir . $_FILES['file_xls']['name'];

if(move_uploaded_file($_FILES['file_xls']['tmp_name'], $uploadfile)) {
  echo "<br>";
  echo "Dados pegos com sucesso.";
  echo "<br><br>";
}else{
  echo "Não foi possível pegar arquivo";
  echo "<br><br>";
}

$pegaDados=$pdo->prepare("SELECT * FROM dadosImportados");
$pegaDados->execute();
$qtd = $pegaDados->rowCount();

if($qtd > 0){ // se tiver dados no banco, apaga tudo
  $qryZera=$pdo->prepare("DELETE FROM dadosImportados");
  $qryZera->execute();
}

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($uploadfile);

$sheet = $objPHPExcel->getActiveSheet();

foreach ($sheet->rangeToArray($sheet->calculateWorksheetDataDimension()) as 
$row) {
  echo $row[0]." ".$row[1]." ".$row[2]." ".$row[3]."<br>";
  $qryInsert=$pdo->prepare("INSERT INTO dadosImportados 
  VALUES(NULL,'$id_empresa','$row[0]','$row[1]','$row[2]','$row[3]')");
  $qryInsert->execute;
}

?>
    
asked by anonymous 18.12.2017 / 13:58

2 answers

2

You can pass id direct to the URL:

url: 'http://localhost:8888/sistemas/webApps/fluxo_de_caixa/fluxojoin_2.0/php/importaArquivo.php?idempresa='+idempresa,

And recover in PHP with $_GET :

$_GET['idempresa'];
    
18.12.2017 / 14:10
0

Add the variable idempresa to your formData :

$scope.input = document.createElement("INPUT");
$scope.input.setAttribute("type", "file");
$scope.input.addEventListener('change', function(){
  formData.append('file_xls', $scope.input.files[0]);
  formData.append('idempresa', idempresa);

    $.ajax({
      url: 'http://localhost:8888/sistemas/webApps/fluxo_de_caixa/fluxojoin_2.0/php/importaArquivo.php',
      data: formData,
      type: 'POST',
      contentType: false,
      processData: false,
  });
});

And in PHP you retrieve this value through the variable $_POST :

<?
    $idempresa = $_POST["idempresa"];
    echo "Id da empresa: ".$idempresa;
?>
    
18.12.2017 / 14:06