Client Side Things
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesClient Side Things

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old May 12th, 2003, 06:06 PM
mearnh mearnh is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 30 mearnh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
JavaScript question

I got this piece of javascript from someone on this forum and I forgot who it was, sorry and THANKS it has proven to be very useful......until now.

This used to work but I've transfered my code to another server and now it doesn't work, I've been told the only difference is that one of the PHP GLOBAL options has been truned off due to security risk. No biggie, but for some reason my code won't work.

Purpose - I'm trying to dynamically create an array from a select menu.

Here is the error that I get:
"Error: 'document.forms.myform.elements' is null or not an object"

I know what is happening (I think) I just don't know how to fix it. I think the value that I'm passing is getting lost some how. I tried to add some output statements to see what was happening and this is what I get for output:
IM HERE
this is el:[object]:
el length:0:
SHOW ARRAY

Its missing the addtoarray() function which is whats causing me to think that the value being passed (el) is getting lost somehow.....

Here is the javascript code:

php Code:
Original - php Code
  1.  
  2. <SCRIPT TYPE="TEXT/JAVASCRIPT">
  3.  
  4. var selected_array = new Array();
  5.  
  6. function build_array(el)
  7. {
  8.   document.write("IM HERE<BR>");
  9.   document.write("this is el:" + el + ":<BR>");
  10.   document.write("el length:" + el.length + ":<BR>");
  11.  
  12.   for (var i = 0; i < el.length; i++)
  13.   {
  14.     var val = el[i].value;
  15.  
  16.     if (el[i].selected)
  17.     {
  18.       addtoarray(val);
  19.     }
  20.     else
  21.     {
  22.       removefromarray(val);
  23.     }
  24.   }
  25.  
  26.   showArray(val);
  27. }
  28.  
  29. function addtoarray(val)
  30. {
  31.   document.write("ADD TO ARRAY<BR>");   
  32.   for (var i = 0; i < selected_array.length; i++)
  33.   {
  34.     if (selected_array[i] == val) return;
  35.   }
  36.   selected_array[i] = val;
  37. }
  38.  
  39. function removefromarray(val)
  40. {
  41.   document.write("REMOVE FROM ARRAY<BR>")
  42.   for (var i = 0; i < selected_array.length; i++)
  43.   {
  44.     if (selected_array[i] == val)
  45.     {
  46.       selected_array.splice(i, 1);
  47.       return;
  48.     }
  49.   }
  50. }
  51.  
  52. function showArray(array)
  53. {
  54.   document.write("SHOW ARRAY<BR>")
  55.   arraytext = "";
  56.     for (var i=0; i < selected_array.length; i++)
  57.     {
  58.       if(i == 0)
  59.         arraytext += selected_array[i];
  60.       else
  61.         arraytext += ":" + selected_array[i];
  62.     }
  63.  
  64.   document.forms['myform'].elements['array_build'].value = arraytext;
  65. }
  66.  
  67. </SCRIPT>


Here is the form that is making the function call (mixture of PHP and HTML):

php Code:
Original - php Code
  1.  
  2. <FORM NAME="myform">
  3. <SELECT NAME="myselect" MULTIPLE="multiple" SIZE="10" ONCHANGE="build_array(this)">
  4. <?php
  5. while ($row = $result->fetchRow($mode))
  6. {
  7.     $trimed = trim($row[0]);
  8.     echo "<OPTION VALUE="$trimed">$trimed</OPTION>";
  9. }
  10.  
  11. $result->free();
  12. ?>
  13. </SELECT>
  14. <BR>
  15. <INPUT TYPE="hidden" NAME="array_build" ><BR>
  16. <INPUT TYPE="submit"  VALUE="SELECT">
  17. <INPUT TYPE="reset"  VALUE="RESET"><BR>
  18. </FORM>


anybody got any ideas????

Reply With Quote
  #2  
Old May 12th, 2003, 08:07 PM
crisp crisp is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Holland
Posts: 336 crisp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: JavaScript question

looks familiar

the errormessage you are getting is not so strange concidering the debug-method you are using. By using document.write after the page is already parsed in the browser you are actually overwriting the original page, so by the time the script tries to fill the hidden field, it simply doesn't exist anymore.

I don't think the problem is in the javascript, but is indeed in the form-handling page. Because of the different setting of PHP (the register_globals setting) you will need to use the so-called superglobals to get to your GET and POST data.
In this case it means you cannot just use $array_built in your PHP script, but you need to use $_GET['array_built'] or $_POST['array_built'] (depending on wether it is a GET or a POST method form).

Reply With Quote
  #3  
Old May 12th, 2003, 08:44 PM
mearnh mearnh is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 30 mearnh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: JavaScript question

crisp-

YES, it should look very familuar. It helped me out tremendously. Thanks again.

I fear I need your help again.
I understand your explenations about the superglobals, but I don't even think its making it to the javascript correctly. I can't even seem to get the paramater passed to the javascript.

I added this code:

php Code:
Original - php Code
  1.  
  2. function show_props(obj, obj_name)
  3. {   
  4.     document.write("INSIDE SHOW PROPERTIES<BR>");
  5.    var result = "";
  6.    for (var i in obj)
  7.    {
  8.       result = obj_name + "." + i + " = " + obj[i] + "<BR>";
  9.       document.write(result);
  10.    }
  11.    return result
  12. }


and I get back a bunch of stuff similar to this, but the interesting part is the .value object is null which means to me that it isn't even passing the parameter correctly:

php Code:
Original - php Code
  1.  
  2. INSIDE SHOW PROPERTIES
  3. el.language =
  4. el.scrollHeight = 0
  5. el.isTextEdit = true
  6. el.currentStyle = null
  7. el.document = [object]
  8. el.onmouseup = null
  9. ...
  10. ...
  11. ...
  12. el.size = 20
  13. el.value =
  14. el.border = 


Maybe I am missing something.

Also, is there a better method for debugging? I'm certanily open to suggestions. Currently I'm using IE6 as my web browser.

TIA,
mike

Reply With Quote
  #4  
Old May 12th, 2003, 09:15 PM
crisp crisp is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Holland
Posts: 336 crisp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: JavaScript question

your very first document.write already overwrites the page with everything in it.
I'd say in this case the best debugging method would be to change

<INPUT TYPE="hidden" NAME="array_build" ><BR>

into

<INPUT TYPE="text" NAME="array_build" ><BR>

so you can actually see what is written to it. Alternatively you can use alert() to display a value.
Mozilla has a built-in javascript debugger which can sometimes also be helpfull (Mozilla's javascript errormessages also make more sence).

But anyway, I just tested the code after removing the document.write's and it looks to me like it is working just fine.
Else could you maybe post the parsed HTML (using view-source from your browser)?

Reply With Quote
  #5  
Old May 13th, 2003, 03:39 PM
mearnh mearnh is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 30 mearnh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: JavaScript question

crisp-
I get the feeling i'm in way over my head.
Something you are telling me just isn't sinking in. I feel like I'm missing something.

OK, I have taken out the document.write statements and un-hid the array_build field in the form.

Here is the parsed HTML:
php Code:
Original - php Code
  1.  
  2.  
  3. <HTML>
  4. <HEAD>
  5. <TITLE>select build2</TITLE>
  6. <SCRIPT TYPE="TEXT/JAVASCRIPT">
  7.  
  8. var selected_array = new Array();
  9.  
  10. function build_array(el)
  11. {
  12. //  document.write("IM HERE<BR>");
  13. //  document.write("this is el:" + el + ":<BR>");
  14. //  document.write("el length:" + el.length + ":<BR>");
  15. //  show_props(el, "el");
  16.  
  17.   for (var i = 0; i < el.length; i++)
  18.   {
  19. //      document.write("INSIDE FOR<BR>")
  20.     var val = el[i].value;
  21.  
  22.     if (el[i].selected)
  23.     {
  24.       addtoarray(val);
  25.     }
  26.     else
  27.     {
  28.       removefromarray(val);
  29.     }
  30.   }
  31.  
  32. //  document.forms['myform'].elements['array_build'].value = selected_array.toString();
  33.  
  34.   showArray(val);
  35. }
  36.  
  37. function addtoarray(val)
  38. {
  39. //  document.write("ADD TO ARRAY<BR>"); 
  40.   for (var i = 0; i < selected_array.length; i++)
  41.   {
  42.     if (selected_array[i] == val) return;
  43.   }
  44.   selected_array[i] = val;
  45. }
  46.  
  47. function removefromarray(val)
  48. {
  49. //  document.write("REMOVE FROM ARRAY<BR>");   
  50.   for (var i = 0; i < selected_array.length; i++)
  51.   {
  52.     if (selected_array[i] == val)
  53.     {
  54.       selected_array.splice(i, 1);
  55.       return;
  56.     }
  57.   }
  58. }
  59.  
  60. function showArray(array)
  61. {
  62. //  document.write("SHOW ARRAY<BR>");   
  63.   arraytext = "";
  64.     for (var i=0; i < selected_array.length; i++)
  65.     {
  66.       if(i == 0)
  67.         arraytext += selected_array[i];
  68.       else
  69.         arraytext += ":" + selected_array[i];
  70.     }
  71.  
  72.   document.forms['myform'].elements['array_build'].value = arraytext;
  73. }
  74.  
  75. function show_props(obj, obj_name)
  76. {   
  77. //  document.write("INSIDE SHOW PROPERTIES<BR>");
  78.    var result = "";
  79.    for (var i in obj)
  80.    {