Request with Ajax unsuccessful

Asked

Viewed 141 times

2

Hello, I am making a request in Ajax, and there is an error where the value of the code of the line clicked (in a table) is not passed to Ajax, but I have another example that works perfectly and this is in 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 PDF view, but must be giving conflict 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>

Requisition 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 suggestion I "joined" the codes in a single tag <a> however now when clicking, nothing happens, neither opens the pdf nor updates in the bank, follows the new code of <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>
  • 3

    You’re putting a tag a inside one and that is not allowed. http://stackoverflow.com/questions/13052598/creating-anchor-inside-anchor-tag

  • I was doubtful about it but already understood, thank you very much, you have some suggestion of how to do the update in the bank and open the PDF on the system?

  • 2

    First thing you have to do is join these 2 elements <a> in a single, moreover, qnd you click it comes to hit in PHP, gives some error message?

  • Nothing, stamping(I think that’s it) he doesn’t even get the values, he passes beaten, but thanks for the help, I’ll try to do and show the results

  • 2

    Join the tag elements <a>, may be that he is not even gone to PHP because of this, if you are that I try to help you

  • @Jefersonalmeida updated the data

  • 2

    Worked now?

  • No, now by clicking, nothing happens, neither opens the pdf nor updates in the bank

  • @Jefersonalmeida updated the Post

  • 2

    Replaces your data: 'id='+uid for data: {id: uid}, the ideal was also vc debug the return of your post in php to know if there is an error or if the id arrives correctly in it

  • @Jefersonalmeida now it just updates and does not open the pdf, but it is already a progress if you continue to help I will be grateful

  • If I already opened on the other page there was no problem, but I can’t open even with target="_Blank" and if I click open link on new tab it already opens normal

  • 1

    I will formulate an answer with everything here

Show 8 more comments

1 answer

2


First you are using a tag a within another tag a, this is not allowed, and with it must be generating your HTML wrong way. The ideal is to join these 2 elements in a single.

<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>

Also your ajax is wrong, you are passing the date the wrong way, the correct way is the one below:

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

Also as you are overwriting the click of your element it will no longer open a new page, to get around this we will take 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 got the attribute href and kept in a variable and after the completion of the ajax i reload the page with that value

  • 1

    Our guy, really thank you so much, it’s been so long that I was looking for help and I wasn’t getting it, so thank you so much for your help!

Browser other questions tagged

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