簡體   English   中英

一種形式的PHP中的兩種形式的動作

[英]Two Form Actions In One Form PHP

我的PHP代碼有兩個問題。

第一個是可變的

$linkorig

可以看到

$_POST['formsubmitted']

,當我從重定向頁面得到它時?

第二個問題是,在進行完錯誤檢查之后,表單如何才能以不同的方式提交?

可能有一些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>

您可以輕松地將一種形式用於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>

在頁面post.php

您所有的變量都將在$_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

但是,如果您重定向頁面,則不再有$ _POST變量。 您可以使用/page.php?var1=...和$ _GET它們,就像使用$ _POST一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM