Jump to content

Recommended Posts

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]

Link to comment
https://forums.phpfreaks.com/topic/191520-sudden-script-stop/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/191520-sudden-script-stop/#findComment-1009639
Share on other sites

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 !!!

Link to comment
https://forums.phpfreaks.com/topic/191520-sudden-script-stop/#findComment-1009661
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.