Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在php中访问变量值_Php - Fatal编程技术网

无法在php中访问变量值

无法在php中访问变量值,php,Php,我正在开发一个简单的应用程序,在点击按钮时添加数组项,并在点击时显示值 我的问题是在将项目添加到数组后,我无法显示值 希望你能帮助我 HTML <form action="" method="POST"> <button name="save">save</button> <button name="display">display</button> </form> 这是两种不同的情况 因此,当您单击“保存

我正在开发一个简单的应用程序,在点击按钮时添加数组项,并在点击时显示值

我的问题是在将项目添加到数组后,我无法显示值

希望你能帮助我

HTML

<form action="" method="POST">
    <button name="save">save</button>
    <button name="display">display</button>
</form>

这是两种不同的情况

因此,当您单击“保存”按钮时,一切都正常工作,
$arr
包含所需值,但现在,当您单击“显示”按钮时,您再次提交此表单,第一个条件无法正常工作,因为服务器忘记了您的
$arr
。这是完全不同的要求

使用此代码:

$arr = array();

if (isset($_POST['save'])) {

    $items = array('Mark', '12', 'Japan');

    array_push($arr, $items);
    echo 'one <br>';

}

if (isset($_POST['display'])) {
    echo 'two <br>';
    print_r($arr);

}
$arr=array();
如果(isset($_POST['save'])){
$items=数组('Mark','12','Japan');
阵列推送($arr,$items);
呼应“一个
”; } 如果(isset($_POST['display'])){ 呼应“两个
”; 印刷费($arr); }
单击
display
按钮后,您将看到
one
不会显示

您可以使用
会话
作为最简单的解决方案

您可以尝试使用。此示例非常基本,但可能有助于您了解如何在后续访问中保留数据:

<?php
session_start();

if (isset($_POST['save'])) {
    echo 'Save'.'<br>';
    $items = array('Mark', '12', 'Japan');
    $_SESSION['SimpleArray'] = array();
    array_push($_SESSION['SimpleArray'], $items);
}

if (isset($_POST['display'])) {
    echo 'Display'.'<br>';
    print_r($_SESSION['SimpleArray']);
    echo '<br>';
}
?>

这里有一种方法,您可以使用隐藏的输入元素来使用添加的信息不断更新POST数组

使用以下命令:

<?php

//Check to make sure there is a value set for the hidden field in the post array.
  if(isset($_POST['myArray']) && $_POST['myArray']){

  //If there is we are going to decode and unserialize it and pass it to the $arr varaible.
  $arr = unserialize(base64_decode($_POST['myArray'])); 

  }

  //Check to see if you hit the save button.
  if (isset($_POST['save'])) {

    //We did so we are adding an element to the $arr array.
    $arr[] = array('Mark', '12', 'Japan');

  }

  //Check to see if we want to display all the data from hitting the save
  //button multiple times.
  if (isset($_POST['display'])) {

    //We did so lets print out the array of what we have "saved so far."
    print_r($arr);

  }

  //Checking to make sure we have an array.  If we do we are going to 
  //serialize and encode the array so we can pass it to the hidden input element.
  if(isset($arr)){

    //serialize and encode.
    $arr = base64_encode(serialize($arr));

  } else {

    //Nothing happened.  Pass nothing to the hidden input element.
    $arr = '';

  }


?>


<form action="" method="POST">
    <button name="save">save</button>
    <button name="display">display</button>
    <input type="hidden" name="myArray" value="<?php echo $arr; ?>">
</form>

拯救
显示

输出是什么?
print\r($arr)
?在save上按下的项目这不起作用的原因是,当单击save按钮时,您用项目填充了
$arr
变量,但从未保存它们,但当您单击display按钮时,它将
$arr
数组重新创建为空列表。要做到这一点,您需要在“保存”按钮上将数据存储在数据库或文件等位置,然后在“显示”按钮上首先检索并显示数据。在向php的ituse array merge函数(而不是array_push)添加值后,您实际上不会“保存”数组。这更多的是一种调试帮助,而不是实际的答案。也许可以添加一个使用会话的示例。OP似乎是PHP的新手
<?php

//Check to make sure there is a value set for the hidden field in the post array.
  if(isset($_POST['myArray']) && $_POST['myArray']){

  //If there is we are going to decode and unserialize it and pass it to the $arr varaible.
  $arr = unserialize(base64_decode($_POST['myArray'])); 

  }

  //Check to see if you hit the save button.
  if (isset($_POST['save'])) {

    //We did so we are adding an element to the $arr array.
    $arr[] = array('Mark', '12', 'Japan');

  }

  //Check to see if we want to display all the data from hitting the save
  //button multiple times.
  if (isset($_POST['display'])) {

    //We did so lets print out the array of what we have "saved so far."
    print_r($arr);

  }

  //Checking to make sure we have an array.  If we do we are going to 
  //serialize and encode the array so we can pass it to the hidden input element.
  if(isset($arr)){

    //serialize and encode.
    $arr = base64_encode(serialize($arr));

  } else {

    //Nothing happened.  Pass nothing to the hidden input element.
    $arr = '';

  }


?>


<form action="" method="POST">
    <button name="save">save</button>
    <button name="display">display</button>
    <input type="hidden" name="myArray" value="<?php echo $arr; ?>">
</form>