Unsuccessful Ajax request

2
Hello, I am performing a request in Ajax, and there is an error in which the value of the line code clicked (in a table) for Ajax is not passed, but I have another example that works perfectly and that is with error. I believe that the possible error is because when the user clicks on the link, in theory the system should update in the table and then send to view the PDF, but it must be conflicting internally

Page code where the request is made:

<td class="hidden-480">
  <?php $cod_publicacao = $row['cod_publicacao']; ?>
    <a data-id="<?php echo $row['cod_publicacao']; ?>" id="updateVisualization">
      <?php $arquivo = $row['arquivo']; 
        echo"<a href='upload/publicacoes/{$razao_social}/{$tipo}/{$titulo}/{$ano}/{$arquivo}'>
          <i class='ace-icon fa fa-eye bigger-110 hidden-480'></i>&nbsp;Visualizar Arquivo</a>";
?></a>
</td>

Request Code:

$(document).ready(function(){

$(document).on('click', '#updateVisualization', function(e){

    e.preventDefault();

    var uid = $(this).data('id');   // it will get id of clicked row

    $.ajax({
        url: 'updateVisualization.php',
        type: 'POST',
        data: 'id='+uid,
        dataType: 'html'
    })      
    });

});

PHP page:

<?php
    include "conexao.php";
    $pdo= conectar();

    if (isset($_REQUEST['id'])) {
        try{    
        $codigo = intval($_REQUEST['id']);
        $SQL = "UPDATE tbl_publicacao SET status = S WHERE cod_publicacao = ?";
        $stmt = $pdo->prepare( $SQL );
        $stmt->bindValue(1, $cod_publicacao, PDO::PARAM_INT);
        $stmt->execute(array(':codigo'=>$codigo));

    }catch(PDOException $e){
        'ERROR :' . $e->getMessage()."<br>";
        'ERROR :' . $e->getCode();
    }}
?>

Update: According to a suggestion, I "put" the codes in a single <a> tag, however now when clicking, nothing happens, neither opens the pdf nor updates in the database, it follows the new code <td> :

<td class="hidden-480">
<?php $cod_publicacao = $row['cod_publicacao']; 
$arquivo = $row['arquivo']; 
echo"<a href='upload/publicacoes/{$razao_social}/{$tipo}/{$titulo}/{$ano}/{$arquivo}' id='updateVisualization' data-id='$cod_publicacao'>
<i class='ace-icon fa fa-eye bigger-110 hidden-480'></i>&nbsp;Visualizar Arquivo</a>";
?>
</td>
    
asked by anonymous 21.03.2017 / 13:52

1 answer

1

First you are using a a tag inside another a tag, this is not allowed, and therefore you should be generating your HTML in the wrong way. The ideal is to join these 2 elements into one.

<td class="hidden-480">
<?php $cod_publicacao = $row['cod_publicacao']; 
$arquivo = $row['arquivo']; 
echo"<a href='upload/publicacoes/{$razao_social}/{$tipo}/{$titulo}/{$ano}/{$arquivo}' id='updateVisualization' data-id='$cod_publicacao'>
<i class='ace-icon fa fa-eye bigger-110 hidden-480'></i>&nbsp;Visualizar Arquivo</a>";
?>
</td>

In addition your ajax is wrong, you are passing the date in the wrong way, the correct way is the one below:

$.ajax({
    url: 'updateVisualization.php',
    type: 'POST',
    data: {id: uid},
    dataType: 'html'
});

In addition, since you are overwriting the click of your element, it will no longer open a new page, to get around that we will get the value of href and with js open the page.

$(document).on('click', '#updateVisualization', function(e){

    e.preventDefault();

    var uid = $(this).data('id');   // it will get id of clicked row
    var href = $(this).attr('href');

    $.ajax({
        url: 'updateVisualization.php',
        type: 'POST',
        data: {id: uid},
        dataType: 'html'
    }).done(function() {
        window.location.href = href;
    });      
});

You can check that I have taken the href attribute and saved it in a variable and after finishing ajax I reload the page with that value

    
23.03.2017 / 15:13