无法在PHP中访问变量值

时间:2018-12-17 07:30:48

标签: php

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

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

希望你能帮助我。

HTML

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
    name: "main"
    Button:
        on_release: app.root.current = "Map"
        text: "Map"
        font_size: 8
        size_hint:0.05,0.05
        pos_hint: {"right":1,"top":1}
        color: 0,1,0,1

<AnotherScreen>:
    name: "Map"

    FloatLayout:
        Map
        Button:
            color: 0,1,0,1
            font_size: 8
            size_hint: 0.05,0.05
            text: "Graph"
            on_release: app.root.current = "main"
            pos_hint: {"right":1, "top":1}

PHP

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

3 个答案:

答案 0 :(得分:1)

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

SELECT id, av[1] AS source, av[2] AS target
FROM (
    SELECT id, CASE
        WHEN id <> 1 THEN ARRAY[source, target]
        ELSE ARRAY[0, 0]
    END AS av
    FROM t
) AS x

答案 1 :(得分:1)

在这里,您可以使用隐藏的输入元素来不断使用添加的信息更新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>

希望有帮助。

答案 2 :(得分:0)

这是两个不同的条件。

因此,当您单击“保存”按钮时,一切正常,并且$arr包含了所需的值,但是现在,当您单击“ display”按钮时,您正在重新提交此表单再次,第一个条件无法正常运行,因为服务器忘记了您的$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);

}

点击one按钮后,您将看到display没有显示。

您可以使用session作为最简单的解决方案。