在同一窗口上执行多个按钮

时间:2014-05-07 10:27:47

标签: javascript php html css

我在同一个表单上创建了两个按钮,当我点击每个按钮时,会弹出一个新窗口。我希望他们在同一个窗口打开动作表单。

在代码块中找到我的代码。

提前致谢

<?php
//Start session
session_start();    
//Unset the variables stored in session
unset($_SESSION['SESS_FIRST_NAME']);
unset($_SESSION['SESS_LAST_NAME']);
?>

<!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>
    <script language="Javascript">
<!--
function OnButton1()
{
    document.loginform.action = "login_exec.php"
    document.loginform.target = "_blank";    // Open in a new window
    document.loginform.submit();             // Submit the page
    return true;
}

function OnButton2()
{
    document.loginform.action = "adminLogin.php"
    document.loginform.target = "_blank";    // Open in a new window
    document.loginform.submit();             // Submit the page
    return true;
}
-->
</script>
<noscript>You need Javascript enabled for this to work</noscript>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Document</title>
</head>

<body>
    <?php
    if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && 

count($_SESSION['ERRMSG_ARR']

) >0 ) {

echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>

<body bgcolor="green">

<form name="loginform" method="post">
<br/><br/><br/><br/><br/><br/><br/>
<table width="309" border="0" align="center" cellpadding="2" cellspacing="5">
<tr>
<td colspan="2">
<!--the code bellow is used to display the message of the input validation-->
</td>
</tr>
<tr>
<td><div align="right">Username</div></td>
<td><input name="username" type="text" /></td>
</tr>
<tr>
<tr>
<td><div align="right">Password</div></td>
<td><input name="password" type="password" /></td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><INPUT type="button" value="Login" name=button onclick="OnButton1();"><br/><br/>
<INPUT type="button" value="Click to Login as Admin" name=button onclick="OnButton2();"></td>
</tr>
</table>

</form>



</body>
</html>

2 个答案:

答案 0 :(得分:0)

在OnButton1和OnButton2函数中,将表单的目标设置为空白(打开一个新窗口)。

您的代码:

document.loginform.target = "_blank";    // Open in a new window

如果希望表单在同一窗口中执行操作,则必须将目标设置为“_self”。

答案 1 :(得分:0)

我不确切地知道如何说出我的想法,所以我希望有人可以帮助我进行编辑,但是你不需要将功能附加到类或按钮的id就像这样

<input type=submit id=button1/><input type=submit id=button2/>

<script>
$('#button1').click(function() {
   document.loginform.action = "login_exec.php"
   document.loginform.target = "_blank";    // Open in a new window
   document.loginform.submit();             // Submit the page
   return true;

});
$('#button2').click(function() {
   document.loginform.action = "adminLogin.php"
   document.loginform.target = "_blank";    // Open in a new window
   document.loginform.submit();             // Submit the page
   return true;
});
</script>

然后你甚至可以通过ajax调用来启动登录php脚本