【发布时间】:2021-04-17 09:34:44
【问题描述】:
我在这里问的问题可能很简单。我使用 Javascript/Jquery 在页面上添加元素。我使用 Jquery 添加的元素工作正常,我可以将它们添加到 php.ini 中的变量中。但问题在于 Javascript 之一。
当我检查浏览器时,我可以看到我创建的元素 <h6> 在我的例子中被命名为我想要的 meal_category[1]; meal_category[2] 等等......名称和 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);
【问题讨论】:
-
print_r($mealCategories)的输出是什么(没有明确指定的索引)? -
这些数据是如何传输到 PHP 中的?某处是否有 AJAX 调用?
-
h6不是表单元素,因此未检测到它看起来很正常。其他元素也是标题标签吗? -
其他都是
<input>元素,我现在明白了,基本上<form>看不到<div>、<h6>这样的元素……对吧? -
同意@El_Vanja。您应该在表单中使用输入标签来使用 POST on php 访问它
标签: javascript php jquery post