Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / JavaScript - Using ajax to validate form data

Author
Message
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 16th Apr 2014 19:59
I'm trying to call a php script on a form submit using ajax so I can validate the fields without having to refresh the page. (yes validation is done on the server side regardless)

I've looked at using jquery (1.11.0), but nearly every example I look up has a slightly different structure and frankly I haven't managed to get very far.

So far the only example I've gotten to work is this:


Which does me little good since it's not actually posting the form's data. I'm not sure how I would do that. I've gathered that whatever I echo out in the php script is what I'll see in 'data'. Java I can do, javascript however I have fairly basic skills.

Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 17th Apr 2014 07:33 Edited at: 17th Apr 2014 09:21
in your example, the only thing you are posting is {name: 'bob'}, as a hard coded value. You should be able to test this in your php script using echo $_POST['name']; and it should print out bob


change this block to reference an element's value like:
{name: $("#field_name").val()},


you should do the validation in javascript before making the request as we don't even need to transmit anything if we already know that there is missing or invalid data. It doesn't hurt to revalidate server side if you want, and always always clean the data server side before sending it to a database, but there is no need to create more traffic than needed.

Here is a full jquery example:

control.js



page.html



*** many edits later, this is now tested and working ***

Some things to keep in mind with javascript:

While it is OOP, it does not have traditional classes. Instead, all functions are also objects, and like any object they can have variable members and methods. It seems odd at first to define a class by declaring a function, but this is really one of the keys to understanding javascript.

Because functions are also objects, they can be passed as parameters to other functions.

Because functions are also objects, you can create instances using new

Functions can be anonymous. anonymous functions can be passed to other functions, or assigned to variables/members to create methods.




Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Apr 2014 19:16
Quote: "should be able to test this in your php script using echo "

That's what I kept messing up. I kept sticking the php var in there but wasn't echoing it out. That's my blonde moment for the day.


Quote: " It doesn't hurt to revalidate server side if you want,"

That's a must since JS can be turned off or even bypassed.

Login to post a reply

Server time is: 2024-04-26 14:17:54
Your offset time is: 2024-04-26 14:17:54