Php 如何正确设置$cityError变量?

Php 如何正确设置$cityError变量?,php,html,forms,variables,post,Php,Html,Forms,Variables,Post,我想知道如何设置$cityError变量?我认为问题是因为我的$cityError在实际的$cityError变量下,但我必须将if语句保留在我的表单echo下: echo $cityError . '<form action="" method="POST"> <p>Įvertinkite mėnesio orą </p> Miestas: <input type="text" name="cityc"><br />

我想知道如何设置
$cityError
变量?我认为问题是因为我的
$cityError
在实际的
$cityError
变量下,但我必须将
if
语句保留在我的表单
echo
下:

echo $cityError . 
 '<form action="" method="POST">
    <p>Įvertinkite mėnesio orą </p>
    Miestas: <input type="text" name="cityc"><br /><br />
    Mėnesis: <input type="text" name="month"><br /><br />'; 
    foreach ($weather as $value) {
      echo '<input type="checkbox" name="' . $value . '" value="' . $value . '"> '. ucfirst($value) . '<br />';
    }
    echo '<br /><input type="submit" value="Tęsti">
  </form>';

if (isset($_POST['cityc']) && !empty($_POST['cityc'])) {
  if (isset($_POST['month']) && !empty($_POST['month'])) {
    if (isset($_POST['rain']) || isset($_POST['sunshine']) ||
      isset($_POST['clouds']) || isset($_POST['hail']) ||
      isset($_POST['hail']) || isset($_POST['sleet']) ||
      isset($_POST['snow']) || isset($_POST['wind'])) {
  } else { echo 'Pasirinkite orą!'; }
 }
} else {
  $cityError = 'Įrašykite miestą!';
}
echo 'CIA: ' .$cityError;
echo$cityError。
'
Įvertinkite mėnesio或ą

密斯塔斯:

Mėnesis:

; foreach($weather作为$value){ 回显“.ucfirst($value)。”
; } 回声'
'; 如果(设置($_POST['cityc'])和&!空($_POST['cityc'])){ 如果(设置($_POST['month'])和&!空($_POST['month'])){ 如果(isset($_POST['rain'])| | isset($_POST['sunshine']))|| isset($_POST['clouds'])| | isset($_POST['hail']))|| isset($_POST['hail'])| | isset($_POST['sleet']))|| isset($_POST['snow'])| | isset($_POST['wind'])){ }否则{echo'Pasirinkite或ą!'} } }否则{ $cityError='rašykite miestą!'; } echo“CIA:”.$cityError;

我的变量已经设置好了,但在提交表单之前我不想设置它

如果我喜欢(交换位置):

if(设置($\u POST['cityc'])和&!空($\u POST['cityc'])){
如果(设置($_POST['month'])和&!空($_POST['month'])){
如果(isset($_POST['rain'])| | isset($_POST['sunshine']))||
isset($_POST['clouds'])| | isset($_POST['hail']))||
isset($_POST['hail'])| | isset($_POST['sleet']))||
isset($_POST['snow'])| | isset($_POST['wind'])){
}否则{echo'Pasirinkite或ą!'}
}
}否则{
$cityError='rašykite miestą!';
}
echo“CIA:”.$cityError;
echo$cityError。
'
Įvertinkite mėnesio或ą

密斯塔斯:

Mėnesis:

; foreach($weather作为$value){ 回显“.ucfirst($value)。”
; } 回声'
';


即使我不提交表格,它也只写了两句话。提交后,没有任何更改。

将整个错误内容包装在一个新的文件中,如果是,则只在提交了某个文件后才进行检查

if ( ! empty($_POST) ) {
 if (isset($_POST['cityc']) && !empty($_POST['cityc'])) {
   if (isset($_POST['month']) && !empty($_POST['month'])) {
     if (isset($_POST['rain']) || isset($_POST['sunshine']) ||
       isset($_POST['clouds']) || isset($_POST['hail']) ||
       isset($_POST['hail']) || isset($_POST['sleet']) ||
       isset($_POST['snow']) || isset($_POST['wind'])) {
   } else { echo 'Pasirinkite orą!'; }
  }
 } else {
   $cityError = 'Įrašykite miestą!';
 }
 echo 'CIA: ' . $cityError;
}
echo  
 '<form action="" method="POST">
    <p>Įvertinkite mėnesio orą </p>
    Miestas: <input type="text" name="cityc"><br /><br />
    Mėnesis: <input type="text" name="month"><br /><br />'; 
    foreach ($weather as $value) {
      echo '<input type="checkbox" name="' . $value . '" value="' . $value . '"> '. ucfirst($value) . '<br />';
    }
    echo '<br /><input type="submit" value="Tęsti">
  </form>';
if(!empty($\u POST)){
如果(设置($_POST['cityc'])和&!空($_POST['cityc'])){
如果(设置($_POST['month'])和&!空($_POST['month'])){
如果(isset($_POST['rain'])| | isset($_POST['sunshine']))||
isset($_POST['clouds'])| | isset($_POST['hail']))||
isset($_POST['hail'])| | isset($_POST['sleet']))||
isset($_POST['snow'])| | isset($_POST['wind'])){
}否则{echo'Pasirinkite或ą!'}
}
}否则{
$cityError='rašykite miestą!';
}
echo“CIA:”.$cityError;
}
回音
'
Įvertinkite mėnesio或ą

密斯塔斯:

Mėnesis:

; foreach($weather作为$value){ 回显“.ucfirst($value)。”
; } 回声'
';
您不想在提交后显示表单吗?或者,你想做什么。请详细说明。这个
echo$cityError'似乎是问题所在。你想做的是用
$cityError
变量连接一些字符串,嗯?如果这是有史以来第一次出现
$cityError
,则您试图访问内置函数中的未定义变量。这是错误的原因。我想在提交表单后显示错误。在提交表单之前,我不想显示任何内容。在这种情况下,也会出现错误:未定义索引。我只添加了if循环。在未定义的索引上它在哪里出错?我假设
$weather
是在其他地方生成的。谢谢,它成功了:)顺便说一句,我的数组是$weather=array('rain','sunshine','clouds','hail','sleet','snow','wind');
if ( ! empty($_POST) ) {
 if (isset($_POST['cityc']) && !empty($_POST['cityc'])) {
   if (isset($_POST['month']) && !empty($_POST['month'])) {
     if (isset($_POST['rain']) || isset($_POST['sunshine']) ||
       isset($_POST['clouds']) || isset($_POST['hail']) ||
       isset($_POST['hail']) || isset($_POST['sleet']) ||
       isset($_POST['snow']) || isset($_POST['wind'])) {
   } else { echo 'Pasirinkite orą!'; }
  }
 } else {
   $cityError = 'Įrašykite miestą!';
 }
 echo 'CIA: ' . $cityError;
}
echo  
 '<form action="" method="POST">
    <p>Įvertinkite mėnesio orą </p>
    Miestas: <input type="text" name="cityc"><br /><br />
    Mėnesis: <input type="text" name="month"><br /><br />'; 
    foreach ($weather as $value) {
      echo '<input type="checkbox" name="' . $value . '" value="' . $value . '"> '. ucfirst($value) . '<br />';
    }
    echo '<br /><input type="submit" value="Tęsti">
  </form>';