vinothini Posted February 17, 2009 Share Posted February 17, 2009 1.can anyone help me in doing form validation using ajax if i have 5 elements ina page 2.how to retrieve data from db using ajax to drop down box Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted February 17, 2009 Share Posted February 17, 2009 form validation: write a function that checks each value in the form. You'll need a lot of if's or a lot or ||'s (in one if). Here's what I did: if(box1.value == "") { alert("box1 is empty"); return false; } if(box2.value == "") { alert(...); return false; } you get the point. Just do that for each value, then at the end after all the ifs, put return true. If any of the boxes are empty, they'll return false and kill the function, if they all check out, it'll return true. Now, put that function inside an onsubmit attribute in your form: <form method="POST" action="someaction.php" onsubmit="return validatefunction(this);"> In your validation function you'll need to specify the "this" parameter you passed, which is the whole form. so whatever you name it, do this: function somefunction(form) { then to get the value for each box, you just put: form.[fieldname].value Did that help? As for retrieving data. You don't retrieve the data from the database with AJAX, you retrieve the data from the database using PHP, you use AJA to call that PHP page, the AJAX returns the result of that page to the page from where you ran the request. Make a page that gets the info you need and builds a dropdown menu using PHP, then use AJAX to call that page from whatever page you need the dropdown menu on. From what I'm reading from your post, you don't need AJAX. You don't need AJAX to validate a form, you just need Javascript. You don't need AJAX to build a dropdown menu out of data from a database, you need PHP. You would need AJAX to validate a form against certain values a person might put in that need to match something. You would use AJAX if you wanted that dropdown menu to appear on, say, a search page, if the dropdown menu contained items that a person might have searched for. Quote Link to comment Share on other sites More sharing options...
vinothini Posted February 18, 2009 Author Share Posted February 18, 2009 I know to do javascript coding...just wanted to practise and learn ajax . suppose if i have a dropdown box with values onchange im calling javascript and my page s reloading.how to avoid this by using ajax.i can understand better if you send code and explain. Quote Link to comment 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.