Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic Posting of Form 1

Status
Not open for further replies.

gcaplan

Programmer
Sep 28, 2000
3
GB
Hi

Is it possible to achieve the following?

Using CGI, I would be creating an HTML page with a number of links, and a session ID in a hidden field.

What I would like to do is this:

Whenever a link is clicked, use JavaScript to write the clicked URI as the ACTION attribute of the form, and automatically POST the form.

I can't see any way to do this, but as I am rather new to JavaScript perhaps I am missing something.

The solution would have to be robust for all major browsers from version 3 and later.

Any advice would be much appreciated

Geoff Caplan [sig][/sig]
 
It's very easy, actually.

<HTML>
<HEAD>
<SCRIPT Language=&quot;Javascript&quot;>

function post_to_link(link_loc){

document.the_form_name.action = link_loc;
document.the_form_name.submit();

}

</SCRIPT>
</HEAD>
<BODY>
<FORM name=&quot;the_form_name&quot; method=&quot;POST&quot; action=&quot;&quot;>
<INPUT TYPE=&quot;HIDDEN&quot; name=&quot;session&quot; value=&quot;sessionIDno&quot;>
<A HREF=&quot;#&quot; onclick=&quot;post_to_link('thelink.html')&quot;>The Link</A>
<A HREF=&quot;#&quot; onclick=&quot;post_to_link('anotherlink.html')&quot;>Another Link</A>
</FORM>
</BODY>
</HTML>

This should work for all version 3 browsers and up, with Javascript enabled. All of these methods were included in Javascript 1.0 and up.

Now, the only problem with this approach is anyone who hasn't turned off the standard browser warning for form posting will be presented with this every time they click a link on your page. It could get annoying. Why don't you want to just use cookies, or use URL arguments? [sig][/sig]
 
rycamor

Many thanks for the suggestion. Looking at your solution I realise that I didn't make it clear that the links would need to look like 'normal' links to a search engine spider. I suspect that this is not possible though - I can't see any event in JavaScript that would capture a click on a normal link...

>>>>>>>
>Now, the only problem with this approach is anyone who >hasn't turned off the standard browser warning for form >posting will be presented with this every time they click >a link on your page. It could get annoying.
>>>>>>>

You are right. I hadn't though about this.

>>>>>>>>
>Why don't you want to just use cookies, or use URL >arguments?
>>>>>>>>

For various reasons we are very keen to build a dynamic storefront that will look like a static site to the search engines so that it will be indexed. As you will know, URL arguments are a major turn-off for most spiders. On the server side we will be using PHP. The idea is to use Apache to direct all requests to the domain to a single script. This allows us to use &quot;virtual&quot; URIs, and our top level script will parse them in order to determine the arguments it will use to construct the page. This way, the URIs are very clean.

Of course, cookies are the easiest way to go on the session side. But with all the current paranoia about cookies, and the fact that so many people shop from behind firewalls during their lunchbreak at work we are keen that the site degrades elegantly if cookies are turned off. There are a number of ways we could go, but I know very little about client side programming and thought I would check out what it can add to the mix. If you have any suggestions about how JavaScript could help here, I would be grateful for your input.

Thanks again [sig][/sig]
 
gcaplan,
yes, it's possible to do what you want. The key is the ability to capture events at the document level. If you return a value of false from a handler, the event is cancelled. So what you need to do is capture all clicks at the document level, then in the click handler, set the action and submit the form, then return false so the browser doesn't process the link. The only problem is that all clicks will be sent to the handler, not just clicks on the links. You'll have to filter them somehow. In IE, you can use the tagName property. Not sure what to use in Netscape. Maybe Rob can help here.

<script language='javascript'>
if (document.layers)
document.captureEvents(Event.CLICK);

document.onclick=MyClickEvent;

function MyClickEvent(e)
{
var sHref;
if (document.layers)
{
sHref = e.target.href
}
if (document.all)
{
if (event.srcElement.tagName == 'A')
sHref = event.srcElement.href;
}
if (sHref.length != 0)
{
document.forms[0].action=sHref;
document.forms[0].submit;
return false;
}
else
return true;
}
</script>

[sig]<p>nick bulka<br><a href=mailto: > </a><br><a href= </a><br>Get your technical books at Bulka's Books<br>
[/sig]
 
Nick

Sorry I have taken so long to reply - I am in the middle of moving house...

Thanks for the tip. It looks well worth exploring, but it will be a few days before I have got unpacked and have time to try it out.

Thanks again

Geoff Caplan [sig][/sig]
 
On another note, the best way to make a dynamic site look like a static one to search engines is to use Apache's mod_rewrite features. It's amazing what you can do with just a few regular expressions to make the external links appear totally unrelated to what goes on inside the server. Search the forums at for mod_rewrite and you will see what I mean. Also, there are a few workarounds to this with PHP. If you are interested, meet me in the PHP forum. [sig][/sig]
 
I have a dumb question.

I have been looking for a way to automatically post a response to a form on my website.

For one, i dont know how to post the response. Two, I would like to post it in an aesthetically pleasing way.

Can anyone help?

--druggist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top