views:

219

answers:

5

I have a multi-lingual page where I want to display form validation error in the user's language. I use a hidden input to determine which language version the user is browsing like this: <input type="hidden" name="lang" id="lang" value="<?php echo $lang; ?>" />

The PHP side of the script works, but jQuery doesn't seem to realize which language is passed on. It displays the English error message no matter on which language site I am.

Here's the code (I removed the other form fields for length):

            $(document).ready(function(){
                $('#contact').submit(function() {
                    $(".form_message").hide();

                    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                    var lang = $("#lang").val();
                    var name = $("#name").val();

                    var dataString = {
                        'lang': lang,
                        'name': name
                    }

                    if (name == '') {
                        if (lang == 'de') {
                            $("#posted").after('<div class="form_message"><p><span class="error">Fehler:</span> Bitte gib deinen Namen an!</p></div>');
                        } else {
                            $("#posted").after('<div class="form_message"><p><span class="error">Error:</span> Please enter your name!</p></div>');
                        }
                        $("#name").focus();
                        $("#name").addClass('req');
                    } else  {
                        $("#loading").show();
                        $("#loading").fadeIn(400).html('<img src="/img/loading.gif" />Loading...');
                        $.ajax({
                        type: "POST",
                        url: "/contact-post.php",
                        data: dataString,
                        cache: false,
                        success: function(html){

                            $("#loading").hide();
                            $("#posted").after('<div class="form_message"><p>Thank you! Your contact request has been sent.</p></div>');
                            $("#contact input:submit").attr("disabled", "disabled").val("Success!");

                    }
                });
                }return false;
            }); });

The problem seems to be somewhere in the nested if statement. Does jQuery / javascript even recognize nested ifs? And if yes, why is it not working?

+3  A: 

Does jQuery / javascript even recnogize nested ifs?

Yes they do

London
+1  A: 

Why wouldn't it recognize nested if's?

Can you include the HTML for the page? There doesn't appear to be anything wrong with this javascript at all - so I have a feeling the issue is with the rest of the page.

Barring that, put an alert(lang) in right before your if statement to see what it is set to. My guess is that it will not be set to the value that you think it should be set to.

Charles Boyung
Thank you for the hint - I did that, and I get an empty alert. Obviously the value of #lang is not passed to jQuery, but I don't understand why because when I look at the html source code of the form, there is clearly a hidden field with the id #lang and the value 'en' (or 'de').
rayne
Is the "id" of the element "lang" or "#lang"?
Pointy
Just lang of course, source code looks like this: `<input type="hidden" name="lang" id="lang" value="de" />`
rayne
Oh duhh sorry I just saw that in the original question. OK then.
Pointy
Oh well here's another one: it it possible that you're using "lang" for the "id" of more than one element on the page?
Pointy
D'oh! That was it. Stupid beginner's mistake. Thank you very much for pointing it out! :)
rayne
+2  A: 

Javascript is case-sensitive, and perhaps the value of your #lang element is in a different case. You can force it to be lowered like this...

var lang = $("#lang").val().toLowerCase(); 
Josh Stodola
It's all lowercase, but thank you for the helpful hint!
rayne
+1  A: 

Check the value

alert("'" + lang + "' :" + lang.length);
chris
+2  A: 

One thing worth checking that would cause this behaviour is that you don't have any other elements on your page with id = lang. If there are, your $("#lang") selector will only find the first one, and if that's not your hidden input it won't work as you expect.

rosscj2533