Dynamic Update (Insert) with ajax

Asked

Viewed 130 times

1

I did some research, and I still can’t solve this problem. is an onclick event that sends a request to the update.php file, which updates the notification table. Follows code below:

Triggers the onclick event:

<input type="hidden" name="id" id="id" value="'.$id.'">

<a onclick="sendData()" href="#">

Ajax from this page:

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

    $.ajax({
        type:"POST",
        url:"update.php",
        data: { id: id_n },
        contentType: false,
        cache: false,
        processData: false,
        success:function(dta)
        {
            alert(dta);
        }

    });
}

</script>

Excerpt from update.php file, code:

$id = ($_POST['id']);
$sql = "UPDATE notif SET status = 1 WHERE id = :id";
$res = $pdo->prepare($sql);
$res->bindParam(':id',$id);
$res->execute();
$pdo->close(); 

What could be wrong? Grateful for the attention.

  • What’s the matter with you? What is '#id'? one input?

  • It showed no error. I reviewed the code and removed "contenttype:False" "processData: False" and ran only the GET method. What could be wrong to work the POST? The #id comes from the value of an input

1 answer

0


When you pass an object like { id: id_n }, in the data from ajax, what jQuery does is convert to query string: id=oValorDe_id_n with the jQuery.param(). Having the option processData: false it prevents it. Removing it assumes processData: true.

You can read more about it here (in English)

  • Thanks. But it is only passing via GET. How do I pass a variable that varies through this code? type: var id_n = $( <?php echo $id; ?> ).val() has a way? Thank you

  • @Adrianoleal hence my question "What is the #id".

  • It is an input that is generated by a loop. Are 8. input type Hidden id=idnot name=idnot value=$id

Browser other questions tagged

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