简体   繁体   English

一种形式的PHP中的两种形式的动作

[英]Two Form Actions In One Form PHP

I have two problems with my php code. 我的PHP代码有两个问题。

First one is how variable 第一个是可变的

$linkorig

can see after 可以看到

$_POST['formsubmitted']

,when i get it from redirected page? ,当我从重定向页面得到它时?

Second question is how can do form submit with different action, after all error checking ? 第二个问题是,在进行完错误检查之后,表单如何才能以不同的方式提交?

Maybe possible with some javascript or php code. 可能有一些javascript或php代码。

<?php
    $linkorig=$_POST['link-orig']; // get destination link from redirected page , for excample http://mikrotik.lv -- works fine 
    if (isset($_POST['formsubmitted'])) 
    {
        // script try login with credentials  with  <form action="login.php"   --- works fine
        //Error checking !
        if (empty($error)) //if not found any errors {
            // login with credentials and same form but  with  <form action="other_login.php"   --- i dont know how to do :(
        }
    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Login Form</title>
    </head>
    <body>
        <form name="login"  action="login.php"  method="post" class="registration_form" onSubmit="return doLogin()" >
            <input type="hidden" name="dst" value="<?php echo $linkorig; ?>" />
            <input type="hidden" name="popup" value="true" />
            <fieldset>
                <legend>Login Form  </legend>
                <p>Enter Your username and Password Below  </p>
                <div class="elements">
                    <label for="name">Email :</label>
                    <input type="text" id="e-mail" name="username" size="25" value="" />
                </div>
                <div class="elements">
                    <label for="Password">Password:</label>
                    <input type="password" id="Password" name="password" size="25" />
                </div>
                <div class="submit">
                    <input type="hidden" name="formsubmitted" value="TRUE" value="OK" />
                    <input type="submit" value="Login" />
                </div>
            </fieldset>
        </form>
        <!-- $(if error) -->
            <br /><div style="color: #FF8080; font-size: 9px"> </div>
        <!-- $(endif) -->
        <script type="text/javascript">
            <!--
            document.login.username.focus();
            //-->
        </script>
    </body>
</html>

You can easily use one form for 2 actions : 您可以轻松地将一种形式用于2个操作:

<form method='post' action='post.php'>
<input type='text' name='field1' />
<input type='text' name='field1' />

<button name='action1'>action1</button>
<button name='action2'>action2</button>
</form>

On the page post.php : 在页面post.php

All your variable will be in $_POST , namely $_POST['field1'], $_POST['field2'], $_POST['action1'] and $_POST['action2'] Use a simple condition : 您所有的变量都将在$_POST ,即$_POST['field1'], $_POST['field2'], $_POST['action1'] and $_POST['action2']使用一个简单的条件:

if(isset($_POST['action1'])) {
  //some code here but still check action2

  // edit : you can call the action2 with jquery and [ajax][1]
  // you can use header('location: http://example.com?field1='.$_POST['field1'].'&field2='.$_POST['field1']) to redirect, but no output before using header => see ob_get_clean() if you have any problem.
}
else { exit; } //stop it all if action1 failed

However, if you redirect your page, you have no longer $_POST variable. 但是,如果您重定向页面,则不再有$ _POST变量。 You can use /page.php?var1=... and $_GET them the same as you would do with $_POST. 您可以使用/page.php?var1=...和$ _GET它们,就像使用$ _POST一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM