MGS Simple AJAX Message System

  • MGS Simple AJAX Message System

    I was having issues with my mail system not delivering my mail to me in a timely manner (from my research, it is a hosting issue with goDaddy, that they have chosen to not address in the past 7 years, even though smaller hosting companies such as hostGator apparently send their mail as fast as they receive it), so I decided I would quickly make my own message system that I can see in the back-end, and keep all of my business messages together.

    I wanted something simple, just a name, email and message, with some basic client and server side validations. I started with making the form

    (SHOW IMAGE | SHOW CODE)

    I then made the .php file that will show me that the server is actually receiving the passed form data. It just echo’s the data, slightly organized, back to the user.

    (SHOW IMAGE | SHOW CODE)

    I now have a form that can post data to the server. It would be more helpful if I had somewhere to save that information, so I went into my database, and added a new table.

    Aside from the form data being sent to the server, I also wanted to save the date and time that the message was created so that I don’t accidentally miss messages (this allows me to sort by date later). I also added in an archived field that defaults to 0 (not archived) so that I can hide messages without deleting them.

    (SHOW IMAGE | SHOW CODE) sql

    This is where it gets a little interesting. I thought I would be able to have just a php file get triggered from an AJAX call, but when working with wordpress you have to use their way of doing things, usually for safety. You end up putting the code in the functions.php file in the theme you are wanting this to work with – meaning when you update the theme, or if you change themes, this isn’t going to work. This seems like an incorrect way for this to work, but I also havn’t found another solution to it. I keep a detailed note of this in my code, so that if I come back to it, I wont have to search for some random function that I cannot find. The way we send the variables we want, and call the function in the functions.php file is pretty simple.

    (SHOW IMAGE | SHOW CODE)

    Using this, whatever we echo out, will be the response text that we can use on the website. Right now I simply take the information, and once it is saved I repeat their name and email back to them, letting them know that I have actually received their email, although for all they know it was just a javascript trick. I was thinking of adding a .gif image over the form to let them know something was happening, but it goes fairly quick anyways.

    Users can now send me messages, but I have to go into the database to check them, and that seems weird, so I added an admin page into wordpress so I could list the messages in chronological order. I also added a delete and archive buttons so that I only have to see the messages I haven’t already dealt with.

    (SHOW IMAGE | SHOW CODE)

    mikegustafson

    Leave a comment