简体   繁体   English

jQuery的不发送隐藏的输入值到后端

[英]jquery not sending hidden input values to the backend

Here is my code, I am trying to send the hidden input values to the backend via jquery, but input values is not sending, I don't know why, because it looks ok for me, also the php $_POST vars is with same name as input names. 这是我的代码,我正在尝试通过jquery将隐藏的输入值发送到后端,但是输入值未发送,我不知道为什么,因为它对我来说还可以,而且$_POST vars也是相同的名称作为输入名称。

I need to send the value of the hidden input and get on the backend with the input name . 我需要发送隐藏输入 ,并使用输入名称进入后端。

 <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> 

I am trying to send the hidden input values value="unfollow" and others , it should work since the php var is with same name of the inputs. 我试图发送隐藏的输入值value="unfollow"和其他,它应该工作,因为php var与输入的名称相同。 my php backend variables: 我的PHP后端变量:

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

You need to close ur tab before the for input. 您需要在输入之前关闭ur标签页。 Or move the input before the button. 或将输入移动到按钮之前。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM