因此,我尝试使用会话数组将单选按钮值传递给另一个页面,不幸的是,会话数组带有一个空值,尽管它仍显示该数组的计数只是返回一个空白值。
这是我的html形式的代码
顶部:
<?php
if (!isset($_SESSION))
{
session_start();
}
?>
HTML:
<form method="post" action="page1.php">
<table>
<tr>
<th></th>
<th>Info</th>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td>Name</td>
<td> <input type="radio" name="Fullname" value="Full Name"></td> //yes
<td> <input type="radio" name="Fullname" ></td> //no
</tr>
<tr>
<td>Address</td>
<td> <input type="radio" name="Address" value="Address"></td> //yes
<td> <input type="radio" name="Address"></td> //no
</tr>
<tr>
<td>Age</td>
<td> <input type="radio" name="Age" value="Age"></td> //yes
<td> <input type="radio" name="Age"></td> //no
</tr>
</table>
</form>
<?php
if (isset($_POST['Fullname']) && ($_POST['Address']) && ($_POST['Age'])){
$Fullname = $_POST['Fullname'];
$Address = $_POST['Address'];
$Age = $_POST['Age'];
}
$info = array($Fullname, $Address, $Age);
$_SESSION['Info'] = $info;
?>
这是我的page2.php代码
page2的html上方
if (!isset($_SESSION))
{
session_start();
}
内部html
<?php
if (isset($_SESSION['Info']) && is_array($_SESSION['Info'])) {
foreach($_SESSION['Info'] as $key=>$value)
{
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
?>
所以,我真正想做的是当用户选择“是”单选按钮时,它的值将被推到数组中。例如,用户为名称和地址选择是,然后为年龄选择否,然后在page2.php上将显示名称和地址。用户选择是的任何内容都将通过数组显示。但是以某种方式,我的代码仅显示计数,而不显示单选按钮的实际值。
或者对此有其他选择吗?
提前谢谢!
答案 0 :(得分:0)
有两种方法可以做到这一点。如果您遵循这种方法,那么也要在page1.php中开始会话,
<?php
if (!isset($_SESSION))
{
session_start();
} ?>
<form method="post" action="page1.php">
<table>
<tr>
<th></th>
<th>Info</th>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td>Name</td>
<td> <input type="radio" name="Fullname" value="Full Name"></td> //yes
<td> <input type="radio" name="Fullname" ></td> //no
</tr>
<tr>
<td>Address</td>
<td> <input type="radio" name="Address" value="Address"></td> //yes
<td> <input type="radio" name="Address"></td> //no
</tr>
<tr>
<td>Age</td>
<td> <input type="radio" name="Age" value="Age"></td> //yes
<td> <input type="radio" name="Age"></td> //no
</tr>
</table>
</form>
<?php
if (isset($_POST['Fullname']) && ($_POST['Address']) && ($_POST['Age'])){
$Fullname = $_POST['Fullname'];
$Address = $_POST['Address'];
$Age = $_POST['Age'];
}
$info = array($Fullname, $Address, $Age);
$_SESSION['Info'] = $info;
?>
将所有内容保持不变,一旦将表单提交到page1.php本身,它将会话数据存储在$ _SESSION ['Info']数组中。转到page2.php,然后重新加载页面。您将看到如下结果:
The value of $_SESSION['0'] is 'Full Name'
The value of $_SESSION['1'] is 'Address'
The value of $_SESSION['2'] is 'Age'
此外,您还可以在表单操作中使用page2.php,并将会话也存储在其中。 :)
答案 1 :(得分:0)
下面您将如何通过这种方式缩短代码。
然后使用您的html表单提交。
page1.php
<?php
session_start();
if (isset($_POST['Fullname'], $_POST['Address'], $_POST['Age'])){
$_SESSION['Info'] = filter_input_array(INPUT_POST);
}
?>
insidehtml.php
<?php
session_start();
if (isset($_SESSION['Info']) && is_array($_SESSION['Info'])) {
foreach($_SESSION['Info'] as $key => $value){
echo 'The value of ['.$key.'] is ('.$value.') <br />';
}
}
?>
答案 2 :(得分:0)
我的回答:也许您在代码段中缺少一些html代码...尝试我的更改...单选输入需要具有不同的值...在代码段中分配值Fullname但在第二个输入单选中您错过了一个值,与您为Address和Age所做的相同...其他问题...您正在存储到SESSION中,而无需启动session_start();我弄清楚了您想从代码中得到什么,然后我重新设计了答案,虽然有点复杂,但可以按预期工作:
<?php if (session_status() == PHP_SESSION_NONE) { session_start(); } ?>
<form method="post" action="page1.php">
<table>
<tr>
<th>Info</th>
<th>Yes</th>
<th>No</th>
</tr>
<tr>
<td>Name</td>
<td> <input type="radio" name="Fullname" value="yes">Yes</td>
<td> <input type="radio" name="Fullname" value="no">No</td>
</tr>
<tr>
<td>Address</td>
<td> <input type="radio" name="Address" value="yes">Yes</td>
<td> <input type="radio" name="Address" value="no">No</td>
</tr>
<tr>
<td>Age</td>
<td> <input type="radio" name="Age" value="yes">Yes</td>
<td> <input type="radio" name="Age" value="no">No</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php
if (isset($_POST['Fullname']) && isset($_POST['Address']) && isset($_POST['Age'])) {
if ($_POST['Fullname'] == "yes") {
$Fullname = array("Fullname" => "Yes");
} elseif ($_POST['Fullname'] == "no") {
$Fullname = array("Fullname" => "No");
}
if ($_POST['Address'] == "yes") {
$Address = array("Address" => "Yes");
} elseif ($_POST['Address'] == "no") {
$Address = array("Address" => "No");
}
if ($_POST['Age'] == "yes") {
$Age = array("Age" => "Yes");
} elseif ($_POST['Age'] == "no") {
$Age = array("Age" => "No");
}
} elseif (!isset($_POST['Fullname']) || !isset($_POST['Address']) || !isset($_POST['Age'])) {
$Fullname = array("Fullname" => "Not Set");
$Address = array("Address" => "Not Set");
$Age = array("Age" => "Not Set");
}
$info = array($Fullname, $Address, $Age);
$_SESSION['Info'] = $info;
?>
insidehtml.php
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['Info']) && is_array($_SESSION['Info'])) {
foreach($_SESSION['Info'][0] as $key => $value) {
echo 'The value of $_SESSION[' . "'" . $key . "'" . '] is ' . "'" . $value . "'" . ' <br />';
}
foreach($_SESSION['Info'][1] as $key => $value) {
echo 'The value of $_SESSION[' . "'" . $key . "'" . '] is ' . "'" . $value . "'" . ' <br />';
}
foreach($_SESSION['Info'][2] as $key => $value) {
echo 'The value of $_SESSION[' . "'" . $key . "'" . '] is ' . "'" . $value . "'" . ' <br />';
}
}
?>
让我知道您是否在寻找...;-)