Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764

    [HTML] submit form on page load (Not working)

    Hello,

    I was experimenting with javascript and forms to see if i can submit values to a php file on form load. This is what i made so far:

    PHP Code:
    <html>
    <
    head>
    <
    title>Test</title>
    <
    script language="javascript">
    function 
    MyFormSubmit(){
    document.form.submit
    }
    </script>
    </head>
    <body onload="MyFormSubmit()">
    <FORM ACTION="http://localhost/test.php" METHOD="POST">
    <INPUT TYPE="HIDDEN" NAME="username" VALUE="latheesan">FORM>
    </body>
    </html> 
    when i load opened the this page (test.html), it was suppose to post the value "latheesan" to the php script "test.php" file. But it didnt. If it did successfully posted that value, my php file wud say, "you name is latheesan"

    how comes this inst working?

  2. #2
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791
    is some of that code missing where is the submit button? Have you thought of just using pure php so you have a html form and then you do a

    if ($HTTP_POST_VARS['submit']) {

    type tag

    what are you trying to do with the form contents?

  3. #3
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    i know this method:

    if ($HTTP_POST_VARS['submit']) {

    but what i wanted to know is a method of auto-submit hidden values that is found on the html form to a php file, when the html file loads.

  4. #4
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345

    Arrow

    Two things,

    HTML mistakes
    You will go into an infinite loop (will just keep submitting):

    PHP Code:
    <html>
    <head>
    <title>Test</title>
    <?php
    if($_POST['username'] == NULL){?>
    <script language="javascript">
    function MyFormSubmit(){
    document.myname.submit();
    }
    </script>
    <?php ?>
    </head>
    <body onload="MyFormSubmit()">
    <FORM ACTION="http://localhost/test.php" METHOD="POST" name="myname" id="myname">
    <INPUT TYPE="HIDDEN" NAME="username" id="username" VALUE="latheesan"> </FORM>
    </body>
    </html>
    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  5. #5
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    Nicely done azizny. It worked.

    Thanks for ur help.

    Peace,

  6. #6
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    One last question.

    What if i have a list of usernames in array();

    how can i integrate foreach() loop to do this form submission ?

  7. #7
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    I've tried it like this:

    PHP Code:
    <?php

    $list
    ["david"] = "221";
    $list["smith"] = "179";
    $list["cleos"] = "879";

    $i 0;
    while (
    $i 1)
    {
            foreach(
    $list as $user => $id)
            {
                    if((
    $_POST['user'] == NULL) && ($_POST['id'] == NULL))
                    {
                            echo 
    "<script language=\"javascript\">
                                 function MyFormSubmit()
                                 {
                                 document.top.submit();
                                 }
                                 </script>"
    ;
                    }
                    echo 
    "<body onload=\"MyFormSubmit()\">
                         <form action=\"test.php\" method=\"POST\" name=\"top\" id=\"top\">
                         <input type=\"hidden\" name=\"user\" id=\"user\" value=\"
    $user\">
                         <input type=\"hidden\" name=\"id\" id=\"id\" value=\"
    $id\">
                         </form>
                         </body>"
    ;
            }
            
    $i++;
    }
    ?>
    unfortunetly it isnt working

    can someone help me work out why it isnt working plz?

  8. #8
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    why do i need $_POST['user'] == NULL this again?

  9. #9
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    should i have the foreach loop first and then bring in the while loop? how is this done

  10. #10
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345
    I have to understand what you want to do?

    You cant have more than 1 body tags on the same page, well at least you shouldnt?

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  11. #11
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    You don't need the while(), and your loop will never print the form along with the javascript. Just look at the source of your script (view source in the browser).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •