rhill45
rhill45

Reputation: 569

how to post data stored in an input tag using jquery ajax

An initial ajax event saves a unique numerical ID into the value attribute of the following input tag:

 <input class="avatar-id" id="avatar-id" name="id" value="">

I would like to use jquery ajax to send this value to a php script named delete_list.php. I think I am incorrectly using the jquery ajax DATA setting here to pull the value out:

$("#dformclose").click(function() {
if (confirm("Are you sure you want to cancel this? Your image and any information in this form will be deleted")) {
    var id = id;
        $.ajax({
            type: "POST",
            dataType:"html",
            url: "delete_list.php",
            data: "#avatar-id.attr(value.id)",
            success: function(response) {
              location.reload(true);
            }
        });
    }
         else 
      { 
    return false;
 }
});

How can I correctly use this script to process the ID on the php end?

Upvotes: 0

Views: 1045

Answers (2)

ilia
ilia

Reputation: 1334

var idValue = $('#avatar-id').val();

$.ajax({
    type: "POST",
    dataType:"html",
    url: "delete_list.php",
    data: {id: idValue},
    success: function(response) {
      location.reload(true);
    }
});

Upvotes: 1

Rafael
Rafael

Reputation: 7746

Simply,

jquery

data: $('input#avatar-id').val(),

php

To access in php use $_POST['id']

Upvotes: 0

Related Questions