Okay, so I have a poll form that, when submitted, should:

a)  Stop the submission of the form so we don't go to the next page;
b)  Verify that at least one radio button within the form was checked,
and c:
c)  Send a request to the poll processing form, then receive the
request and put the results on the page.

And lo!  Here it is!  (Code snippets below, but here's the page.)  And
we have four separate browsers, and three different errors!

http://www.starcitygames.com/polls/poll_form.php?poll=5

On INTERNET EXPLORER, it works.

On FIREFOX, the Ajax object refuses to send anything.  I mean,
anything.  I'm checking in Firebug, no requests are being sent, and I
have no idea why.

On OPERA, it's erroneously saying that no options have been checked,
because as far as I can tell the Form.serialize() method is not
actually picking up the radio buttons, and thus is throwing an
"undefined" error when we look in the serialized data.

On SAFARI, it's merrily ignoring the Event.stop(e) and continuing on
its merry way to the submission form.  Which I can work around, but
still, that shouldn't happen as I understand it.

Here's the code - most of the errors occur in the poll_submitted
method.  Are these native errors in Prototype, or am I doing something
wrong?  I've run this through JSLint and don't see any obvious syntax
errors here.

var Poll_checker = Class.create();

Poll_checker.prototype = {

        initialize: function() {

                alert('Initialized!');
                var poll_submitted = 
this.poll_submitted.bindAsEventListener(this);
                $$('form').each(
                        function(node){
                                var matches = node.id.match(/poll/g)
                                if (matches !== null) {
                                        node.onsubmit = poll_submitted;
                                        //alert('Found a node ' + node.id);
                                }
                        }
                );
        },

        poll_submitted: function(e) {

                var targ = Event.element(e);

                alert(targ.tagName);
                Event.stop(e);
                var poll_ID_prefix = targ.id;
                //alert(targ.id);
                $(poll_ID_prefix + '_submit').disabled = true;
                $(poll_ID_prefix + '_submit').value = 'Submitting vote....';
                $(poll_ID_prefix + '_feedback').innerHTML = '';

                /*
                if ( $('login_value').value != 'logout') {
                        $(poll_ID_prefix + '_feedback').innerHTML = '<span 
class="red">You
must be logged in to vote in an SCG poll.  You may log in on the left-
hand side, or you can create an account <a href="https://
sales.starcitygames.com/userinfo.php">by clicking this link</a>.</
span>';
                        $(poll_ID_prefix + '_submit').value = 'Please log in to 
vote!';
                        return;
                }
                */

                var submitted_form_values = targ.serialize(true);
                var poll_ID = submitted_form_values['poll_ID'];
                //alert('Poll ID in opera is ' + poll_ID);
                var poll_value = submitted_form_values['poll_' + poll_ID];
                //alert('Poll value in opera is ' + poll_value);
                //alert(dump(submitted_form_values));
                if (typeof(poll_value) == "undefined") {
                        $(poll_ID_prefix + '_feedback').innerHTML = '<span 
class="red">You
must choose an option before you can vote!.</span>';
                        $(poll_ID_prefix + '_submit').value = 'Vote!';
                        $(poll_ID_prefix + '_submit').disabled = false;
                        $(poll_ID_prefix + '_feedback').innerHTML =
dump(submitted_form_values);
                        return;
                } else {
                        //alert('Value is ' + poll_value);
                        $(poll_ID_prefix + '_feedback').innerHTML = 'Getting 
vote
information...';
                }

                //alert(submitted_form_values['poll_ID']);

                var request = new Ajax.Request(
                        'http://www.starcitygames.com/polls/poll_process.php',
                        {
                                method: 'post',
                                parameters: submitted_form_values,
                                onComplete: this.pollComplete.bind(this)
                        }
                );

                alert('Got to end of completion AJAX?');
        },

        pollComplete: function(transport) {
                alert('Wow, completed! -- ' + transport.responseText);
                var response = transport.responseText;
                var jsonObj = eval('(' + response + ')');

                var poll_ID_prefix = 'poll_' + jsonObj.poll_ID;

                if (jsonObj.status != true) {
                        var final_error_message = 'Error in processing poll!';
                        jsonObj.error_messages.each(
                                function whatevs(error) {
                                        final_error_message += error;
                                }
                        );

                        if (! jsonObj.poll_ID) {
                                alert(final_error_message);
                        } else {
                                $(poll_ID_prefix + '_feedback').innerHTML = 
'<span class="red">' +
final_error_message + '</span>';
                        }

                        return;
                } else {
                        $(poll_ID_prefix + '_wrapper').innerHTML = 
jsonObj.HTML_output;
                }
        }
};

function dump(arr,level) {
        var dumped_text = "";
        if(!level) level = 0;

        //The padding given at the beginning of the line.
        var level_padding = "";
        for(var j=0;j<level+1;j++) level_padding += "    ";

        if(typeof(arr) == 'object') { //Array/Hashes/Objects
                for(var item in arr) {
                        var value = arr[item];

                        if(typeof(value) == 'object') { //If it is an array,
                                dumped_text += level_padding + "'" + item + "' 
...\n";
                                dumped_text += dump(value,level+1);
                        } else {
                                dumped_text += level_padding + "'" + item + "' 
=> \"" + value +
"\"\n";
                        }
                }
        } else { //Stings/Chars/Numbers etc.
                dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
        }
        return dumped_text;
}


window.onload = function() {
        Poll_checker = new Poll_checker();
};

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to