Web Hosting Talk







View Full Version : window.onload = function()


PolishPanda
12-13-2004, 12:04 AM
I'm trying to write a java script that will submit a form on a window load.

Everything else works, just this doesn't.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en">
<head>
<title>asdf</title>
</head>


<form action="http://www.polishpanda.net/phpBB2/posting.php" method="post" name="post" onsubmit="return checkForm(this)">

<div style="margin-top:6px">
<input type="hidden" name="subject" tabindex="2" class="post" value="So what's so good about it?" />
<input type="hidden" name="f" value="1" />
<input type="hidden" name="mode" value="newtopic" />
<input type="hidden" name="message" value="http://www.polishpanda.net/wow/insane.html

Why all the threads?" />


<input type="submit" accesskey="s" tabindex="6" name="post" class="mainoption" value="Submit" />


</form>
<script type="text/javascript">
window.onload = function(){document.form["post"].submit()}
</script>
</div>
</td>

The form name is post as you can see... Why isn't it working? Upon page load a button shows up that says submit... I want it to submit on page load. You can test it out on my forums.

http://www.polishpanda.net/phpBB2/viewforum.php?f=1

Akyriel
12-13-2004, 01:21 AM
Whew! Where to start!?

I can't even fathom why you need to submit a form with hidden data automatically on loading, when you could just pass the values to your application via the url... but hey, it takes all kinds in this world. Not to mention why you need a submit button at all if it's going to be automatically submitted? *scratching head*

How I loathe bad programming.

Anyhoo:

1.) Get rid of the quotes in your 'message' field. They are causing problems in your form being rendered.

2.) Get rid of the submit button altogether, it's unnecessary.

3.) QUIT NAMING EVERYTHING 'POST'! Sorry to yell, but it's just bad programming practice.

4.) Use the accepted method for body onload instead of calling it in the middle of the page.

Here's a working example of how to do what you are asking.


<html>
<head>
<script language="javascript" type="text/javascript">
function submitForm() {
document.myform.submit();
}
</script>
</head>
<body onload="submitForm();">

<form action="formHandler.php" method="post" name="myform">
<input type="hidden" name="subject" tabindex="2" class="post" value="So what's so good about it?" />
<input type="hidden" name="f" value="1" />
<input type="hidden" name="mode" value="newtopic" />
<input type="hidden" name="message" value="<a href='http://www.polishpanda.net/wow/insane.html' target='_blank'>http://www.polishpanda.net/wow/insane.html</a> Why all the threads?" />

</form>

</body>
</html>

PolishPanda
12-13-2004, 01:49 AM
Heh, well what you posted didn't work. Oh the irony :)

Anyway, I have an idea of what is wrong. Would it have anything to do with accesskey="s", tabindex="6" or class="mainoption"? I tried making a submit button earlier without that info and I'm having the same problem as with your script. I don't know where to implement that so... :/

Here's what I have now:

<html>
<head>
<script language="javascript" type="text/javascript">
function submitForm() {
document.myform.submit();
}
</script>
</head>
<body onload="submitForm();">

<form action="http://www.polishpanda.net/phpBB2/posting.php" method="post" name="myform">
<input type="hidden" name="subject" tabindex="2" class="post" value="So what's so good about it?" />
<input type="hidden" name="f" value="1" />
<input type="hidden" name="mode" value="newtopic" />
<input type="hidden" name="message" value="http://www.polishpanda.net/wow/insane2.html Why all the threads?" />

</form>

</body>
</html>

http://www.polishpanda.net/wow/insane2.html

Thanks.

Akyriel
12-13-2004, 01:54 AM
thats funny, i tested it before posting it. Hmm.

Akyriel
12-13-2004, 02:02 AM
works perfectly in I.E. and Firefox on Windows 2000. Not sure what problem you're still having. Clear your cache and try again maybe.

PolishPanda
12-13-2004, 02:05 AM
This is the exact code I have now:

<html>
<head>
<script language="javascript" type="text/javascript">
function submitForm() {
document.myform.submit();
}
</script>
</head>
<body onload="submitForm();">

<form action="posting.php" method="post" name="myform">
<input type="hidden" name="subject" tabindex="2" class="post" value="So what's so good about it?" />
<input type="hidden" name="f" value="1" />
<input type="hidden" name="mode" value="newtopic" />
<input type="hidden" name="message" value="<a href='http://www.polishpanda.net/wow/insane.html' target='_blank'>http://www.polishpanda.net/wow/insane.html</a> Why all the threads?" />

</form>

</body>
</html>

The .html with the code above is in the same dir as posting.php.

I still think it has something to do with accesskey="s".

You can try yourself on any phpBB forum.

Akyriel
12-13-2004, 02:11 AM
the accesskey has nothing to do with your form being submitted by javascript. Try copying my 'original' code response/post and pasting it into a blank notepad. Save the file to your desktop as - submit.htm - then click on the file from your desktop and see what happens.

substitute your

http://www.polishpanda.net/phpBB2/posting.php

in the 'action' property.

ANMMark
12-13-2004, 06:15 AM
How I loathe bad programming.

3.) QUIT NAMING EVERYTHING 'POST'! Sorry to yell, but it's just bad programming practice.


I'm not entirely sure, but something tells me the pure fact that he's asking for this kind of help, he's not a pro such as yourself.

Why people can't help, without the dogging first, is beyond me.

ThorN
12-13-2004, 01:09 PM
1. PolishPanda's last code works fine for me. Maybe you have a popup blocker stoping the code or javascript disabled.
2. "hidden" fields do not need or use tabindex.
3. In the "message" field, you should <?php urlencode()?> or use &amp;quot; instead of quotes.

gogocode
12-13-2004, 06:07 PM
Originally posted by Akyriel

I can't even fathom why you need to submit a form with hidden data automatically on loading, when you could just pass the values to your application via the url.


There is a limit to the length of a URL including query string, varies brwser to browser, typically around 1k. It's possible that the user wishes to pass more than 1k of data.


.. but hey, it takes all kinds in this world. Not to mention why you need a submit button at all if it's going to be automatically submitted? *scratching head*


Because not everybody has Javascript available and he'd like his page to be accessable to as many people as possible?



4.) Use the accepted method for body onload instead of calling it in the middle of the page.



Perhaps he doesn't have access to change this. Setting the event in this manner is perfectly valid and acceptable.

gogocode
12-13-2004, 06:10 PM
Originally posted by PolishPanda
I'm trying to write a java script that will submit a form on a window load.



Fix the quotes in the message field.
Test your form in firefox/mozilla with the javascript console open so you can see what the errors are if it still doesn't work.
Remember that the onsubmit event you have on the form will not fire when you call form.submit() directly.

PolishPanda
12-13-2004, 11:03 PM
Sorry, but I don't think you guys are getting what I'm saying. I've tried it with both ie and firefox on two computers with java installed, and it still doesn't do what I want it to do.

When I execute the script (double click on it on my desktop), I get forwarded to posting.php, but the form does not go through. All I get is the posting.php form, no fields are filled in.

What I want it to do is automatically create a new topic when the link is clicked. The code I posted first worked perfectly with vBulletin. I just tried to make it work with phpBB. Try it out on your own phpBB forum to see if it works, you can use mine if you wish as well.

Thanks to everyone that helped.

Akyriel
12-14-2004, 12:05 AM
PolishPanda:

The form is indeed getting posted to posting.php as you attest to in your post. The reasons you are not seeing the form auto-filled out are based on other factors:

1.) Check to see that the names of your form fields match the variables/form fields in your phpBB script - this is most likely what's causing you concern.

2.) Check to see that posting.php is looking for variables based on 'post' method not the 'get' method.

i.e. $_GET[] vs. $_POST[]

Perhaps someone working with phpBB and is familiar with what you are trying to acheive can help facilitate your issue.