我想问一下,我怎样才能构建一个表单和php代码,它指示按钮1何时按下,包括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 :(得分:1)
将您的PHP代码放在html之前,如下所示:
<?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>
...
答案 1 :(得分: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();
}
?>
答案 2 :(得分: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
}
?>