Jump to content

onload form submit doesn't submit the data?


samryan

Recommended Posts

Hi,

I have a form that when used normally works perfectly, the data is submitted and it runs the process using the data that has been sent. I am now trying to get the form to be automatically submitted, using javascript.

 

My normal code, it works perfectly when the "submit" button is used.

 

<html>
<head></head>
<body onload="javascript:document.forms.samsform.submit();">
<form name="samsform" action="login.php" method="post">
<input type="text" class="post" name="myfreefo" size="25" maxlength="40" value="<?php echo "$username";?>" />
<input type="password" class="post" name="password" size="25" maxlength="32" value="<?php echo "$password";?>" />
<input type="checkbox" name="autologin" /></span></td>
<input type="hidden" name="redirect" value="" /><input type="submit" name="login" class="mainoption" value="Log in" />
</form>
</body>
</html>

 

Now, the following is the javascript version, when the page loads, the form processes (it runs to the method page, login.php) but no data is sent, the login.php page receives no data?

 

<html>
<head></head>
<body onload="javascript:document.forms.samsform.submit();">
<form name="samsform" action="login.php" method="post">
<input type="text" class="post" name="myfreefo" size="25" maxlength="40" value="<?php echo "$username";?>" />
<input type="password" class="post" name="password" size="25" maxlength="32" value="<?php echo "$password";?>" />
<input type="checkbox" name="autologin" /></span></td>
<input type="hidden" name="redirect" value="" /><input type="submit" name="login" class="mainoption" value="Log in" />
</form>
</body>
</html>

 

Any help? What's going wrong? ;|

Link to comment
Share on other sites

Aren't both of those code snippets the same?

I think the onload event is fireing before the values are getting initialized. Try it like this:

<html>
<head></head>
<body>
<form name="samsform" action="login.php" method="post">
<input type="text" class="post" name="myfreefo" size="25" maxlength="40" value="<?php echo "$username";?>" />
<input type="password" class="post" name="password" size="25" maxlength="32" value="<?php echo "$password";?>" />
<input type="checkbox" name="autologin" /></span></td>
<input type="hidden" name="redirect" value="" /><input type="submit" name="login" class="mainoption" value="Log in" />
</form>
<script type="text/javascript">
document.forms.samsform.submit();
</script>
</body>
</html>

By the way, I don't know what you are doing with it, but putting a password in the html like that is really insecure.

Link to comment
Share on other sites

Yeah, I know it's insecure, it's for testing purposes and stuff ;)

 

It didn't work though, I tried that before. The body onload should say "When the full body is loaded, do this" shouldn't it? Even when moving the onload to after the form doesn't work.

Link to comment
Share on other sites

Hmmm, I did a simple test and it worked for me.

 

<html>
<head></head>
<?php

if ($_POST['myfreefo'])
{ //Display the results from the submitted data
  echo "Submit Data:<br>";
  echo "<pre>";
  print_r($_POST);
  echo "<pre>";

  $onload="";
}
else
{
  //First page load auto-submit the data
  $onload = "javascript:document.forms.samsform.submit();";
}

?>

<body onload="<?php echo $onload; ?>">
<form name="samsform" action="" method="post">
<input type="text" class="post" name="myfreefo" size="25" maxlength="40" value="THEUSERNAME" />
<input type="password" class="post" name="password" size="25" maxlength="32" value="THEPASSWORD" />
<input type="checkbox" name="autologin" /></span></td>
<input type="hidden" name="redirect" value="" /><input type="submit" name="login" class="mainoption" value="Log in" />
</form>

</body>
</html>

Link to comment
Share on other sites

I just tested both my way and your way in Firefox and IE and all tests worked as expected. I think the problem might actually be in your login.php page. Can you show the code for that?

 

I've come to that conclusion also, however if I drop the javascript, and submit the form manually, it works perfectly, so that leads me to believe the login.php is saying "if javascript has been used, no thanks" but it doesn't.

 

The login.php is from phpBB2, I'm using this to log into a forum. Long story and no point explaining, but I'm trying to submit the form data to login.php on a forum, powered by phpBB2. Without javascript it works perfectly, with it fails.

 

I figured a way to work around it though, I *think*.

Link to comment
Share on other sites

I think lemmin's right. Here is the utput I get with my test page when doing an autosubmit vs. a manual submit.

 

Autosubmit:

    [myfreefo] => THEUSERNAME
    [password] => THEPASSWORD
    [redirect] => 

 

Manual Submit:

    [myfreefo] => THEUSERNAME
    [password] => THEPASSWORD
    [redirect] => 
    [login] => Log in

Link to comment
Share on other sites

I bet you have some code like this:

<?php
if (isset($_POST['login']))
   //...
?>

 

That is the name of your submit button and it isn't getting passed because no one is clicking it.

 

Genius!

Just as you said that, it struck me. Brilliant, that has to be it! Thankyou! <3 Problem solved!

How I missed that, I'll never know.

 

Moron I am.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.