crashmaster Posted February 9, 2010 Share Posted February 9, 2010 Hi there, I have one problem, and even dont know what causes it. Have 1 form, and JS 3 scripts. This function creates from special chars (like "ý", "ě", "š", "ž", etc.) their char codes entities ("ý", "ě", "š", "ž") function toEntity(str) { var str; var return_str; for(i=0; i<str.length; i++){ if(str.charCodeAt(i)>127){ return_str += '&#' + str.charCodeAt(i) + ';'; } else { return_str += str.charAt(i); } } return return_str; } This function gets each Object value and applies toEntity function to it function preajax_data_parse (data) { var data; for (i=0; i < data.length; i++) { data[i].value = toEntity(data[i].value); } return data; } This function creates an object from the form, applies preajax_data_parse function, and the alert object values after parsing. function do_it() { arr = $('#form').serializeArray(); arr = preajax_data_parse(arr) alert(dumpObject(arr)) } Var dumping function for debuggin function dumpObject(obj, maxDepth) { var dump = function(obj, name, depth, tab){ if (depth > maxDepth) { return name + ' - Max depth\n'; } if (typeof(obj) == 'object') { var child = null; var output = tab + name + '\n'; tab += '\t'; for(var item in obj){ child = obj[item]; if (typeof(child) == 'object') { output += dump(child, item, depth + 1, tab); } else { output += tab + item + ': ' + child + '\n'; } } } return output; } return dump(obj, '', 0, ''); } HTML <form method="post" id="form"> <textarea name="q"></textarea><br> <input type="text" name="z"><br> <input type="button" onclick="do_it()" value="Do it!"> </form> The problem: When I call function do_it() => parser preajax_data_parse() doesnt parse second Object value (input tag value), and something going wrong with first value (textarea tah value) (undefined is shown)... Look at screenshot and you will understand [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/191520-sudden-script-stop/ Share on other sites More sharing options...
crashmaster Posted February 9, 2010 Author Share Posted February 9, 2010 I dont want to believe, that nobody knows answer... Is it so complicated ??? Quote Link to comment https://forums.phpfreaks.com/topic/191520-sudden-script-stop/#findComment-1009624 Share on other sites More sharing options...
KevinM1 Posted February 9, 2010 Share Posted February 9, 2010 Your first function seems to have some scope and default value issues. Try the following: function toEntity(str) { var return_str = ''; for(i = 0; i < str.length; ++i) { if (str.charCodeAt(i) > 127) { return_str += '&#' + str.charCodeAt(i) + ';'; } else { return_str += str.charAt(i); } } return return_str; } The reason being you already pass the str variable to the function...using the 'var' keyword in front of it designates a new str variable in local scope. Also, it's a good idea to initialize a variable to an empty string before concatenating other string data to it. If JavaScript behaves like some other languages, you're not guaranteed that the variable will be empty. Hopefully that will help somewhat. I'd look at your overall logic, too. If I'm reading things right, you're invoking toEntity on both the name and value fields of your JSON objects created by serializeArray. You may not want to do that. Quote Link to comment https://forums.phpfreaks.com/topic/191520-sudden-script-stop/#findComment-1009639 Share on other sites More sharing options...
crashmaster Posted February 9, 2010 Author Share Posted February 9, 2010 Your first function seems to have some scope and default value issues. Try the following: function toEntity(str) { var return_str = ''; for(i = 0; i < str.length; ++i) { if (str.charCodeAt(i) > 127) { return_str += '&#' + str.charCodeAt(i) + ';'; } else { return_str += str.charAt(i); } } return return_str; } The reason being you already pass the str variable to the function...using the 'var' keyword in front of it designates a new str variable in local scope. Also, it's a good idea to initialize a variable to an empty string before concatenating other string data to it. If JavaScript behaves like some other languages, you're not guaranteed that the variable will be empty. Hopefully that will help somewhat. I'd look at your overall logic, too. If I'm reading things right, you're invoking toEntity on both the name and value fields of your JSON objects created by serializeArray. You may not want to do that. Particulaty you are right, butproblem was somewhere else. The problem was in variable "i" In both functions "i" was not declared as local var, so it caused problems )) FOR EVERYBODY !! USE VAR DECLArATION !!! Quote Link to comment https://forums.phpfreaks.com/topic/191520-sudden-script-stop/#findComment-1009661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.