samryan Posted September 5, 2008 Share Posted September 5, 2008 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? ;| Quote Link to comment Share on other sites More sharing options...
lemmin Posted September 5, 2008 Share Posted September 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
samryan Posted September 5, 2008 Author Share Posted September 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
samryan Posted September 5, 2008 Author Share Posted September 5, 2008 I'm rather confused, the form does submit, because it takes me to login.php but no data is carried through. I'm very very confused now. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 5, 2008 Share Posted September 5, 2008 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> Quote Link to comment Share on other sites More sharing options...
lemmin Posted September 5, 2008 Share Posted September 5, 2008 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? Quote Link to comment Share on other sites More sharing options...
samryan Posted September 5, 2008 Author Share Posted September 5, 2008 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*. Quote Link to comment Share on other sites More sharing options...
lemmin Posted September 5, 2008 Share Posted September 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 5, 2008 Share Posted September 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
samryan Posted September 5, 2008 Author Share Posted September 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
samryan Posted September 5, 2008 Author Share Posted September 5, 2008 I think lemmin's right. Indeed he was. Problem solved, thank you ever so Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.