【发布时间】:2017-07-04 06:53:00
【问题描述】:
我有一个表格:
<form action="post.php" method="POST">
<p class="select1">Northern Cape</p>
<p class="select1">Eastern Cape</p>
<p class="select2" >New Goods</p>
<p class="select2" >Used Goods</p>
<input id="submt" type="submit" name="submit" value="Submit">
</form>
JQUERY .... 将输入/值附加到所选项目:
$(document).ready(function() {
$('.select1').click(function() {
if ($(this).text() == "Northern Cape"){
$(this).append("<input id='firstloc' type='hidden' name='province'
value='Northern Cape' />");
$("#firstloc").val('Northern Cape');
}; // end of if statement
if ($(this).text() == "Eastern Cape"){
$(this).append("<input id='firstloc' type='hidden' name='province'
value='Eastern Cape' />");
$("#firstloc").val('Eastern Cape');
}; // end of if statement
}); // end of click function
}); // end of document ready
$(document).ready(function() {
$('.select2').click(function() {
if ($(this).text() == "New Goods"){
$(this).append("<input id='category' type='hidden' name='cat'
value='New Goods' />");
$(this).val('New Goods');
};
if ($(this).text() == "Used Goods"){
$(this).append("<input id='category' type='hidden' name='cat'
value='Used Goods' />");
$("#category").val('Used Goods');
};
});
});
如何将值传递给 PHP,即。第一个值省第二个值类别?
<?php
$province = $_POST['province'];
$category = $_POST['cat'];
echo $province;
echo $category;
?>
我在传递给 PHP 时收到一条消息 Undefined index:cat。 用户必须选择 2 个项目并且值必须传递给 PHP,我不想使用带有“选项”的下拉菜单
【问题讨论】:
-
在表单提交时它会通过,你不明白吗?
-
我得到未定义的索引:猫
-
选择一个值后尝试查看源以检查html页面中是否添加了输入字段
-
html 出现在开发者控制台中,但两个值都没有被传递,只有一个值被传递
-
html 是否出现在表单中?还是在外面?