2013-02-05 31 views
1

我的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

「在本網站中,我無法正常插入代碼」 - 您可能想嘗試閱讀問題輸入旁邊顯示的* How to Format *框。 – Quentin

+0

您可以將帖子作爲Xhr在JavaScript中發佈帖子,也可以在第一次收到時使用'curl'從服務器上實質上'轉發'收到的帖子。最後一個選擇是你如何解決一些「同源」問題。 – ficuscr

+0

或做谷歌搜索「post get redirect」 –

回答

1

您可以輕鬆地使用一種形式爲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所做的相同。

+0

或者他可以在條件中包含('other_form.php'),或者寫一個類並執行表單在那裏處理,或者其他任何有意義的地方,就像在問題中描述的單獨行動那樣,在編程上是asinine。我認爲這個問題首當其衝的是他並沒有真正掌握表單處理和網站流程的概念。沒有理由不在這裏做處理,然後再重新定位標題。 –

+0

嗯..很好.... ....但如果行動1失敗,那麼一切都會停止,但如果行動1通過,那麼行動2 –

+0

我編輯了我的答案,你需要什麼:) –