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: 3
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: 3
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: 3
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: 3
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: 3
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.    {
  81.       result = obj_name + "." + i + " = " + obj[i] + "<BR>";
  82.       document.write(result);
  83.    }
  84.    return result
  85. }
  86.  
  87. </SCRIPT>
  88.  
  89. </HEAD>
  90. <BODY BGCOLOR="#000000" TEXT="#00e600">
  91. <!--
  92. <FONT COLOR="#FFFFFF"><center><h1>SELECT BUILD2</h1></center></FONT>
  93. -->
  94. <H2>Select Build(s)</H2>
  95. <FORM NAME="myform" METHOD="get">
  96. <SELECT NAME="myselect" MULTIPLE="multiple" SIZE="10" ONCHANGE="build_array(this)">
  97. <OPTION VALUE="A170">A170</OPTION>
  98. <OPTION VALUE="B170">B170</OPTION>
  99. <OPTION VALUE="C170">C170</OPTION>
  100. <OPTION VALUE="DQ06">DQ06</OPTION>
  101. <OPTION VALUE="EQ06">EQ06</OPTION>
  102. <OPTION VALUE="FOOBAR">FOOBAR</OPTION>
  103. <OPTION VALUE="FQ06">FQ06</OPTION>
  104. <OPTION VALUE="GQ06">GQ06</OPTION>
  105. <OPTION VALUE="GQ06_TEST">GQ06_TEST</OPTION>
  106. <OPTION VALUE="HQ06">HQ06</OPTION>
  107. <OPTION VALUE="HQ06_TEST">HQ06_TEST</OPTION>
  108. <OPTION VALUE="IQ06">IQ06</OPTION>
  109. <OPTION VALUE="IQ06_RESEARCH">IQ06_RESEARCH</OPTION>
  110. <OPTION VALUE="LQ06">LQ06</OPTION>
  111. <OPTION VALUE="MQ06">MQ06</OPTION>
  112. <OPTION VALUE="NQ06">NQ06</OPTION>
  113. <OPTION VALUE="PQ06">PQ06</OPTION>
  114. <OPTION VALUE="QQ06">QQ06</OPTION>
  115. <OPTION VALUE="RQ06">RQ06</OPTION>
  116. <OPTION VALUE="SQ06">SQ06</OPTION>
  117. <OPTION VALUE="SoothSayer">SoothSayer</OPTION>
  118. <OPTION VALUE="SoothSayer_test">SoothSayer_test</OPTION>
  119. <OPTION VALUE="TQ06">TQ06</OPTION>
  120. </SELECT>
  121. <BR>
  122. <INPUT TYPE="text" NAME="$_GET['array_build']" ><BR>
  123. <INPUT TYPE="submit"  VALUE="SELECT">
  124. <INPUT TYPE="reset"  VALUE="RESET"><BR>
  125. </FORM> 
  126.  
  127. </BODY>
  128. </HTML>


The more I sit here an think about this the more I get confused....OK, I could see where the JS code is working fine, its just not accessing/recegnizing the array_build element in the form in order to set its value. Have I started down the right path...

mike

Reply With Quote
  #6  
Old May 13th, 2003, 07: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: 3
RE: JavaScript question

Now I spot the problem right away:

<INPUT TYPE="text" NAME="$_GET['array_build']" >

this should be:

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

(remember that HTML is not PHP ;) )

Reply With Quote
  #7  
Old May 14th, 2003, 01:56 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: 3
RE: JavaScript question

!HA!

CRISP-
I owe you one yet again.
That fixed it.
Now that I sit down and think about it, and look at the error, It suddenly becomes very clear to me now.

Thanks for you help.
I hope that someday I can repay you.
mike

Reply With Quote
  #8  
Old May 14th, 2003, 06:41 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: 3
RE: RE: JavaScript question

Quote:
!HA!

CRISP-
I owe you one yet again.
That fixed it.
Now that I sit down and think about it, and look at the error, It suddenly becomes very clear to me now.

Thanks for you help.
I hope that someday I can repay you.
mike

no problem; just try helping others like I do and one day we'll all be better programmers

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesClient Side Things > JavaScript question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek