M
M
Msklf2017-10-09 17:26:21
PHP
Msklf, 2017-10-09 17:26:21

Ajax Post request without page reload?

I'm trying to send a post-request on one page without reloading, ajax works ok, everything is sent to the console. But for some reason php doesn't want to output.
Code like this:

Button:

<a href="#" class="btn-edit" id="<?php echo $user['id']; ?>" onclick="give(this.id)"></a>


Ajax:
$(document).ready(function( ){
$('div a').click(function() {
$.ajax({  
        type: "POST",  
        url: "singers.php",  
        data: "idart="+this.id, 
        })
})
})


And php on the same page: Who knows what's the reason?
<?php echo $_POST['idart']; ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-10-09
@zkelo

Where is the function give(this.id)? And why is it, if you are already onclickhanging a handler on the event through jQuery?
PS

Try like this
<?php if(empty($_POST['idart'])): $user['id'] = 'tosterUser'; ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    $('#user_link').click(function(){
        $.post({
            url: 'testo.php',
            data: 'idart=' + $(this).attr('data-id'),
            success: function(data){ $('#result').append('<br>' + data); }
        })
    });
});
</script>
<div id="result"><a href="#" id="user_link" data-id="<?php echo $user['id']; ?>">Кнопка</a></div>
<?php else: echo $_POST['idart']; endif; ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question