[英]HTML - Button open link inside form
我正在創建登錄功能,但是我還需要一個注冊按鈕。 該注冊按鈕必須具有與登錄功能相同的形式,因為我需要以塊形式顯示它們。
我有這個
<form class="login" onsubmit="return validate();" method="get" action="php/home.php">
<input class="login-panel credentials" id="username" placeholder="Username" name="username"/>
<input class="login-panel login-button" id="submit" value="Login" type="submit"/>
</form>
進行登錄,但我還需要一個注冊按鈕,該按鈕可將用戶重定向到另一個頁面。 但是由於表單已經有一個動作,我將如何添加此按鈕:
<input class="login-panel login-button" type="submit" value="Register"/>
到表單,仍然能夠將用戶重定向到另一個頁面?
我會在表單中使用一個動作,然后在目標頁面中包含邏輯。 例如,使用PHP:
<?php
if(isset($_POST['submit']) && $_POST['submit']=='Login'){
include("/yourloginscript.php");
} else {
include("/yourregistrationscript.php");
}
?>
正如我從您的操作字段中看到的那樣,您正在使用PHP,因此您可以執行以下操作:
1.將名稱屬性添加到提交按鈕:
<input name='login' class="login-panel login-button" id="submit" value="Login" type="submit" />
<input name='register' class="login-panel login-button" type="submit" value="Register" />
然后調整您的php文件,以便在發送$ _POST ['register']時重定向用戶:
//First use filter_input to clean your post
$post_array = filter_input_array(INPUT_POST);
//then check the kind of action your user wants to do
if(isset($post_array['register'])){
//will trigger if the register submit button was used
//So you can redirect the user using header like so:
header("location:https://yoursite.com/register.php");
}elseif(isset($post_array['login'])){
//will trigger if the login submit button was used
}
根據經驗,如果在單個表單上使用多個提交按鈕,則僅單擊的提交按鈕將在POST中發送。 因此,您可以有多個提交按鈕,並為每個按鈕觸發不同的操作。
您應該創建一個按鈕:
<input type="button" onclick="location.href='php/register.php'" value="Register" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.