jquery, onclick get id values a

0

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

jquery / ajax function:

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

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

</script>

Update.php file:

(...)
$id = $_POST['id'];
$sql = "UPDATE notif SET status = 1 WHERE id = :id";
(...)
    
asked by anonymous 18.11.2016 / 01:22

2 answers

0

Try to do this:

$(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
        }); 
    });
});

Adds a class to identify the links for example link-data , something else, sets id s to each input , the id attribute is to be unique on the page. Hope it helps, vlw.

    
18.11.2016 / 01:52
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 trouble adapting this code.

    
18.11.2016 / 01:37