Jump to content

can anyone help me in doing form validation using ajax


vinothini

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.