How do I automatically submit a form

K

Kevin Audleman

Hi,

I've got a PHP page that, under the right circumstances, builds a form
and should submit automatically. I've tried adding the following code
to my page:

<script>
document.my_form.submit()
</script>

I have done this before with an ASP page and it worked. However
nothing happens with my PHP page.

Is this the right way to do this? Is there a better way?

Thanks,

Kevin
 
V

VK

Hi,

I've got a PHP page that, under the right circumstances, builds a form
and should submit automatically. I've tried adding the following code
to my page:

<script>
document.my_form.submit()
</script>

forms are not properties of document, they are members of
document.forms collection. Also collections and forms are not
finalized until document load event.

....
<form name="my_form" ...>
....
<script type="text/javascript">
window.onload = function() {
document.forms['my_form'].submit();
}
</script>
<noscript>
<!-- do not forget occasional script
support disabled so a way for manual
submit -->
<input type="submit">
</noscript>
</form>
....
 
J

Jake Barnes

Hi,

I've got a PHP page that, under the right circumstances, builds a form
and should submit automatically. I've tried adding the following code
to my page:

<script>
document.my_form.submit()
</script>

I have done this before with an ASP page and it worked. However
nothing happens with my PHP page.

I'm sorry, but I feel like I'm missing something here. Why would you
build an HTML page that automatically submits a form? If you have
information in one PHP script that you want to send to another PHP
page (or any page) why not use something like fwrite() to write the
data to that other page? Unless you need interaction with the user,
there is no need for the HTML form.

However, if you mean you create a form with PHP, and then the user
fills it in and then hits the submit button, then you don't need
Javascript.

So I'm confused about what it is your really asking.

Can you provide a URL?
 
T

Thomas 'PointedEars' Lahn

VK said:
forms are not properties of document, they are members of
document.forms collection.

Not quite true. In user agents implementing "DOM Level 0", form objects can
also be accessed by their name as properties of document objects.

However, since using collections is not only standards-compliant (the
implemented attributes and interfaces are defined in W3C DOM Level 2 HTML)
but also backwards compatible to "DOM Level 0", this is the oft-recommended
approach.
Also collections and forms are not finalized until document load event.

Although there is no specification or proof for that, it is what can be
expected.
...
<form name="my_form" ...>

Note that the `action' attribute is required for the `form' element,
regardless if the form is ever submitted.
...
<script type="text/javascript">
window.onload = function() {
document.forms['my_form'].submit();
}
</script>

There is no need to use yet another proprietary, error-prone feature:

<noscript>
<!-- do not forget occasional script
support disabled so a way for manual
submit -->
<input type="submit">
</noscript>

It does not make sense to hide the submit button from script-enabled user
agents. The form may not be submitted always, especially not if the submit
button is missing.


PointedEars
 
K

Kevin Audleman

Wow, thanks for the thorough answers everybody! I think what I really
need is a good book on Javascript/DOM. I've been mucking around with
it for a few years, but have never sat down and thoroughly tried to
understand the model. Does anybody have a good recommendation?

Kevin
 
T

The Natural Philosopher

Kevin said:
Wow, thanks for the thorough answers everybody! I think what I really
need is a good book on Javascript/DOM. I've been mucking around with
it for a few years, but have never sat down and thoroughly tried to
understand the model. Does anybody have a good recommendation?

Kevin
Ive got the O'Reiily. It's slightly more useful than used toilet paper,
but with javascript, that's par for the course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top