#1
  1. PHP guru in training :p
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Moved to Freezer
    Posts
    83
    Rep Power
    12

    arrays and javascript


    Hi guys,
    just wondering... this little bit here doesn't work, it comes up with a javascript error, 'document.form.week.3' is not an object. how do I get javascript to like names with arrays?

    PHP Code:
    echo '<tr><td align="center" colspan="15"><input type="text" name="week['.$c.']" value="'.$sem.'">

    <a href="java script:show_calendar('
    document.form.week['.$c.']', document.form.week['.$c.'].value);">

    <img src="cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the timestamp"></a>    </td></tr>'

    cheers guys
    Jon
    P.S.: posted this in javascript forum too, but had no reply yet
  2. #2
  3. Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2000
    Location
    Enschede, The Netherlands
    Posts
    1,527
    Rep Power
    15
    You need to do 2 things:

    1) find out the correct javascript (that's something you have to ask in the javascript board... without the PHP would be better I think)

    2) find out how to echo it correctly with PHP.. that's something you could ask here...

    Since the 2 are mixed right now I have no idea (nor desire to find out) which is the cause for the error since I'm not expert in JS. Just a hunch though: check your single quotes! there's one in front of document.form.week which suggests the string is over there (which isn't the case) -> could be that's the reason why the printed result (which is....??? btw) is giving errors...
    There are 10 types of people in this world - those who understand binary and those who don't...

    PHP | MySQL | DevShed Forum Search | Google Search
  4. #3
  5. Gogo Google.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Location
    Adelaide, Australia
    Posts
    226
    Rep Power
    12
    First off, do you have the appropriate quotes escaped in that echo() block?

    The Javascript *looks* ok, but I don't have a broad knowledge of JS. But the quotes look a bit funny to me, are you sure you've escaped them properly?

    vB is notorious for removing slashes in the wrong places, so we can't be sure
    Adam Goossens
  6. #4
  7. PHP guru in training :p
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Moved to Freezer
    Posts
    83
    Rep Power
    12

    Well....


    VB did get rid of the backslashes that were before the document bit, and after it. but the thing is that it sends back the error once I click on it. otherwise the link is correct, it sends it to document.form.week[0]! but like I said, when I actually click on it, there is an error, and it says it was document.form.week.0!!! php is sooooo much better than javascript, but got no idea how to do it in php, so....

    thanks for ur replies guys, you were a lot quicker at giving me a response than the javascript lot.
    Jon
  8. #5
  9. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    7
    Rep Power
    0
    'document.form.week.3' is not an object := there is no object in the document tree that you gave this name. The use of [] for the name of an element looks ambiguous; you better try something like this:
    PHP Code:
    <tr><td align="center" colspan="15"><input type="text" name="week_<% echo $c%>" value="<% echo $sem %>"
    if $c==3 you should have this output:

    PHP Code:
    <tr><td align="center" colspan="15"><input type="text" name="week_3" value="whatever"
    try to call the javascript by hand (for debugging purpose) like this:


    PHP Code:
    <a href="javascript:show_calendar('document.form.week_3, document.form.week_3.value)"
  10. #6
  11. PHP guru in training :p
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Moved to Freezer
    Posts
    83
    Rep Power
    12
    yeah,
    but I have a problem though, is that I have already done everything with arrays, the whole thing works, but so that people don't type in the date in the wrong format (i.e. instead of 23/4/2003 they type in 23042003 or 23-4-2003) I want to put the text input in readonly, and use the javascript calendar to keep a similar input everytime.
    so all I'm actually trying to do is find out how to use the array as a value in javascript. I know this question is very javascipt oriented, but I haven't got any answers from the javascript forum.
    Jon
  12. #7
  13. No Profile Picture
    Dissident
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2003
    Location
    New York
    Posts
    1,671
    Rep Power
    49
    If you can PM me the full code I can take a look at it, but as it stands, there are far too many things that could be causing your problem.

    Off the top of my head though I can say this:

    document.form.week.3 does not look like it's an object the way you have things named.

    Here's where you're running into trouble (I think):

    HTML allows you to use brackets [ and ] in it's object names, but when you try to access that object via javascript, javascript gets confused and thinks you are looking for an array element. You can probably get around this by accessing your HTML elements in a different manner.

    I recently had a similar problem for a quick js search function i was writing that was accessing a field named "item_buyers[]". I originally tried using an eval statement but it kept evaluating item_buyers[] to an array, so I had to use a different approach.

    Here's how i worked around it:

    PHP Code:
    function fnSearchsearchFieldsearchItem )
    {
        
    thisForm document.post;

        
    searchBox thisForm.elements[searchField];
        
    selectBox thisForm.elements[searchItem];

        
    reSearch = new RegExpsearchBox.value"i" );

        
    len selectBox.options.length;
        for ( var 
    0leni++ ){
            if( 
    reSearch.testselectBox.options[i].value ) ){
                
    selectBox.options[i].selected true;
                break;
            }
        }

    Here i am passing the name of the text box that gets the user's search as searchField and the name of the select box being searched as searchItem

    Then in the text box I put:
    PHP Code:
    onKeyUp="fnSearch( this.name, 'item_buyers[]' )" 
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    226
    Rep Power
    12
    This will not work: document.form.week['.$c.']

    this may work:
    var form = document.forms("whatever the form name is");

    var weeks = form.elements["week[5]"] //lets say $c = 5

    var week = weeks[0];

    Now this variable week points to the textfield.

    by doing this way, I think you have to modify the javascript function show_calendar so that it does not take any argument and it gets the week inside the function.
  16. #9
  17. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    thanks for ur replies guys, you were a lot quicker at giving me a response than the javascript lot.
    And a lot more courteous than you were when dealing with those 'javascript lot'... I know the answer to this, and would have helped you to it - but you clearly have an attitude and, when you weren't spoon-fed an answer you liked, took off without even an acknowledgement. Good way to get an answer.

    Hi draelon...just a quick tip: JS allows you to name/reference any object property with whatever character string you want - as long as you use hash references, and not dot syntax. Object.property[] is illegal (square brackets are index operators) - but Object['property'] is swell, as is myObject['gdye**763Nmk.'] JS objects are hashtables, patterned from Perl. That's why

    searchBox = thisForm.elements['item_buyers[]']

    ..cuts it.

    http://www.crockford.com/javascript/survey.html

IMN logo majestic logo threadwatch logo seochat tools logo