PHP auto execute a form

This should be easy, but I cannot figure it out.  I have a form that allows people to log-in to a secure area, but for an odd reason I need to be able to make the form execute on it's own.  So that when people click the "log in" link instead of getting the log in form, I want the page that holds the log in form to auto execute and send them on to the next page.  I have included the form below.  I tried just putting 'value="correct_username"' in for the username and the same for the password so that the user could just click on the submit button and get in, but that isn't what I need, I need the form to actually run without the user having to click the Submit button.
<form name="login_form" id="login" action="login.php" method="post"
  onsubmit="return valid_form( this )">
<?php
if ( ! empty ( $return_path ) ) {
  echo '<input type="hidden" name="return_path" value="' .
    htmlentities ( $return_path ) . '" />' . "\n";
}
?>
<table align="center" cellspacing="10" cellpadding="10">
<tr><td rowspan="2">
 <img src="images/login.gif" alt="Login" /></td><td align="right">
 <label for="user"><?php etranslate ( 'Username' )?>:</label></td><td>
 <input name="login" id="user" size="15" maxlength="25"
   tabindex="1" />
</td></tr>
<tr><td class="alignright">
 <label for="password"><?php etranslate ( 'Password' )?>:</label></td><td>
 <input name="password" id="password" type="not_password" size="15"
   maxlength="30" tabindex="2" />
</td></tr>
<tr><td colspan="3" style="font-size: 10px;">
 <input type="checkbox" name="remember" id="remember" tabindex="3"
   value="yes" <?php if ( ! empty ( $remember ) && $remember == 'yes' ) {
     echo 'checked="checked"'; }?> /><label for="remember">&nbsp;
   <?php etranslate ( 'Save login via cookies so I dont have to login next time.' )?></label>
</td></tr>
<tr><td colspan="4" class="aligncenter">
 <input type="submit" value="<?php etranslate ( 'Login' )?>" tabindex="4" />
</td></tr>
</table>
</form>

Open in new window

jbpeakeAsked:
Who is Participating?
 
Ray PaseurCommented:
... for an odd reason ... - no kidding ;-)

HTML forms are designed to receive client input and send the client input to the server.  The variable here is what the client wants to send to the server.  If you, as the author of the HTML form already know what you want to have sent to the server, you have a couple of good options.

1. Don't bother to send the form at all - you already know what it should contain; just let the server-side script work as if that data had been sent by the client.
2. Pre-populate the form with the data you want sent.  Just plug it into the value= attribute of the <input> tags.
3. Use hidden form inputs to pre-populate the variables and just give the client a submit control.

Any of these should work fine, and all are easy to program and test.  HTH, ~Ray

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Just add this to the bottom of your page

   
<?php if($_POST["submitbutton"]) 
{
?>
    <script>
    onload=autosubmit;
    function autosubmit(){
    document.getElementById('login').action='newpage.php';
    document.getElementById('login').submit();
    }
    </script>
<?php
} 
?>

Open in new window

0
 
jbpeakeAuthor Commented:
I gave it a try, but it isn't working... my new code is below... any suggestions?
<form name="login_form" id="login" action="login.php" method="post"
  onsubmit="return valid_form( this )">
<?php
if ( ! empty ( $return_path ) ) {
  echo '<input type="hidden" name="return_path" value="' .
    htmlentities ( $return_path ) . '" />' . "\n";
}
?>
<table align="center" cellspacing="10" cellpadding="10">
<tr><td rowspan="2">
 <img src="images/login.gif" alt="Login" /></td><td align="right">
 <label for="user"><?php etranslate ( 'Username' )?>:</label></td><td>
 <input name="login" id="user" size="15" maxlength="25"
   tabindex="1" value="webadmin"/>
</td></tr>
<tr><td class="alignright">
 <label for="password"><?php etranslate ( 'Password' )?>:</label></td><td>
 <input name="password" id="password" type="not_password" size="15"
   maxlength="30" tabindex="2" value="webadmin"/>
</td></tr>
<tr><td colspan="3" style="font-size: 10px;">
 <input type="checkbox" name="remember" id="remember" tabindex="3"
   value="yes" <?php if ( ! empty ( $remember ) && $remember == 'yes' ) {
     echo 'checked="checked"'; }?> /><label for="remember">&nbsp;
   <?php etranslate ( 'Save login via cookies so I dont have to login next time.' )?></label>
</td></tr>
<tr><td colspan="4" class="aligncenter">
 <input type="submit" value="<?php etranslate ( 'Login' )?>" tabindex="4" />
</td></tr>
</table>
</form>

<?php 
// Force autorun of form
if($_POST["submitbutton"]) 
{
?>
    <script>
    onload=autosubmit;
    function autosubmit(){
    document.getElementById('login').action='month.php';
    document.getElementById('login').submit();
    }
    </script>
<?php
} 
?>

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
dsmileCommented:
This is all you need

<script>
document.getElementById('login').submit();
</script>

<form name="login_form" id="login" action="login.php" method="post"
  onsubmit="return valid_form( this )">
<?php
if ( ! empty ( $return_path ) ) {
  echo '<input type="hidden" name="return_path" value="' .
    htmlentities ( $return_path ) . '" />' . "\n";
}
?>
<table align="center" cellspacing="10" cellpadding="10">
<tr><td rowspan="2">
 <img src="images/login.gif" alt="Login" /></td><td align="right">
 <label for="user"><?php etranslate ( 'Username' )?>:</label></td><td>
 <input name="login" id="user" size="15" maxlength="25"
   tabindex="1" value="webadmin"/>
</td></tr>
<tr><td class="alignright">
 <label for="password"><?php etranslate ( 'Password' )?>:</label></td><td>
 <input name="password" id="password" type="not_password" size="15"
   maxlength="30" tabindex="2" value="webadmin"/>
</td></tr>
<tr><td colspan="3" style="font-size: 10px;">
 <input type="checkbox" name="remember" id="remember" tabindex="3"
   value="yes" <?php if ( ! empty ( $remember ) && $remember == 'yes' ) {
     echo 'checked="checked"'; }?> /><label for="remember">&nbsp;
   <?php etranslate ( 'Save login via cookies so I dont have to login next time.' )?></label>
</td></tr>
<tr><td colspan="4" class="aligncenter">
 <input type="submit" value="<?php etranslate ( 'Login' )?>" tabindex="4" />
</td></tr>
</table>
</form>
<script>
document.getElementById('login').submit();
</script>

Open in new window

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
the code block i've last sent works.
But with onsubmit="return valid_form( this )" in form tag i cannot test it, could you please write us the valid_form function?
There is the code  block to fail second submit i guess.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
would you please run page without return valid_form( this ) checking and tell us if there is still any problem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.