How do I move the company id from angular to php?

Asked

Viewed 63 times

2

Hello

I need to pass the angular company id to the php api. How do I do? Follows 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;
}

?>

2 answers

2


You can pass the id right in 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'];
  • But I am going to send the request by POST.

  • @Gustavosevero Yes, the form will be sent by POST normally. Putting the company id in the url as I said, it doesn’t change anything, just you will get it with GET in PHP.

0

Add the variable idempresa in his 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 the PHP you recover this value through the variable $_POST:

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

Browser other questions tagged

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