SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,094
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Dumb Noob question about calling functions

    The code I have is,
    Code:
    <script type="text/javascript" src="ajax.js"></script>
    
    <script type="text/javascript">
     function BuildPostVars(form1) {     
     return str = 'fname=' + form.fname.value + '&comment=' + form.comment.value; 
     }  
     </script>
     
    <form name="form1" method="post" action="" id="event_form">
    	<input type="text" name="fname" value=""><br>
        <input type="text" name="comment" value=""><br>
    <div id="output"><input type="button" name="action" value="Send" onclick="SubmitNewEvent('event_form', 'output');"></div><br>
        </form>
    <br>
    <script type="text/javascript">
     function BuildPostVars(form2) {     
     return str = 'fname2=' + form.fname2.value + '&comment2=' + form.comment2.value; 
     }  
     </script>
    
    <form name="form2" method="post" action="" id="event_form2">
    	<input type="text" name="fname2" value=""><br>
        <input type="text" name="comment2" value=""><br>
    <div id="output2"><input type="button" name="action" value="Send" onclick="SubmitNewEvent('event_form2', 'output2');"></div>
    </form>
    This is an ajax app that I am hacking together. I have a php script that builds about 150 forms depending on what is in an rss feed.

    I need to submit each form without a page refresh.

    The problem I have is that if I submit the first form I get the error that form2 is null or not an object. I see what is happening but have no idea if it can be prevented short of creating 150+ functions in ajax.js.

    I do have an alternative ajax solution where php will count all items and build a monsterous JavaScript on the fly. It requires an extra "for" loop and if I can get the script I posted to work, it would be a nice solution.

    Any ideas and comments greatly appreciated.

    Thanks
    Loren
    What I lack in acuracy I make up for in misteaks

  2. #2
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,094
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I am trying something different and it works so there are no more problems.
    What I lack in acuracy I make up for in misteaks

  3. #3
    Google Engineer polvero's Avatar
    Join Date
    Oct 2003
    Location
    Mountain View
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah. Basically you shouldn't be redeclaring the same functions more than once. Eg: 'BuildPostVars'
    You can however override them with assignment:

    Code:
    var f = function() {
      // something...
    };
    .
    .
    .
    .
    f = function() {
      // something else...
    };
    But in all best interests, you'd want to have the function take in an argument and point to which form you want. That is, in fact, the point of functions in the first place - so you wouldn't have to duplicate so much code.


Bookmarks

Posting Permissions

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