jquery, onclick catch id values <a

Asked

Viewed 1,383 times

0

It’s a notification system. The code below is working fine. When I click on the notification (

php file that generates notifications:

 <ul class="dropdown-menu">
      <li class="header">Você tem <?php echo $cont; ?> notificações</li>
      <li>
        <!-- inner menu: contains the actual data -->
        <ul class="menu">
<!-- Notificações  -->
         <?php

            foreach(variavel as variavel):
                (...)
                print '
                      <li>
                        <a onclick="sendData()" href="#">
                          <i class="'.$icone.'"></i> '.$nome.'
                          <input type="hidden" name="id" id="id" value="'.$id.'">
                        </a>
                      </li>';


            endforeach;
         ?>
        </ul>
      </li>
      <li class="footer"><a href="notif.php">Ver todas</a></li>
    </ul>

Function jquery/ajax:

<script>
function sendData()
{
    var idn = $('#id').val();

    $.ajax({
        type:"POST",
        url:"upd.php",
        data: { id: idn },
        cache: false,
    });
}

</script>

upd.php file:

(...)
$id = $_POST['id'];
$sql = "UPDATE notif SET status = 1 WHERE id = :id";
(...)

2 answers

0

I know it would be something like:

<script>
    $('.approved').on('click',function(){ 

        var taskid = $(this).parent().parent().attr('id');

        $.ajax({ 
            url: './task.php', 
            type: 'POST', 
            data: { action: 'aprovar', ID: taskid },
            success: function(data) { 
                window.location.reload(); 
            } 
        }); 

    });
</script>

But I’m having a hard time adjusting this code.

0


Try to do it this way:

$(document).ready(function() {
      $('.link-data').click(function(e) {
        e.preventDefault();
        var idn = $(this).children('input').val();
        $.ajax({
            type:"POST",
            url:"upd.php",
            data: { id: idn },
            cache: false
        }); 
    });
});

Add a class to identify links for example link-data, something else, put ids different for each input, the attribute id is to be unique on the page. I hope it helps, vlw.

  • Perfect. It worked. However, can you help me with another problem that came up? <li> <a class="link-data" data-toggle="modal" href="#" data-target="#'. $not_id. '"> <i class="'. $not_icone. '"></i> '.$not_name. ' <input type="Hidden" name="idnot" id="'. $not_id. '" value="'. $not_id. '"> </a> </li>'; Bootstrap use. The modal is not opening when I put the input inside the 'a' or 'li' ... just outside the dropmenu

  • I did. Thanks to the code you posted, which solved the initial question I asked. I removed the input, put a class in the parent <li> and added the id variable to it and changed the line in the script: $(this). Parent('li'). attr('class'); PHP CODE: <li class="'. $id. '"> <a class="link-data" data-toggle="modal" href="#" data-target="#'. $id. '"> <i class="'. $icone. '"></i> '.$name. ' </a> </li> Grateful Fernando.

  • To open the modal try to use $(seletor).click(function(){ $(seletor_do_modal).modal();});. How it stays inside the menu the event that is fired in the click goes to the function of the menu, I think that’s it.

Browser other questions tagged

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