Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

ajax / server side validation

General discussions related to php

Moderators: macek, egami, gesf

ajax / server side validation

Postby faust » Sat Jul 28, 2012 2:49 pm

Hey guys. I have a simple login form (username, password, but the fields are not important). So, what I want to do is: the user types in the data, clicks the submit button and if the data is incorrect to be displayed some kind of message like: 'wrong data'. I don't want to have javascript validation. I want only server side validation. So, when javascript is turned on, an ajax request is send to the server to a function to check the data. But, if javascript is turned off for some reason on the user's computer, I want the page to refresh and show the error message or if the data is correct, the user should be redirected to the index page. I hope I made the question clear enough :D Cheers ;)
faust
New php-forum User
New php-forum User
 
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: ajax / server side validation

Postby freshnet » Sat Jul 28, 2012 3:05 pm

Are you saying that you only want the server-side validation to be performed if js is turned on? Why?
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 275
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: ajax / server side validation

Postby faust » Sat Jul 28, 2012 10:13 pm

I think you didn't understand me. I want when js is ON to make the validation via ajax request (so that the page won't refresh) and when it is turned OFF when I click the submit button the page should be refreshed and the err/success messages should be displayed properly.
That is because with js you can do validations like "Field is empty", "Field is more than 20 symbols" but you can't check if this user and pass are in the db and are the correct info (you can't do db checks and manipulations). This is done with ajax request -> php script that checks if the password is for this user etc. etc. That's why I think I don't really need only js validation - I can do all the validation with php and then return a message - "Success", "Field is empty" etc. etc. I'm not saying it's the best practice but I personally would do it like that.
faust
New php-forum User
New php-forum User
 
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: ajax / server side validation

Postby freshnet » Sun Jul 29, 2012 3:49 am

That can't be done. You can't detect if JS is on using php, at least not with one page.
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 275
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: ajax / server side validation

Postby faust » Sun Jul 29, 2012 7:55 am

Okay, thanks for your reply. If I think of a way to get around this I'll post it here if it's helpful for someone else :)
faust
New php-forum User
New php-forum User
 
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: ajax / server side validation

Postby faust » Fri Aug 03, 2012 11:45 am

okay, I think I found a workaround. I haven't tried it, but when I do, I'll post results. I'm giving the simplest example: you have a form with a submit button. You set onClick on the submit button to some js function whete the ajax is called. At the very beginning of that function you call: submit_button.preventDefault. This way, if js is enabled you will stop the form from submitting and make the ajax call. If js is disabled - the form will simply submit, because the preventDefault will not be executed. Hope this works :)
faust
New php-forum User
New php-forum User
 
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Re: ajax / server side validation

Postby freshnet » Mon Aug 06, 2012 3:34 am

That may work... but then you're not using php to do that :D
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 275
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron