SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    use js functions only for particular forms

    I have a nice little js script for using XHR to submit forms and get the results back in hidden div. My problem is that it currently is setup to work with all forms on the page. I only want it to work particular forms. Here is my code

    http://pastebin.ca/492590

    Im thinking i need to change line 85 and then add action on the submit buttons on the forms i want to use it with. Im really horrible at js, so any help is really appreciated.

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    you could either reference the form by ID or class

    if you want to apply the functions to one form then use ID, but you could set a function that iterates through all the forms, and see if one has the className of, for instance, jsapply, and if one is found apply the function.

    Hope it's of help
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Markdidj View Post
    you could either reference the form by ID or class

    if you want to apply the functions to one form then use ID, but you could set a function that iterates through all the forms, and see if one has the className of, for instance, jsapply, and if one is found apply the function.

    Hope it's of help
    Thanks for the idea about className, i will definitely look into that. Since i stink at js, could you by chance give me a example function for checking for a className? Tks!

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function findClass() {
    var x=document.getElementsByTagName('form');
    for(i=0;i<x.length;i++) {
    if(x[i].className.indexOf("class_named_form")>-1) {
    do_something();
    }
    }
    }
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  5. #5
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the sample code, but i wasnt able to get it to work. Here is part of the original code.

    Code:
    // Initialize forms:
    window.onload = ajaxInit;
    function ajaxInit(){
    	for (var i = 0; i < document.forms.length; i++){
    		var curForm = document.forms[i];
    		if (curForm.method == "post") {
    			curForm.onsubmit = post_method; // Apply post_method to *every* form on the page
    		} else if (curForm.method == "get") {
    			curForm.onsubmit = get_method;
    		}
    	}
    }
    
    		                <form action="" method="post" id="{$ip}chk">
            			        <input type="hidden" value="revSubmit.php" name="resultspage" id="resultspage" />
    			                <input type="hidden" value="check" name="mode" />
    			                <input type="hidden" value="{$ip}" name="ip" />
    			                <input type="submit" value="Check" />
    	                	</form>
    and now with your changes:

    Code:
    // Initialize forms:
    function findClass() {
    	var x=document.getElementsByTagName('form');
    	for(i=0;i<x.length;i++) {
    		if(x[i].className.indexOf("ajax")>-1) {
    			return ajaxInit();
    		}
    	}
    }
    function ajaxInit(){
    	for (var i = 0; i < document.forms.length; i++){
    		var curForm = document.forms[i];
    		if (curForm.method == "post") {
    			curForm.onsubmit = post_method; // Apply post_method to *every* form on the page
    		} else if (curForm.method == "get") {
    			curForm.onsubmit = get_method;
    		}
    	}
    }
    
    		                <form class="ajax" action="" method="post" id="{$ip}chk">
            			        <input type="hidden" value="revSubmit.php" name="resultspage" id="resultspage" />
    			                <input type="hidden" value="check" name="mode" />
    			                <input type="hidden" value="{$ip}" name="ip" />
    			                <input type="submit" value="Check" />
    	                	</form>
    any idea what im doing wrong? it basically submits just like a regular form (page refresh) when i try it the new way.

  6. #6
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think it would be better to put it in your ajaxInit function, although I'm not sure where you want it, but something like this
    Code:
    function ajaxInit(){
        for (var i = 0; i < document.forms.length; i++){
            if(document.forms[i].className.indexOf("ajax")>-1){
                var curForm = document.forms[i];
                if (curForm.method == "post") {
                    curForm.onsubmit = post_method; // Apply post_method to *every* form on the page
                } else if (curForm.method == "get") {
                curForm.onsubmit = get_method;
               }
            }
        }
    }
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  7. #7
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but dont i need to somehow init the ajax function in the beginning? Should i keep this code in then?

    Code:
    window.onload = ajaxInit;
    BTW, i really appreciate your help with this. Thanks!

  8. #8
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome, combination of your last post and the window.onload = ajaxInit; worked perfectly. You are the man! Can you PM me your paypal address so i can at least buy you a drink?

  9. #9
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Not to worry dude, I only know the stuff because I've been taught it here
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


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
  •