Php 为什么我的数组在while循环被破坏后不打印while循环中得到的内容?

Php 为什么我的数组在while循环被破坏后不打印while循环中得到的内容?,php,Php,我试图在while循环中将值(来自用户的输入)添加到数组中,但它不会在循环本身之后打印它 <?php $x = 1; while(!isset($_POST['stopButton'])) { $countryArr[$x] = $searchCountry; $message = "added!"; echo "<script> alert('$message'); window.location.href='../html/choicePag

我试图在while循环中将值(来自用户的输入)添加到数组中,但它不会在循环本身之后打印它

<?php
$x = 1;

while(!isset($_POST['stopButton'])) {

  $countryArr[$x] = $searchCountry;
  $message = "added!";
  echo "<script>
    alert('$message');
    window.location.href='../html/choicePage.php';
  </script>";
  $x++;
}

foreach($countryArr as $country) {
  echo $country;
}
?>

请查看我添加的注释

<?php
$x = 1;

while(!isset($_POST['stopButton'])) { // this runs if no form post stopButton

  $countryArr[$x] = $searchCountry;    // $searchCountry is undefined
  $message = "added!";
  echo "<script>
    alert('$message');
    window.location.href='../html/choicePage.php';
  </script>";
  $x++;
}    // it's still not posted, never will be in this request, so this will keep looping

foreach($countryArr as $country) { // you only an array of empty strings, but won't reach here
  echo $country;
}

while(!isset($\u POST['stopButton']){
-这应该是如何工作的?我声明您的代码从未添加任何内容。抱歉,但这听起来好像您缺乏PHP web应用程序最基本的基本工作原理的知识。如果您希望该数组在多个请求中“存活”下来(这样你就可以通过让用户一个接一个地提交项目来添加项目,每次提交一个项目),你已经大错特错了。你需要在两次请求之间将这些数据存储在服务器端,例如使用会话。如果这样做行得通,那么
$searchCountry
设置在哪里?此代码不完整。
$\u POST在哪里[“停止按钮”]
set?你提交表单了吗?
$searchCountry
从哪里来?无论如何,这个概念是有缺陷的。即使你到达foreach循环,它也会回显一系列脚本块。但是当它被传递到浏览器时,它会运行第一个脚本块,其中包含重定向到另一个p的指令年龄。因此,其他脚本块将永远不会被看到或使用。我不确定您在这里想要实现什么功能,但您肯定需要考虑一种更好的方法。如果您解释您的总体需求,那么也许我们可以制作一些有用和可用的东西。