aJ-47 aJ-47 - 3 months ago 6
jQuery Question

jQuery posting to PHP

Very new to jQuery and almost infant to PHP.
I have a very basic web page using jQuery that populates a JavaScript object, as the user provides values in a form.

My next learning step is to use $.post(url,data,callback);
And dynamically update content on the page. I have a test server and PHP running and installed on it(using XAMPP).

I was after some guidance as to what needs to coded on PHP side of things to receive the JavaScript object, and send a return message back?

Answer

I could help you with the Javascript/Jquery part.

You should learn Ajax. Docs Here

You will do an asynchronous request to your PHP server, to an specific route that will receive this post, for example:

             $.ajax({
               type:'POST',
                url:"/login",
                data: { username: $('.username').val(), pass: $('.password').val() 
                },
                success: function(data){
                    console.log("request worked");

                }, statusCode: {
                200: function(response){
                   // modify your content here
                  },
                }
            });

After receiving, you should send an appropriate response with a code status (e.g 200), and based on this response, you can dynamically modify your HTML on the Ajax callback.