<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']
有存在于其中的值,但是当我在文本字段中输入一个值并点击提交时,这个值的比较没有发生,我遇到了一个未定义的索引'arr3'错误。
我的$_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] =>
)
)
注意:表单中的文本字段将给出数组中第一个条目中的值。如果存在匹配项,则打印该项存在。
您的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>