Skip to content
Advertisement

save data from php to ajax and change color of div when new data insert?

Hi i am trying to save value and alert them using ajax which i am insert using php in my sql table but my alert is not working

Here is my code demo.php

JavaScript

demo.html

JavaScript

here i want when i submit form them div color should change which is in demo.html where i am wrong in this code

and how can i achieve my goal

Any help will be appreciated

Advertisement

Answer

changes you need to make:

  1. add jquery as a dependency as you are using $.ajax utility function which is provided by Jquery.
  2. As you are using Jquery, you could use its selectors for getting values of elements and binding functions to dom elements. I have commented it in the source code.
  3. You are using a form with a submit button and executing the ajax call on click of it. But you need to prevent the page from submitting the form by preventing the default behavior of the submit button. Refer event.preventDefault();
  4. Move the php ajax response part to the top and call exit() once your response is complete. Else your ajax response will include the whole page html source also.

.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement