0

I am currently working on a project which consists of a chatting website. I have a form(for adding some as a friend through his name) created in javascript and I want to submit it through ajax, but my ajax submission is ignored at all. It is strange because, with a day before it was working correctly, after the submission, I checked the database and everything was fine. The next day, when I woke up and tested again the form it was broken and a URL was displayed in the URLs place :)) despite the fact that I used ajax. I got no response from the database because the submitting function was ignored. What should I do? Here is my project https://github.com/Alex-dev02/letschat the problem is in the main.js file which is attached to index.php. My main.js content:


let mainList = document.createElement("div");
mainList.classList.add("mainList");

let addFriendForm = document.createElement("form");
addFriendForm.id = "FriendForm";
// my ajax submision
$('#FriendForm').submit(function(){
  $.ajax({
    url: '../letschat/Functions/createUserRelation.php',
    type: 'POST'
  });
});

let addFriendField = document.createElement("input");
addFriendField.classList.add("addFriend");
addFriendField.placeholder = "Add a friend";
addFriendField.type = "text";
addFriendField.name = "addFriend";
addFriendField.autocomplete = "off";
addFriendForm.appendChild(addFriendField);

mainList.appendChild(addFriendForm);

body.appendChild(mainList);

Alex
  • 1
  • 1
  • You are not passing any data value to the post call under submit... Try replacing your Ajax call with [this one](https://stackoverflow.com/a/13613094/8043806) – Giddy Naya Jan 11 '20 at 19:32
  • Ahh, you are right, I was thinking about my ajax submission as a button which don't refresh my page... Sorry – Alex Jan 11 '20 at 19:34
  • Ok, I got it, but still, how did it work fine a day before? @GiddyNaya – Alex Jan 11 '20 at 19:38
  • Too many maybes... – Giddy Naya Jan 11 '20 at 19:46
  • @GiddyNaya It still doesn't work, I made those changes to main.js and I olso moved the ajax code just before `mainList.appendChild(addFriendForm);`: `$('#FriendForm').submit(function(){ $.ajax({ url: '../letschat/Functions/createUserRelation.php', type: 'POST', data: $("form").serialize(), succes: function (msg){ alert('Done'); } }); });` – Alex Jan 11 '20 at 19:57

0 Answers0