【问题标题】:How do I pass value from Jquery to PHP ? HTML is a hidden input form field如何将值从 Jquery 传递到 PHP? HTML 是一个隐藏的输入表单域
【发布时间】: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 是否出现在表单中?还是在外面?

标签: php jquery


【解决方案1】:

这是解决方案。

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

  • 你在 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>

试试这个代码,希望对你有帮助。

【讨论】:

    【解决方案2】:

    PHP 不会为所有 post 操作设置 $_POST,例如 text/xml:

    尝试将 application/x-www-form-urlencoded 或 multipart/form-data 添加到您的表单中以确保。

    <form action="post.php" method="POST" enctype="multipart/form-data">
    

    还要确保您没有损坏的 HTML 导致您的表单标签过早关闭。使用网络开发者工具验证发布的内容和内容类型。

    您还应该使用 isset 来检查变量是否存在:

    if (isset($_POST["cat"])) {
        $cat = $_POST["cat"];
        echo $cat;
        echo " is the category";
    } 
    else 
    {
        $cat = null;
        echo "no category supplied";
    }
    

    【讨论】:

    • 谢谢,做到了。这么多思考,一个人错过了一些小事
    猜你喜欢
    • 1970-01-01
    • 2014-08-17
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多