我將表單數據傳遞到通過服務器發送的電子郵件中(在這種情況下,我使用SendGrid,但我認爲這與PHP相關,而不是API)和I在表格上有一個可重複的工作部分。他們可以在表單上添加多個工作來填寫,如果他們這樣做,只需克隆字段並將每個字段標籤/名稱和ID前綴爲一個整數,例如wh-company2
和wh-company2
等等。他們最多可以添加10個工作。PHP isset可選字段數據 - 未定義索引
然後,我將這個傳遞到我發送的電子郵件模板中。一切都很好,但是當涉及到工作部分時,我想進行檢查,例如,如果存在wh-company8
,那麼它將返回8個工作數據。
<?php for ($count = 1; $count <= 10; $count++) : ?>
<?php if (isset($submitted_data['wh-company' . $count . '\''])) : ?>
<p><strong>Work History <?php echo $count; ?></strong><br/>
Company: <?php echo $submitted_data['wh-company' . $count . '\'']; ?><br/>
Position: <?php echo $submitted_data['wh-position' . $count . '\'']; ?><br/>
Company address: <?php echo $submitted_data['wh-address-1' . $count . '\'']; ?><br/>
Date worked from: <?php echo $submitted_data['wh-date-started' . $count . '\'']; ?><br/>
Date worked until: <?php echo $submitted_data['wh-date-ended' . $count . '\'']; ?><br/>
Reasons for leaving: <?php echo $submitted_data['wh-reasons-you-left' . $count . '\'']; ?><br/>
Referee name(s): <?php echo $submitted_data['wh-referee-name' . $count . '\'']; ?><br/>
Referee position: <?php echo $submitted_data['wh-referee-position' . $count . '\'']; ?><br/>
Referee phone number: <?php echo $submitted_data['wh-referee-telephone' . $count . '\'']; ?><br/>
Referee email: <?php echo $submitted_data['wh-referee-email' . $count . '\'']; ?><br/></p>
<?php endif; ?>
<?php endfor; ?>
這是我到目前爲止。我沒有isset
而只是if ($submitted_data['wh-company' . $count . '\'']) :
,但它爲10個項目中的每一個返回了Notice: Undefined index
錯誤。然後我包括isset
,但現在不返回任何內容。
我在做什麼明顯錯誤?
你爲什麼要添加這個'\''? –
\是爲了逃避''',但是,在索引字符串的末尾添加'''字符似乎有點有趣。這可能是導致所有索引首先不正確的原因。 – GKnight
發佈您的HTML表單以查看字段名稱。 –