[英]How do I pass value from Jquery to PHP ? HTML is a hidden input form field
我有一个表格:
<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,我不想使用带有“选项”的下拉菜单
这是解决方案。
您在代码中犯了几个错误。
;
在if语句之后 $document.ready
标签 post.php
是否发布数据 input type hidden
追加了,而没有将其删除。 post.php中
<?php
$province = '';
$category = '';
if(isset($_POST['province'])):
$province = $_POST['province'];
endif;
if(isset($_POST['cat'])):
$category = $_POST['cat'];
endif;
echo $province;
echo $category;
?>
$(document).ready(function() { $('.select1').click(function() { if ($(this).text() == "Northern Cape"){ $("#firstloc").remove(); $(this).append("<input id='firstloc' type='hidden' name='province' value='Northern Cape' />"); } // end of if statement if ($(this).text() == "Eastern Cape"){ $("#firstloc").remove(); $(this).append("<input id='firstloc' type='hidden' name='province' value='Eastern Cape' />"); } // end of if statement }); $('.select2').click(function() { if ($(this).text() == "New Goods"){ $("#category").remove(); $(this).append("<input id='category' type='hidden' name='cat' value='New Goods' />"); } if ($(this).text() == "Used Goods"){ $("#category").remove(); $(this).append("<input id='category' type='hidden' name='cat' value='Used Goods' />"); } }); // end of click function }); // end of document ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <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> </html>
尝试使用此代码,希望对您有所帮助。
PHP不会为所有发布操作设置$ _POST,例如text / xml:
尝试向表单中添加添加application / x-www-form-urlencoded或multipart / form-data。
<form action="post.php" method="POST" enctype="multipart/form-data">
另外,请确保您没有损坏导致表单标签过早关闭的HTML。 使用Web开发人员工具验证发布的内容和内容类型。
您还应该使用isset来检查变量的存在:
if (isset($_POST["cat"])) {
$cat = $_POST["cat"];
echo $cat;
echo " is the category";
}
else
{
$cat = null;
echo "no category supplied";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.