Uncaught ReferenceError: url is not defined in the url line: url + 'remove_contact_company.php',

0
function cargaPagina(pagina)
    {
      var desde = pagina * itemsPorPagina;
      $.ajax({
        data:{"param1":"dame","limit":itemsPorPagina,"offset":desde},
        type:"GET",
        dataType:"json",
        url:"Conexion_Contacto.php"
      }).done(function(data,textStatus,jqXHR){

        var lista = data.lista;

        $("#miTabla").html("");

        $.each(lista, function(ind, elem){


          $("<tr>"+

            '<td>'+elem.nombre_contacto_empresa+'</td>'+
            '<td>'+elem.telefono_contacto_empresa+'</td>'+
            '<td>'+elem.correo_contacto_empresa+'</td>'+
            '<td>'+elem.razon_social_empresa+'</td>'+
            '<td data-idpersona="'+elem.id_contacto_empresa+'">' +
            '<button class="btn btn-primary modalEdicion"data-toggle="modal" data-target="#modalEdicion">EDITAR</button> '+
            '<button class="btn btn-danger remove-item">ELIMINAR</button>'+'</td>'+
            '</tr>').appendTo($("#miTabla"));

        });     


      }).fail(function(jqXHR,textStatus,textError){
        alert("Error al realizar la peticion dame".textError);

      });



    $("body").on("click",".remove-item",function(){

        confirm('Eliminar Datos de la bbdd , ¿Esta seguro de eliminar este registro?');
        var idpersona = $(this).parent("td").data('data-idpersona');
        var c_obj = $(this).parents("tr");

        $.ajax({
          dataType: "json",
          type:"POST",
          url: url + 'eliminar_contacto_empresa.php',
          data:{idpersona:idpersona}
        }).done(function(data){
          c_obj.remove();
          toastr.success('Item Deleted Successfully.', 'Success Alert', {timeOut: 5000});
          getPageData();
        });

      });


<?php

require 'Conectar_bbdd.php';

 $id = $_POST["data-idpersona"];

 echo "string".$id;


 $sql = "DELETE FROM contacto_empresa WHERE id_contacto_empresa =  '".$id."'";

 $result = $mysqli->query($sql);

 echo json_encode([$id]);

?>  
    
asked by Keane 15.09.2017 в 05:28
source

2 answers

0

You have not declared the variable url in the click event.

You must declare it so that it does not send you the error:

$("body").on("click",".remove-item",function(){

  //..

    var url = "https://www.mipagina.com";

    $.ajax({
      dataType: "json",
      type:"POST",
      url: url + 'eliminar_contacto_empresa.php',
      data:{idpersona:idpersona}
    }).done(function(data){
      c_obj.remove();
      toastr.success('Item Deleted Successfully.', 'Success Alert', {timeOut: 5000});
      getPageData();
    });

});

Or you eliminate the use of the undeclared variable:

$("body").on("click",".remove-item",function(){

    //..

    $.ajax({
          dataType: "json",
          type:"POST",
          url: '/eliminar_contacto_empresa.php',
          data:{idpersona:idpersona}
    }).done(function(data){
          c_obj.remove();
          toastr.success('Item Deleted Successfully.', 'Success Alert', {timeOut: 5000});
          getPageData();
    });

});

Remember that whenever you find the error undefined , it is because you are trying to access an object that was never declared.

    
answered by 15.09.2017 / 15:26
source
1

In the AJAX you are calling a PHP file which is preceded by a text string 'url', however I do not see that it is defined as var url="root / folder".

url: url + 'eliminar_contacto_empresa.php',

You can declare the url variable locally or globally:

var url = "dominio.com/";
...
url: url + 'eliminar_contacto_empresa.php',

Or, remove the url variable from your ajax parameter, and put everything within the same string that calls the PHP file:

url: 'eliminar_contacto_empresa.php',
    
answered by 15.09.2017 в 15:10