2014-04-12 31 views
0

我想问,我怎样才能建立一个表单和php代码,其中指示button1按下时,包括test.php和当button2按下打开一个新的页面。两个按钮的形式,每个使不同的东西

我所拥有的就是这个

... 
<form name="frm_action" method="post" action=""> 
<div align="center"> 
    <input type="submit" style="font-size: 1.5em;border: 1pt;" name="btn_getVal1" id="btn_getVal1" value="VAL1" tabindex="1"> 
    <input type="submit" style="font-size: 1.5em;border: 1pt;" name="btn_getVal2" id="btn_getVal2" value="VAL2" tabindex="2"> 
</form> 
... 
<?php 
    if (isset($_POST['btn_getVal1'])) 
    { 
     include('test.php'); 
    } 
    if (isset($_POST['btn_getVal2'])) 
    { 
     header('location: test2.php'); 
     exit(); 
    } 
?> 
... 

第一个选项(btn_getVal1)可以完美运行,第二个不会工作。它每次显示index.php。

如何在按下按钮时链接到内部.php页面?

+0

您可以使用javascript打开一个新页面,必须在发送任何实际输出之前调用'header'函数,或者使用正常的H TML标签,文件中的空白行或PHP中的空白行。 http://fr2.php.net/manual/en/function.header.php – ChoiZ

回答

1

这样的HTML之前把你的PHP代码:

<?php 
    if (isset($_POST['btn_getVal1'])) 
    { 
     include('test.php'); 
    } 
    if (isset($_POST['btn_getVal2'])) 
    { 
     header('location: test2.php'); 
     exit(); 
    } 

?> 

<form name="frm_action" method="post" action=""> 
<div align="center"> 
    <input type="submit" style="font-size: 1.5em;border: 1pt;" name="btn_getVal1" id="btn_getVal1" value="VAL1" tabindex="1"> 
    <input type="submit" style="font-size: 1.5em;border: 1pt;" name="btn_getVal2" id="btn_getVal2" value="VAL2" tabindex="2"> 
</form> 
... 
0

在发送任何实际输出之前,必须调用header函数,通过普通HTML标记,文件中的空行或PHP发送。

要解决你的头虫:

<?php 
// only display form at first load or on push button 1 
if (empty($_POST) || isset($_POST['btn_getVal1'])) { 
?> 
<form name="frm_action" method="post" action=""> 
<div align="center"> 
    <input type="submit" style="font-size: 1.5em;border: 1pt;" name="btn_getVal1" id="btn_getVal1" value="VAL1" tabindex="1"> 
    <input type="submit" style="font-size: 1.5em;border: 1pt;" name="btn_getVal2" id="btn_getVal2" value="VAL2" tabindex="2"> 
</form> 
<?php 
} // close first if 

if (isset($_POST['btn_getVal1'])) { 
    include('test.php'); 
} 

if (isset($_POST['btn_getVal2'])) { 
    // in this case no html was print before so header works 
    // you can add a peace of javascript here if you want open a new page. 
    header('location: test2.php'); 
    exit(); 
} 
?> 
0

你应该把PHP代码的HTML代码的面前,就像这样:

<?php 
    if (isset($_POST['btn_getVal1'])) 
    { 
     include('test.php'); 
    } 
    elseif (isset($_POST['btn_getVal2'])) 
    { 
     header('location: test2.php'); 
     exit(); 
    } 
    else{ 
?> 
<form name="frm_action" method="post" action=""> 
<div align="center"> 
    <input type="submit" style="font-size: 1.5em;border: 1pt;" name="btn_getVal1" id="btn_getVal1" value="VAL1" tabindex="1"> 
    <input type="submit" style="font-size: 1.5em;border: 1pt;" name="btn_getVal2" id="btn_getVal2" value="VAL2" tabindex="2"> 
</form> 
<?php 
    } 
?> 
+0

在这种情况下,表格是在包括test.php后... – ChoiZ

+0

编辑我的答案 – Jonan

+0

请解释downvote? – Jonan

相关问题