www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23

Thread: [RESOLVED] Very simple method doesn't work reliably

  1. #16
    Join Date
    Jul 2010
    Posts
    34
    Well, I was having a similar problem in another script and I fixed it by switching the single quotes for double quotes within the index definition.

    so myArray["this"] instead of myArray['this'].

    But here it didn't work at all.
    Last edited by Errant_Shadow; 07-30-2010 at 04:02 AM.

  2. #17
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Errant_Shadow View Post
    Well, I was having a similar problem in another script and I fixed it by switching the single quotes for double quotes within the index definition.

    so myArray["this"] instead of myArray['this'].

    But here it didn't work at all.
    Technically, in JavaScript there is no difference between single and double quotes as delimiter, except the case when there are the same type of quotes inside the string to be treated literally. If so, the inner quotes must be escaped.
    Code:
    var myString="It's my life";
    // or
    var myString='It\'s my life';
    It is not the same as in PHP, so I don't see how you could fix a problem by switching the quotes.
    Last edited by Kor; 07-30-2010 at 04:50 AM.

  3. #18
    Join Date
    Jul 2010
    Posts
    34
    Okay. By commenting out every bit of my javascript except this array and the function that uses it, I have deduced that the problem is in fact stemming from somewhere else in the code...

    *rubs his face* is there a good way to debug javascript? In PHP if you have some sort of syntax error or what not, it tells you what and where. This is my first real experience with Javascript, so is there something like that for this?

  4. #19
    Join Date
    Jul 2010
    Posts
    34
    Quote Originally Posted by Kor View Post
    Technically, in JavaScript there is no difference between single and double quotes as delimiter, except the case when there are the same type of quotes inside the string to be treated literally. If so, the inner quotes must be escaped.
    Yes, I'm aware of that. What I'm saying is that when I used single quotes, it was broken. Then I switched to escaped double quotes and it worked. Don't know why, don't know how. All I know is that it did.

  5. #20
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You probably have written the JavaScript code on using a PHP echo. Now, the quotes problem was not a JavaScript problem, but a PHP one, thus you could have solved using the escape as well.

    But, really, I am confused now. Does your code works, or not? Are we talking about JavaScript or PHP?

  6. #21
    Join Date
    Jul 2010
    Posts
    34
    Quote Originally Posted by Kor View Post
    You probably have written the JavaScript code on using a PHP echo. Now, the quotes problem was not a JavaScript problem, but a PHP one, thus you could have solved using the escape as well.

    But, really, I am confused now. Does your code works, or not? Are we talking about JavaScript or PHP?
    Let me back up.

    I'm using PHP to parse MySQL data into JavaScrip arrays that I use to control data as the end user fills out a form.

    I'm using the array method as opposed to the object method for 2 reasons: 1) it is a simpler method to generate using PHP for loops, and 2) when I write "alert(myArray);" it traces all the values held in myArray, where if I write "alert(myObject);" it traces "[object Object]".

    After much work, I deduced that the problem I was facing was actually stemming from a completely separate function that was sitting quite a few lines up from the one I was working on. So at this time I'm just trying to figure out what the problem is so I can fix it.

    Additionally, I'm wondering if JavaScript has a debug feature similar to PHP where an error will be printed to the screen with information regarding the type and location of the error.

  7. #22
    Join Date
    Jul 2010
    Posts
    34
    Okay now. I've tracked the error to a single line and determined the malfunction.

    I have a function that analyzes the selection from a select box and uses that data to set the display of the appropriate div element for the end user to supply additional information if necessary.

    I had a need to execute this function on load in case POST data contained a selected value that would require additional fields to be made visible before the end user supplied any input. The problem was that if this function were executed with an empty string (or any invalid data) then it would cause an error when the function passed that string to the function that sets the visibility of the div element.

    Basically, since that data is used to assemble the appropriate id, if invalid data was passed then it would target a div element that doesn't exist; and that is what was causing my error...

    What I had to do to find it was to eliminate all interference from other scripts and processes by commenting them out; there was no fault in my array nor function at all.

    Moral: some scripts don't play well with others. sometimes you gotta put em in time out to sort out who did what.

  8. #23
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    1. Well, there is no reason for PHP should use more code lines to generate a JavaScript Object than a JavaScript Array:
    Code:
    <script type="text/javascript">
    //creates an array
    var array=[];
    
    <?php
    $myArray=array('value1','value2','value3');
    $i=0;
    foreach($myArray as $key){
    echo "array[".$i."]='".$key."';";
    $i++;
    }
    ?>
    
    var arrString='';
    for(var i=0;i<array.length;i++){
    arrString+=(i+' : '+array[i]+' | ');
    }
    alert(arrString);
    
    //creates an object
    var object={};
    
    <?php
    $myObject=array('value1'=>'property1','value2'=>'property2','value3'=>'property3');
    foreach($myObject as $key=>$value){
    echo "object['".$myObject[$key]."']='".$key."';";
    }
    ?>
    
    var objString='';
    for(var p in object){
    objString+=(p+' : '+object[p]+' | ');
    }
    alert(objString);
    </script>
    2. That is normal. on alert() an array display its elements. But in practice you don't need the alert, you need the values. To circle through the elements of an array, use an ordered for loop for(var i=0;i<array.length;i++). To circle through the pairs property:value of an object use an unordered loop for(var p in object) (where the variable p is implicit - you may name it whichever - it will automatically get the value of each property in a loop)
    Last edited by Kor; 07-30-2010 at 06:16 AM.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles