如何将值从Jquery传递给PHP? HTML是隐藏的输入表单字段

时间:2017-07-04 06:53:01

标签: php jquery

我有一张表格:

<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,我不想使用带有“选项”的下拉菜单

2 个答案:

答案 0 :(得分:2)

这是解决方案。

你在代码中犯了几个错误。

  • 您在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;
?>

&#13;
&#13;
$(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
        
    
&#13;
<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>
&#13;
&#13;
&#13;

试试这段代码希望它会对你有所帮助。

答案 1 :(得分:1)

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";
}
相关问题