PHP Form Validation + JavaScript + gettext

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hgeithus
    New Member
    • Oct 2008
    • 20

    PHP Form Validation + JavaScript + gettext

    Hi. I'm doing a form at my website, and I found this document which is very interesting: Javascript form validation - doing it right .

    I have a little question though. All the documents within my page has the *.php extention since I'm using PHP the most. I'm also using gettext to translate strings to other languages. If I want to translate the string "hello", I would write it like this in my *.php document:

    Code:
    <?php echo _("hello");?>
    In my navigation bar, I have links to different languages. Pressing one of these links sets a global variable that gettext uses to identify what language that's active and then translates all the strings which are marked in same way as the code above..

    I encounter a problem when mixing this JavaScript code with my PHP code. Because I want the output messages from the validation to be translated as well. For instance if a field is left blank the JavaScript outputs the message: "Required field cannot be left blank". I want this message to be translated as well.

    So what is the best strategy here? Should I do it all in PHP? Does anyone have experience with this kind of setup? Thanks in advance.

    Regards
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    hgeithus, please do not hi-jack another person's thread. This is against the forum rules. If you have a question, start your own thread.

    Moderator.

    Comment

    • hgeithus
      New Member
      • Oct 2008
      • 20

      #3
      I wanted to ask a question, therefore I started this thread. What seems to be the problem?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by hgeithus
        I wanted to ask a question, therefore I started this thread. What seems to be the problem?
        You didn't 'start this thread', you hi-jacked someone else's thread. That is the problem.

        Moderator.

        Comment

        • hgeithus
          New Member
          • Oct 2008
          • 20

          #5
          Ah, Sorry about that :S I didn't realise I posted inside another thread :S

          Hmm...

          Can you please delete this thread? It looks silly now with this discussion.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by hgeithus
            Ah, Sorry about that :S I didn't realise I posted inside another thread :S
            It happens.

            Originally posted by hgeithus
            Hmm...

            Can you please delete this thread? It looks silly now with this discussion.
            Negative. Then you might not receive an answer. I'll have a look at your question now.

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              I can't, at the moment, think of an efficient way to do this. I could only suggest you make an ajax call to a page with the error that your javascript validation threw and the language you want it changing to. Then have the php page return the errors counterpart in whatever language you specify.

              Quite an ugly way of doing it.. but, hey.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Could you not simply set the message your JavaScript is supposed to use via PHP when the page is loaded?

                Something like:
                [code=php]
                <html>
                <head>
                <title><?php echo _("Page title"); ?></title>
                <script type="text/javascript">
                var fieldRequiredMs g = '<?php echo _("Fill out the fields! NOW!"); ?>';
                </script>
                </head>
                <body>
                <button onclick="alert( fieldRequiredMs g);">Click me</button>
                </body>
                </html>
                [/code]

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by Atli
                  Could you not simply set the message your JavaScript is supposed to use via PHP when the page is loaded?

                  Something like:
                  [code=php]
                  <html>
                  <head>
                  <title><?php echo _("Page title"); ?></title>
                  <script type="text/javascript">
                  var fieldRequiredMs g = '<?php echo _("Fill out the fields! NOW!"); ?>';
                  </script>
                  </head>
                  <body>
                  <button onclick="alert( fieldRequiredMs g);">Click me</button>
                  </body>
                  </html>
                  [/code]
                  Doh! I'm an idiot..

                  Comment

                  • hgeithus
                    New Member
                    • Oct 2008
                    • 20

                    #10
                    That was kinda sweet :D
                    Thank you :)

                    Comment

                    Working...