So, I am publishing my first PHP function, which I am proud of, but I recently learned about AJAX and wanted to test it. Unfortunately, I cannot get it to work.
My experience: PHP (3 weeks). CSS3, HTML, Basic Javascript.
My problem: Getting AJAX to work. I want ajax to get my data from a php file that receives votes from a test server database (Xampp). Therefore, every time the user clicks on a good or bad file, AJAX should display new results without refreshing the page. However, the problem is that: A) My if statements work by checking isset ($ _ POST), which will no longer work if I call AJAX. B) Prevention of updates. C) Updating AJAX after each click. I know them almost there, I just missed something, and I donβt know for sure that it should be honest.
What I tried: Checking the database connection. Checked if my PHP code works without ajax and it does a great job (I just demonstrate half the functionality here, the lightened version, for simplicity). Tried to change submit to button. Clearing the cache. JQuery is at the head of my document, and the path is right. I looked through the textbooks and read the documentation, but I'm not going anywhere, possibly due to lack of experience.
Edit: Sessions and everything that works with php is fine. I have my session start and database connection enabled at the very top.
Summary: How to fix this ajax so that it always updates my numbers?
Let me know if you want me to explain parts of my PHP code. I want to comment on the details if necessary.
JQUERY / AJAX CODE
function vote() { var request = $.ajax({ url: "php/core/voting_system.php", type: "POST", dataType: 'html' }); request.done(function(vote_sum) { $("#votes").html(vote_sum); }); }
HTML code:
<div id='votes'></div> <form id="good" action="" method="post"> <input type="submit" name="good" onclick="vote()" value="+"> </form> <form id="bad" action="" method="post"> <input type="submit" name="bad" onclick="vote()" value="-"> </form>