无法在php中更改数组的值

无法在php中更改数组的值,php,Php,我有一个全局数组变量,用于存储$\u POST变量中的一些值。因为我在if语句中修改它们,所以我使用了global这个词。我已经将数组填充为数组(0,1)。如果在我的if语句中修改了这些值,那么我仍然会得到我开始使用的初始化值 编辑: 这是我的formHandler.php代码: // get form ID $formID = $_GET['id']; // create a global array $results = array(); // check to see what

我有一个全局数组变量,用于存储
$\u POST
变量中的一些值。因为我在
if
语句中修改它们,所以我使用了
global
这个词。我已经将数组填充为
数组(0,1)
。如果在我的
if
语句中修改了这些值,那么我仍然会得到我开始使用的初始化值

编辑:

这是我的
formHandler.php
代码:

   // get form ID
$formID = $_GET['id'];

// create a global array
$results = array();

// check to see what ID has be retrieved
if ($formID == 1) {
    $value = $_POST['emotion'];
    $results[0] = $value;
    // 302 redirect
    header("location: page02.php?results1=$results[0]");
} 
else if ($formID == 2) {
    $value = $_POST['emotion02'];
    $results[1] = $value;
    // 302 redirect
    header("location: page03.php?&results2=$results[1]");
}
  <h1>This is Page 01</h1>

    <form id="form" action="formHandler.php?id=1" method="POST">

            <p><input type="radio" value="happy" name="emotion">Happy
            <input type="radio" value="excited" name="emotion">Excited
            <input type="radio" value="angry" name="emotion">Angry
            <input type="radio" value="frustrated" name="emotion">Frustrated
            <input type="radio" value="miserable" name="emotion">Miserable
            <input type="radio" value="sad" name="emotion">Sad
            <input type="radio" value="tired" name="emotion">Tired
            <input type="radio" value="calm" name="emotion">Angry</p>

            <p><input type="submit" name="submit" value="Submit"></p>
<h1>This is Page 02</h1>

    <form id="form" action="formHandler.php?id=2" method="POST">

            <p><input type="radio" value="happy" name="emotion02">Happy
            <input type="radio" value="excited" name="emotion02">Excited
            <input type="radio" value="angry" name="emotion02">Angry
            <input type="radio" value="frustrated" name="emotion02">Frustrated
            <input type="radio" value="miserable" name="emotion02">Miserable
            <input type="radio" value="sad" name="emotion02">Sad
            <input type="radio" value="tired" name="emotion02">Tired
            <input type="radio" value="calm" name="emotion02">Angry</p>

            <p><input type="submit" name="submit" value="Submit"></p>
<?php include("formHandler.php"); ?>

<html>
    <head>
        <title>Audio Survey</title>
    </head>

    <body>
        <h1>Thanks for your submission</h1>

        <?php

        $results = array();
        $results[0] = $_GET['results1'];
        $results[1] = $_GET['results2']; 

        echo "<p>" . $results[0] . " " . $results[1] . "</p>"; 

        ?>

    </body>
</html>
这是我的
page01.php
代码:

   // get form ID
$formID = $_GET['id'];

// create a global array
$results = array();

// check to see what ID has be retrieved
if ($formID == 1) {
    $value = $_POST['emotion'];
    $results[0] = $value;
    // 302 redirect
    header("location: page02.php?results1=$results[0]");
} 
else if ($formID == 2) {
    $value = $_POST['emotion02'];
    $results[1] = $value;
    // 302 redirect
    header("location: page03.php?&results2=$results[1]");
}
  <h1>This is Page 01</h1>

    <form id="form" action="formHandler.php?id=1" method="POST">

            <p><input type="radio" value="happy" name="emotion">Happy
            <input type="radio" value="excited" name="emotion">Excited
            <input type="radio" value="angry" name="emotion">Angry
            <input type="radio" value="frustrated" name="emotion">Frustrated
            <input type="radio" value="miserable" name="emotion">Miserable
            <input type="radio" value="sad" name="emotion">Sad
            <input type="radio" value="tired" name="emotion">Tired
            <input type="radio" value="calm" name="emotion">Angry</p>

            <p><input type="submit" name="submit" value="Submit"></p>
<h1>This is Page 02</h1>

    <form id="form" action="formHandler.php?id=2" method="POST">

            <p><input type="radio" value="happy" name="emotion02">Happy
            <input type="radio" value="excited" name="emotion02">Excited
            <input type="radio" value="angry" name="emotion02">Angry
            <input type="radio" value="frustrated" name="emotion02">Frustrated
            <input type="radio" value="miserable" name="emotion02">Miserable
            <input type="radio" value="sad" name="emotion02">Sad
            <input type="radio" value="tired" name="emotion02">Tired
            <input type="radio" value="calm" name="emotion02">Angry</p>

            <p><input type="submit" name="submit" value="Submit"></p>
<?php include("formHandler.php"); ?>

<html>
    <head>
        <title>Audio Survey</title>
    </head>

    <body>
        <h1>Thanks for your submission</h1>

        <?php

        $results = array();
        $results[0] = $_GET['results1'];
        $results[1] = $_GET['results2']; 

        echo "<p>" . $results[0] . " " . $results[1] . "</p>"; 

        ?>

    </body>
</html>
这是我的
page03.php
code:

   // get form ID
$formID = $_GET['id'];

// create a global array
$results = array();

// check to see what ID has be retrieved
if ($formID == 1) {
    $value = $_POST['emotion'];
    $results[0] = $value;
    // 302 redirect
    header("location: page02.php?results1=$results[0]");
} 
else if ($formID == 2) {
    $value = $_POST['emotion02'];
    $results[1] = $value;
    // 302 redirect
    header("location: page03.php?&results2=$results[1]");
}
  <h1>This is Page 01</h1>

    <form id="form" action="formHandler.php?id=1" method="POST">

            <p><input type="radio" value="happy" name="emotion">Happy
            <input type="radio" value="excited" name="emotion">Excited
            <input type="radio" value="angry" name="emotion">Angry
            <input type="radio" value="frustrated" name="emotion">Frustrated
            <input type="radio" value="miserable" name="emotion">Miserable
            <input type="radio" value="sad" name="emotion">Sad
            <input type="radio" value="tired" name="emotion">Tired
            <input type="radio" value="calm" name="emotion">Angry</p>

            <p><input type="submit" name="submit" value="Submit"></p>
<h1>This is Page 02</h1>

    <form id="form" action="formHandler.php?id=2" method="POST">

            <p><input type="radio" value="happy" name="emotion02">Happy
            <input type="radio" value="excited" name="emotion02">Excited
            <input type="radio" value="angry" name="emotion02">Angry
            <input type="radio" value="frustrated" name="emotion02">Frustrated
            <input type="radio" value="miserable" name="emotion02">Miserable
            <input type="radio" value="sad" name="emotion02">Sad
            <input type="radio" value="tired" name="emotion02">Tired
            <input type="radio" value="calm" name="emotion02">Angry</p>

            <p><input type="submit" name="submit" value="Submit"></p>
<?php include("formHandler.php"); ?>

<html>
    <head>
        <title>Audio Survey</title>
    </head>

    <body>
        <h1>Thanks for your submission</h1>

        <?php

        $results = array();
        $results[0] = $_GET['results1'];
        $results[1] = $_GET['results2']; 

        echo "<p>" . $results[0] . " " . $results[1] . "</p>"; 

        ?>

    </body>
</html>

音频调查
谢谢你的意见

我想你不需要page03.php。只需死亡或返回formHandler.php中的代码(html)

也许这对你也有帮助:


此处不需要全局变量,所有代码都在同一范围内。

您使用$\u GET获取表单ID,但您说它是$\u POST(method=“POST”)表单,因此ID永远不会与if语句上的条件匹配。这应该可以解决这个问题:

$formID = $_POST['id'];
此外,id应该是表单上的一个输入,以便将其作为$\u POST传递,表单的实际id属性本身不会传递给服务器

如果您实际上是从action属性在url上传递“?id=x”,那么请同时发布表单HTML


还有一件事,你不需要,也不应该使用“全局”,尤其是当变量在你调用它们的同一个文件中时。

…你想做什么?你的问题是?php的变量范围是函数级的。您的
global
调用完全没有意义。PHP也不会在页面之间保留变量。您的每个脚本都是完全/完全独立的,并且“全局”不会神奇地在脚本之间保留变量。您需要使用
$\u SESSION
或其他外部存储方法来实现这类功能。我明白了,谢谢。我所遵循的教程将它们放在一个函数中,因此我假设您将需要它们放在
if
语句中。我将删除
global
调用并查看
$\u会话
当您重定向时,使用URL中的参数重定向以使用FormHandler.php中的值。例如,page03.php?Result1=x&Result2=y。然后你可以在使用GET调用他们后在页面上显示它。谢谢,我会查看:-)谢谢-是的,我正在使用
page01.php
传递
id=1
,然后在
page02.php
上传递
id=2
,然后
page03.php
是我想要打印结果的地方。我希望我可以从每个页面检索表单信息,然后在最后一页显示它。这只是一个练习,因为我希望能够有20个与我刚刚发布的页面相同的页面。我仍然希望看到您的表单HTML。您有$value=$\u POST['emotion02'];在第二个if语句中,但在第02页上,该字段仍然称为emotion。再次道歉,它是“emotion02”。刚刚在上面纠正了它。page01和page02上都有6个单选按钮,但为了使代码更简洁,我省略了它们。不要害羞,发布所有内容。这都是相关的。