Hi there, I just have one quistion...ussually, when I validate a form, I send all the stuff the user writes in it, to another php file...but how do I validate the form in the same page? so if the user have forgotten one feeld or so, the page can show him wich feild that was and so on...?

please help me! =)

    Not possible with PHP. But you can do it with Javascript.

      damn....really? it isn't? why not?
      can u tell me how to do it with javascript?

      hmm...what if the validating page looks exactly the same as the form page and I simply fill in all the feilds that were right and leave all the wrong ones empty in it? maybe people would notice it's two different pages...except for the names of them...or is that too much trouble for this sort of thing?

        I should have explained why.. So i will now.

        PHP is Server Side. Thus, all parsing is done on the server. From that it cannot contact the Client Machine. So if you wanted to have something like,

        Client Click submit.. SHow error immediately it wouldnt work, as the Server has already Parsed the PHP. SO Javascript would be needed.

        However. If you want to do it like this

        eg..

        Persons click invalid form, then re-show the form with the errors, than that is possible.

        Im not a Javascript Guru, so i dont think id be a worthy teacher of it yet. But you can take a look at http://www.w3schools.com/jscript/ they have a good Validation Example.

          lets say you've 3 form fields(name, email and description) and you would like to check if the user has filled in all those fields.Submit the form if all the fields have been filled else throw a message.
          Use the following code between your head tags.

          <SCRIPT LANGUAGE="JavaScript">
          function verify() {
          var themessage = "You are required to complete the following fields: ";
          if (document.form.name.value=="") {
          themessage = themessage + " - Your Name";
          }
          if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.form.email.value)) {
          
          themessage = themessage + " - Your E-mail";
          }
          if (document.form.description.value=="") {
          themessage = themessage + " -  Your Description";
          }
          //alert if fields are empty and cancel form submit
          if (themessage == "You are required to complete the following fields: ") {
          document.form.submit();
          }
          else {
          alert(themessage);
          return false;
             }
          }
          </script>
          

          and your submit button would look like

          <input type=button value="Submit" onclick="verify();">
          

          HTH

            Why go through the trouble of using a javascript when you can use php to check for any empty fields, if a field is empty then echo the form again with error messages stating that the empty fields need to be filled in before submitting?

            something like:

            if ($field == ""){
              $send = "no";
              $empty_field1 = "you need to fill this in";
            }
            
            .
            .
            .
            .
            
            if ($send != "no"){
              submit form
            }
            else{
              echo "$empty_field1";
              echo "$form_block";
            }
            

              Originally posted by Pittperson
              Why go through the trouble of using a javascript when you can use php to check for any empty fields, if a field is empty then echo the form again with error messages stating that the empty fields need to be filled in before submitting?

              something like:

              if ($field == ""){
                $send = "no";
                $empty_field1 = "you need to fill this in";
              }
              
              .
              .
              .
              .
              
              if ($send != "no"){
                submit form
              }
              else{
                echo "$empty_field1";
                echo "$form_block";
              }
              

              [/B]

              Because JavaScript is client side. It is a simple as that - there is no point in having your server process everything + form validation is not really essential. Do it using JavaScript - it is quicker, and can infact give better results.

                Ahh, learn something new everyday.

                I'm still progressing through my "newBism" 🙂

                  Yep. Obviously it wouldn't matter if you were using a small site with few page requests, but imagine if somthing like Hotmail's sign-up system relied on server-side validation (me having said that, it probably does!) if all validation is done by clients, the server response is likely to me much faster.

                  While I say this, there are some key things you should validate using PHP - remeber not everyone will be using JavaScript enabled browsers, and some people will try to cheat the system.

                  Thomas

                    I guess JAvascript is nice....but I need to see if the username and stuff like that are busy and so on and dealing with both javascript and php seems to be a bit too messy for me at the moment...I think I'll try the php sollution...

                    thanks everyone for yuor help! hmm...
                    but what am I supposed to write in the action of the form? just the same filename? or something else?

                      <form name="whatever you like" action="<?PHP echo $PHP_SELF;?>" method="post">

                      THomas

                        hm..now u'll have to help me here....

                        first, I have a problem...when I go into the page...all the feilds are emty..so...ofcource the error messages come up...wich I don't want to happen when u enter the page....how can I fix that?

                        I have a test-code thing here wich I've made...

                        what do I have to change??

                        <html>

                        <?php

                        $test= $_POST["test"];
                        if ($test == ""){
                        $send = "no";
                        $empty_field1 = "you need to fill this in";
                        }
                        else{
                        echo "works!";
                        }
                        ?>
                        <form name="test" action="<?PHP echo $PHP_SELF;?>" method="post">
                        <input type="text" name="test">
                        <?php
                        if($send == "no"){
                        echo "$empty_field1";
                        echo "$form_block";
                        }
                        ?>
                        <input type="submit" value="test">
                        </form>

                        </html>

                        lots of thanks!

                          I've tried to use a <input type="hidden" name="set"> in the form...and the form validation doesn't start as long as that variable isn't set! =)

                          and it seems to work...

                          was I correct? =)

                            <?php
                            if(isset($POST[test]))
                            {
                            if($
                            POST[text] == "")
                            {
                            echo("Please fill in the field text");
                            }
                            else
                            {
                            echo("Form filled in correctly");
                            }
                            }
                            ?>
                            <form name="test" action="<?PHP echo $PHP_SELF;?>" method="post">
                            <input type="text" name="text">
                            <input type="submit" name="test" value="test">
                            </form>

                            I think that is a better way of doing things.

                            Thomas

                              Write a Reply...