<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<label for="textbox">Scan Tracking ID </label>
<input type="text" name="blah" >
<input type = "Submit" value="submit" name = "submit">
</form>
<?php
session_start();
$s=array();
$s=$_SESSION['arr3'];
//print_r($s);
if((isset($_POST['blah'])) && !empty($_POST['blah']) && isset($_POST["submit"]))
{
$name = $_POST['blah']; //note i used $_POST since you have a post form **method='post'**
echo $name;
foreach( $_SESSION['arr3'] as $key => $obj)
{
if ($obj['ano'] == $name)
{
echo'<script>alert("item present")</script>';
}
else{
echo '<script> alert("item is not present");</script>';
}}
}
else
session_destroy();
?>
在上面的代码中,我有一个表格,它应该从文本字段发送给我的值。该值在$ name变量中捕获,我需要将此特定变量与数组$_SESSION['arr3']
的内容进行比较。当我第一次运行页面时$_SESSION['arr3']
中有值,但是当我在文本字段中输入一个值并点击提交时,这个值的比较没有发生,我遇到了一个未定义的索引&#39 ; ARR3&#39;错误。
我的$_SESSION['arr3']
如下,
Array
(
[0] => Array
(
[dono] => 2015081
[ano] => 5153350
[ord] => 6974000
[form] =>
[ext] =>
[fl] =>
)
[1] => Array
(
[dono] => 2015081
[ano] => 5153351
[ord] => 7644600
[form] =>
[ext] =>
[fl] =>
)
[2] => Array
(
[dono] => 2015081
[ano] => 5153352
[ord] => 4845100
[form] =>
[ext] =>
[fl] =>
)
[3] => Array
(
[dono] => 2015081
[ano] => 5153353
[ord] => 60082200
[form] =>
[ext] =>
[fl] =>
)
[4] => Array
(
[dono] => 2015000
[ano] => 5153354
[ord] => 6417400
[form] =>
[ext] =>
[fl] =>
)
)
注意:表单中的文本字段将被赋予数组的ano条目中存在的值。如果有匹配则打印该项目。其他项目不存在。
答案 0 :(得分:0)
您的session_start()
应始终是页面上的第一件事。
<?php
session_start();
$s=array();
$s=$_SESSION['arr3'];
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<label for="textbox">Scan Tracking ID </label>
<input type="text" name="blah" >
<input type = "Submit" value="submit" name = "submit">
</form>
<?php
//print_r($s);
if((isset($_POST['blah'])) && !empty($_POST['blah']) && isset($_POST["submit"]))
{
$name = $_POST['blah']; //note i used $_POST since you have a post form **method='post'**
echo $name;
foreach( $_SESSION['arr3'] as $key => $obj)
{
if ($obj['ano'] == $name)
{
echo'<script>alert("item present")</script>';
}
else{
echo '<script> alert("item is not present");</script>';
}}
}
else
session_destroy();
?>
<强>更新强>
<?php
session_start();
$_SESSION['someValue'] = array("1","2");
$vExploded = implode('|', $_SESSION['someValue']);
if(isset($_POST['submit']))
{
$name as $blah;
$vExploded = $_POST['someValue'];
$vExplodedArray = explode('|', $vExploded);
if(in_array($name, $vExplodedArray))
{
echo "Name exists";
}
else
{
echo "Name doesn't exist";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit">
<input type="hidden" name="someValue" value="<?php echo $vExploded; ?>">
<input type="text" name="blah" >
</form>