I have a page that I'm building that I want to be able to change account names on the fly using Ajax. Everything works smoothly on the name change submission, however, on the second one, I get duplicated data, even though the function is relative to $(this). Here is an example of my console. It ends up overwriting the first submission somehow, and sends the second inputted name in for both the current account, and the previous account.
index.html
<a id="edit-name" number-data="+155512345678" phone-sid="example"></a>
$(document).on("click", "#edit-name", function(){
var num = $(this).attr("number-data");
var sid = $(this).attr("phone-sid");
$(this).parent().parent().append('<div class="input-wrapper"><div class="input-field"><label for="name-change">Name</label><input id="name-change" type="text" class=""></div><a id="send-name" class="btn">send</a></div>');
$(document).on("click", "#send-name", function(){
var val = $(this).prev().children("#name-change").val();
$.ajax({
type: "POST",
url: 'manage-account.php',
data: {trackingNumber: num, newName: val, phoneSID: sid},
success: function(data){
console.log(data);
}
});
});
});
manage-account.php
$trackingNumber = mysqli_real_escape_string($link, $_POST['trackingNumber']);
$newName = mysqli_real_escape_string($link, $_POST['newName']);
echo "Tracking Number: ". $trackingNumber;
echo "Phone Name: ". $newName;