2017-04-14 42 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