【问题标题】:jquery not sending hidden input values to the backendjquery没有将隐藏的输入值发送到后端
【发布时间】:2019-07-23 01:46:33
【问题描述】:

这是我的代码,我试图通过 jquery 将隐藏的输入值发送到后端,但是输入值没有发送,我不知道为什么,因为它看起来对我来说还可以,还有 php $_POST vars 与输入名称同名。

我需要发送隐藏输入并使用输入名称进入后端。

<form id="user_page_form" method="post">
 <input type="hidden" value="<?php echo htmlentities($db_user); ?>" name="user_page" />
  <button id="btn_follow" class="btn btn-outline-success" style="border-radius:8px;">
    <input type="hidden" value="unfollow" name="follow_button" /><i class="fa fa-user"></i>following<i class="fa fa-check"></i></button>
    </form>
    <script>
$(document).ready(function() {
$('#btn_follow').click(function() {
var follow_and_unfollow = $("#user_page_form").serialize();
                     $.post(                            "backend/ajax/follow_and_unfollow.php",
                            follow_and_unfollow
                     ).done(function(data) {   
    				$("#result").html(data);
    						 }).fail(function () {
})    	
}
});
</script>    	
    <div id="result"></div>

我正在尝试发送隐藏的输入值value="unfollow" 和其他值,它应该可以工作,因为 php var 与输入的名称相同。我的 php 后端变量:

$userpage = $_POST['user_page'];
$follow = $_POST['follow_button'];

【问题讨论】:

  • 表单是模态的还是动态生成的?
  • 它在&lt;section&gt;
  • 您在按钮内输入...无效
  • 你应该像formElement.submit(function(e){ /* AJAX here */ e.preventDefault(); })一样运行。页面正在提交,并且按钮问题中的输入。
  • 您的控制台有错误吗?因为我认为您缺少),最后应该是}) }) })..

标签: javascript php jquery html ajax


【解决方案1】:

你需要在输入之前关闭你的标签。 或者将输入移到按钮之前。

<form id="user_page_form" method="post">
 <input type="hidden" value="<?php echo htmlentities($db_user); ?>"name="user_page"/>
 <input type="hidden" value="unfollow" name="follow_button" />
 <button id="btn_follow" class="btn btn-outline-success" style="border-radius:8px;">
 <i class="fa fa-user"></i>following<i class="fa fa-check"></i></button>
 </form>
<script>
 $(document).ready(function() {
   $('#btn_follow').click(function() {
      var follow_and_unfollow = $("#user_page_form").serialize();
                 $.post("backend/ajax/follow_and_unfollow.php",
                        follow_and_unfollow
                 ).done(function(data) {   
                $("#result").html(data);
                         }).fail(function () {
          })        
         }
       });
 </script>      
<div id="result"></div>

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 2013-08-18
    • 2019-12-22
    • 2012-08-04
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多