2017-04-14 43 views
0

我在想如何设置$cityError变量?我认为这个问题是因为我$cityError是实际$cityError变量中,但我要保持我的下形式echoif声明:

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; 

Here is my image of the form until submitting

我的变量已经成立,但我不知道想要设置它直到我的表单提交。

如果我做这样的(开关的地方):

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 . 
'<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>'; 

Here is my image of the form until submitting

它只是让2句就算我不提交表单。提交之后,没有任何变化。

+0

您不想在提交后显示FORM吗?或者,你想做什么。请详细说明。 –

+0

这个'echo $ cityError。 '

'似乎是问题所在。你在那里试图做的是用'$ cityError'变量连接一些字符串?如果这是**首次出现** $ cityError,那么您正试图在内置函数中访问一个未定义的变量。这是错误的原因。 –

+0

我想在表单提交后显示错误。我不想在表单提交之前展示任何内容。 – Lukas

回答

0

将整个错误的东西包装在一个新的如果是这样的检查只有当提交的东西。

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>'; 
+0

在这种情况下,错误:未定义索引即将到来。 –

+0

我只添加了if循环。它在哪里错误未定义的索引?我假设'$天气'是在其他地方生成的。 – RST

+0

谢谢,它工作:)顺便说一句,我的数组是$ weather = array('rain','sunshine','clouds','hail','snowet','snow','wind'); – Lukas