?
Solved

PHP auto execute a form

Posted on 2011-05-08
6
Medium Priority
?
513 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:jbpeake
6 Comments
 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 35717274
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
 

Author Comment

by:jbpeake
ID: 35717427
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
 
LVL 13

Expert Comment

by:dsmile
ID: 35717765
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 35719319
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35720236
... 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
 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 35783988
would you please run page without return valid_form( this ) checking and tell us if there is still any problem
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
What You Need to Know when Searching for a Webhost Provider
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question