rastacre Posted February 14, 2008 Share Posted February 14, 2008 Hi all, I'm new to php and webdev in general, so i'm trying many things on my own which usually work but this time i'm completely stuck... Ok i made a script that automatically generates a form to add/upate/delete stuff from a database. the db have various tables, so the script automatically generates the form pulling the relevant fields from the table that the user chooses. I implemented the form validation via php so it simply call the validation via the action attribute of the form. So far so good. Now I wanted to try to implement a realtime validation: let's say that i have a table called "category" where of course i have cat_id and cat_name let's say i want to add a new value to such table. The form script generates the form requiring just the name of a new category. So at this point if i input an existing name and send the form, the validation script will redraw the form and show an error by looking for the name value in the appropriate table. But what I'm aiming to is to also implement a realtime validation that will check the user input and show the error before the form is sent. I would just like to know if this is possible, and what kind of approach shud be taken. I was thinking about pulling the values from the table at form generation, and then use a js function to compare the user input (onkeyup i think) against the values (maybe stored in an array) previously pulled. In this regard is there a way to pass variable data from a php script to a js script? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/91102-realtime-form-validation/ Share on other sites More sharing options...
Isityou Posted February 14, 2008 Share Posted February 14, 2008 You could use AJAX. Quote Link to comment https://forums.phpfreaks.com/topic/91102-realtime-form-validation/#findComment-466920 Share on other sites More sharing options...
Psycho Posted February 14, 2008 Share Posted February 14, 2008 onkeyup would be a poor choice for performing form validations. it would be too intensive and be difficult to prevent false-positive. Ex: there is an existing field called 'name' and the user tries to create a new field called 'name_suffix'. As soon as the user enteres 'name' it would generate an error and most likely prevent the user from contuniuing. onchange would be a much better choice. Yes you can do this with AJAX, but you also still need to implement the validation server-side once the form is submitted in case the user does not have JS enabled. I would aslo agree that generating an array of the values to be validated for uniqueness instead of sending an individual request each time the user changes the value. Quote Link to comment https://forums.phpfreaks.com/topic/91102-realtime-form-validation/#findComment-466936 Share on other sites More sharing options...
haku Posted February 14, 2008 Share Posted February 14, 2008 Everything the last poster said was pretty much spot on. Just to add, you should make the function run onblur. That way it wont provide the false negatives (and positives as well) that he mentioned. Although sending back an array of all the values could get very bulky depending on the amount of data needed to be sent. AJAX is definitely the way to do this. Its a new technology that basically uses javascript to make requests in the background, then reloads parts of a page rather than the whole page. There is an AJAX section on this forum. Quote Link to comment https://forums.phpfreaks.com/topic/91102-realtime-form-validation/#findComment-466957 Share on other sites More sharing options...
Psycho Posted February 14, 2008 Share Posted February 14, 2008 Why use onblur? If a user enters a field and makes no changes, why would you need to revalidate it? Granted, there could be some situations where onblur might be useful, such as if the validation parameters are changing in real-time. But, those situations are few and far between. I still stand by using onchange for form validations. Quote Link to comment https://forums.phpfreaks.com/topic/91102-realtime-form-validation/#findComment-466962 Share on other sites More sharing options...
haku Posted February 14, 2008 Share Posted February 14, 2008 I hear what you are saying. I think its 6 of one and 1/2 dozen of the other though. There are times when onblur would be preferable, like if the user left the page for 10 minutes and then came back and was going through and making some changes. Someone may have registered the name in the meantime. Not the most likely thing to happen, but not impossible either. Either of them will work fine though. Quote Link to comment https://forums.phpfreaks.com/topic/91102-realtime-form-validation/#findComment-466978 Share on other sites More sharing options...
rastacre Posted February 15, 2008 Author Share Posted February 15, 2008 OK thanks everyone, I'll see what I can do with the info u all gave me. Just to clarify, I'm going to leave the php validation running at form submit just to prevent users to input incorrect values. I thought that ajax would have been the answer, but without knowing which direction to take I preferred to ask here before spending days looking into something it might not be useful Thanks again guys, I'll post my findings and results as soon as I get things to work Quote Link to comment https://forums.phpfreaks.com/topic/91102-realtime-form-validation/#findComment-467490 Share on other sites More sharing options...
rastacre Posted February 15, 2008 Author Share Posted February 15, 2008 Hi All, just to let you know, I got the validation right. I simply used the AJAX example code from the AJAX section, modified to suit my script, and it worked straight away. This is a very informative forum, I hope one day i'll be able to give advice here as well Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/91102-realtime-form-validation/#findComment-467518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.