[英]Server-side won't recognize elements from $_POST
我在這里問的問題可能很簡單。 我使用 Javascript/Jquery 在頁面上添加元素。 我用 Jquery 添加的元素工作正常,我可以將它們放入 php 中的變量中。 但問題在於 Javascript 之一。
當我檢查瀏覽器時,我可以看到我創建的元素<h6>
在我的情況下被命名為我想要meal_category[1]; meal_category[2]
meal_category[1]; meal_category[2]
等等... name 和 id 是一樣的。 但是當我出於某種原因嘗試print_r
元素時,我得到了Notice: Undefined index: meal_category[1] in...
我確信它確實已定義,但 php 只是不會以某種方式識別它。 這是從瀏覽器inspect element
元素粘貼的生成元素之一,例如:
<h6 class="d-xl-flex justify-content-xl-center align-items-xl-center mb-2" id="meal_category[1]" name="meal_category[1]" style="margin-bottom: 0px; margin-top: 0px; background: rgb(255, 35, 81); border-radius: 15px; font-size: 14px; padding-left: 5px; padding-right: 6px; color: rgb(255, 255, 255) !important;">Burgeriai
<button class="btn btn-primary" id="removeCategory[1]" onclick="removeCategory(this)" type="button" style="background: transparent;border-style: none;padding: 0px;margin-left: 3px;">X</button>
</h6>
我不知道我是否解釋清楚了一切,如果有什么遺漏,請告訴我。 這也是我的 php 代碼:
$mealCategories = $_POST['meal_category'];
$ingredientNames = $_POST['ingredient_name'];
$ingredientPrices = $_POST['ingredient_price'];
$mealsizeSelect = $_POST['mealsize_select'];
$mealsizeCorrect = $_POST['mealsize_correct'];
print_r($mealCategories[1]);
除了meal_category
一個之外,所有其他元素都有效。 是因為我使用 Jquery 創建了其他元素,而這個使用了 js? 下面是我如何創建這個<h6>
。
var cat = document.createElement('H6');
cat.className = 'd-xl-flex justify-content-xl-center align-items-xl-center mb-2';
cat.style = 'margin-bottom: 0px;margin-top: 0px;background: #ff2351;color: rgb(255,255,255)!important;border-radius: 15px;font-size: 14px;padding-left: 5px;padding-right: 6px;';
cat.id = 'meal_category['+selectCategory.counter+']';
cat.setAttribute('name', 'meal_category['+selectCategory.counter+']');
cat.innerHTML = selectedText + '<button class="btn btn-primary" id="removeCategory['+selectCategory.counter+']" onClick="removeCategory(this)" type="button" style="background: transparent;border-style: none;padding: 0px;margin-left: 3px;">X</button>';
document.getElementById('selectedCategories').appendChild(cat);
執行常規表單提交時,只會提交表單元素。 文檔中的當前表單元素列表:
<input>
<label>
<select>
<textarea>
<button>
<fieldset>
<legend>
<datalist>
<output>
<option>
<optgroup>
由於您的元素是<h6>
標記,因此不會檢測到它們,盡管您為它們附加了name
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.