When a user inserts data into a form and then submits it, it goes to my php script and inserts the data. The script will return a value letting the user know if the data was inserted or not. Since I am using JQuery, I have no idea how to do this. Can someone please point me in the right direction? All I’m after here is a nice, neat little 200px by 25px container that will fade in and the user MUST click it to acknowledge it.
Now that I think about it, since users are not the brightest lights in the harbor, it would be really cool if I could not only show then this css box letting them know if the insert was successful but also show them the customer that was inserted in case they forgot where they left off.
Thanks.
I’m not going to mark this as a duplicate, but you are essentially asking the best way to show notifications using jQuery. And that has been asked before. In a nutshell, in your basic setup all you need to do is show a div and fade it in, but there are a lot of plugins out there that handle more situations.
As far as showing them the customer that was inserted or whatever, all you have to do is return a JSON response from the server and handle it with the success callback of your AJAX request. You will have access to the data passed back from the server then.
To explain a little further: What you seem to be asking to do is how to send a request to a server using Ajax, get a notification that everything went as expected, and then show a notification in the webpage depending on what happened. If that is correct, I’m going to move on with these assumptions.
Let’s put together a simple form to add a new customer to a fictional website. It might look like this:
Now, our naive, insecure PHP code to handle this form submission (through AJAX) might look something like this. I say naive and insecure because you would do more data validation (as you never trust anything coming from the client) and would certainly need to clean the data to prevent SQL injections. This is an example, however, so it might look like this:
We might then handle the sending of this form and the receiving of data from the server, using jQuery, with code that looks a little something like this:
I haven’t tested any of this code but the idea is there. You return a format, JSON, from the server, with data, and handle the success callback jQuery fires when the request is complete, and add an element to your document you can style however you want (and show with things like
fadeIn()if you want) with a message describing what happened. You might also need to catch the error callback (which fires if your server doesn’t respond for whatever reason) and firing off another alert in that situation.