I have an email sign up section on my index page. I have figured it all out except the validation and ajax part. When I submit the email it saves it to database and send me an email. But it displays simple success message. What I want to do is to validate the email address and and upon submission only refresh the form itself. The tricky part for me is I want to display any error or success message inside the input field itself. Simply because if I display it above or beneath the input field it messes up the layout of the page. I shot a screenshot here: http://i.imgur.com/14BXzil.png
Any help is greatly appreciated on how to construct this function. Many thanks to you´all.
Sorry, I meant on the client - the Ajax routine.
Also, I removed your email address from your code - it’s not a good idea to post that in plain text.
I have tried couple of scripts but none of them worked as I expected, as you can tell I am not very savvy on JS. I was wondering what would be the best way to construct such functionality. I found most tutorials either incomplete or confusing.
Well, you have a form that accepts an email address.
The user enters something and you validate it.
If the user’s input doesn’t validate, then display an error message.
If the input does validate, submit the details via Ajax and process them on the server.
I am presuming you have all of this working.
You can do this in a success callback in your Ajax function:
Thanks for taking time to response. I have tried it and it works on local host, on live server however, it did not work. Since local host cannot send email, I have received a PHP error inside the p tag but it shows it that is working. The id for the email field is not email but “newsletter-email” so I have changed the “data: {email: email}” to “data: {newsletter-email: email},” also appended the PHP you have sent me to my own PHP script. It is really frustrating. I need to pick up on JS asap… :S
I have stripped the curly braces but still no luck. I am about to lose my mind. Would you please look at my code and tell what I am doing wrong? I keep getting a blank page with success message but PHP takes over once I hit the submit button.
I really appreciate the answers. As you may have guessed, I’ve tried it but no luck. I’m certain that there is something wrong with my PHP script but everything seems ok !!!
Nah, data can be an object literal. That’s fine.
Looking at the code again, though the minus in the variable name is causing the script to break.
It should probably be: