- Create Account

Forms in DHTML scroller

Forums > DHTML Scroller > Forms in DHTML scroller
Author
Message

1) garison Group: Members
Posts: 13 Joined: 14 Nov 2004 Location: Tucson, AZ IP: 24.94.--.--
This has been covered in the forum before, but I thought to introduce it again, for those who don't want to search the whole forum.

It is difficult to use forms in the DHTML scroller. When a regular form is submitted, it opens in a new window, instead of the scroller. I found a way to fix that.

Put this function in the MAIN BODY of your page -- better still, in the divscroll.js file.

  function  submitToDiv(f,d) {
    var  end, url, amp, div, nam, val;
    end = f.elements.length;
    url  = f.action + (f.action.indexOf('?')<0? '?' : '&');
div = eval(d);
    amp = '';
for (i=0; i<end; i++) {
      p = f.elements[i];
nam = escape(p.name).replace(/%20/g,'+')
      val = escape((p.type.substring(0,6)=="select")  ?
          p.options[p.selectedIndex].value :
          p.value).replace(/%20/g,'+')
url += amp + nam + '='  + val;
      amp = '&';
    }
div.load(url);
    return  false;
  }

In  your included HTML pages, add the function call to your forms:

<form action="handler.cgi" action="post"
          onsubmit="return submitToDiv(this,'contentDiv')">

where "contentDiv"  is the name of (you guessed it) your DHTML scroller. The action should probably be a fully-realized URL, such as "http://myurl.com/forms/handler.cgi" -- in some cases, a relative URL did not work for me.

That's all you have to do. The function extracts the action from the form, and accounts for the possibility that it might already have a query string. It handles all types of form inputs (but does not disquise passwords). The value and name of each item are escaped, as required for some shopping carts.

Currently, it doesn't handle the values of submit buttons, but I'm working on that. If you have only one submit button, you can use a hidden field to pass that value. When I have submit-button support added, I'll post that here.

Angus should think about including this (or something like it) in his next release.

2) Angus Turnbull Group: Moderators
Posts: 4042 Joined: 7 Dec 2003 Location: New Zealand IP: 210.54.--.--
Nicely done -- thanks :). I was thinking "oh, not again!" when I saw your topic title, too, until it turned out to be a solution not a question!

I'm actually putting more effort into the HTMLHttpRequest script these days; it's a modified variant of the scroller script's loader component, which when you combine with a div like this:

<div style="width: 300px; height: 200px; overflow: auto"></div>

will load content and scroll with the browser's native scrollbars (try it out!). So once I get a few good demos of that up and running I might start pushing that as the "best" loader/scroller (kind of like how I'm now recommending the FreeStyle Menus over the Cascading Popup Menus script) as it's less complicated and more standards-based.

In any case, I have to work on form submission for a client with that script; doing GETs like you've posted is quite possible, but I'm also thinking up crazy schemes to copy form contents into the hidden IFRAME and get actual POSTs working too ;). Stay tuned, it should be interesting if I can get it working!

Cheers - Angus.

3) garison Group: Members
Posts: 13 Joined: 14 Nov 2004 Location: Tucson, AZ IP: 24.94.--.--
Okay the above won't work exactly as I figured. It includes checkboxes and radio buttons when they aren't set, and only does one value of multiple select boxes. :(

So  the for loop above should be replaced with:
    for (i=0;  i<end; i++) {
      p = f.elements[i];
typ = p.type.substring(0,6);
      nam = escape(p.name).replace(/%20/g,'+');
if (nam!='') {
        if  (typ=="select") {
          for (j=0;  j<p.length; j++) {
          if (p.options[j].selected)  {
          val = escape(p.options[j].value).replace(/%20/g,'+');
          url  += amp + nam + '=' + val;
amp = '&';
          }
          }
} else if ((typ=="radio") || (typ=="checkb")) {
if (p.checked) {
          val  = escape(p.value).replace(/%20/g,'+');
          url += amp + nam  + '=' + val;
          amp = '&';
}
        } else {
          val  = escape(p.value).replace(/%20/g,'+');
          url += amp + nam  + '=' + val;
          amp = '&';
}
      }
    }

There is probably a prettier, more concise way of doing this, but I can't spend the time to find it. This will do for now.

-garison

4) Marielle Group: Guests
IP: 81.221.--.--
Hi garison. Thank you so much. I spent 2 days on this problem, because I didn't think about consulting this forum :-( Now you solved my problem within 2 minutes.
Great. Thanks.

By the way Divscroll is great too.


5) pluz Group: Guests
IP: 81.6.--.--
dear angus
i did a page with the divscroll and there is a form in it. i had the known problem that after the submit the page didn't open in the content. i saw the solutions of garison. but it doesn't work.
the first one is opening in the content but it has like garison mentioned the problem with the radio buttons.
so i included the second script. now it shows me instead of the page only the loading message. and if i click on a link from the menu (target="content") the page opens in a second window.
is there a solution for the problem with the radio buttons?

6) Angus Turnbull Group: Moderators
Posts: 4042 Joined: 7 Dec 2003 Location: New Zealand IP: 202.137.--.--
Please try the HTMLHttpRequest script, it's a lot newer and has solid form support built into the core script.

Sounds like you're getting a JS error currently with this script that is preventing it from executing, if you don't want to use HTMLHttpRequest try fixing that (check Firefox's JavaScript Console).

GOod luck - Angus.

Post a Reply:

You are not logged in, and will be posting anonymously as a guest. If you want to post using an account, please login at the top of this page.