如何在输入字段中显示AJAX响应并将该值传递给PHP?

如何在输入字段中显示AJAX响应并将该值传递给PHP?,php,jquery,html,ajax,Php,Jquery,Html,Ajax,我从我的AJAX中获得Id值,并且我还能够显示它。我在AJAX中设置Id值输入文本,并将其显示在HTML上,如下所示,现在我必须将该值传递给PHP以从数据库获得结果。 你知道怎么做吗 AJAX $(document).ready(function() { arr = []; $("[type=checkbox]").click(function() { $.ajax({ type: "POST", url: "includes/compare_proces

我从我的AJAX中获得Id值,并且我还能够显示它。我在AJAX中设置Id值输入文本,并将其显示在HTML上,如下所示
,现在我必须将该值传递给PHP以从数据库获得结果。 你知道怎么做吗

AJAX

$(document).ready(function() {
  arr = [];
  $("[type=checkbox]").click(function() {
    $.ajax({
      type: "POST",
      url: "includes/compare_process.php", // 
      data: 'users=' + arr,
      dataType: 'json',
      success: function(msg) {
        $("#pics_name,#pics_Id").empty();
        $.each(msg, function() {
          $("#pics_Id").append("<input type=hidden value=" + this.Id + " id=compare_id name=compare_id>");
          //alert(msg.id);
        });

      },
      error: function() {
        alert("failure");
      }
    });
  });
});


//tried AJAX
     $(document).ready(function(){
        $('#search-button').click(function(){
            $.ajax( {
                type: "GET",
                url: 'includes/compare_process.php?key=compare_details',
                data: $('#search-form').serialize(),
                success: function(response) {
                    //$('#response').html(response);
                    alert(response);
                }
            } );
        });
    });
$(文档).ready(函数(){
arr=[];
$(“[type=checkbox]”。单击(函数(){
$.ajax({
类型:“POST”,
url:“includes/compare_process.php”,//
数据:“用户=”+arr,
数据类型:“json”,
成功:功能(msg){
$(“#pics#u name,#pics#u Id”).empty();
$.each(msg,function(){
$(“#pics#u Id”)。追加(“”);
//警报(msg.id);
});
},
错误:函数(){
警报(“故障”);
}
});
});
});
//尝试AJAX
$(文档).ready(函数(){
$(“#搜索按钮”)。单击(函数(){
$.ajax({
键入:“获取”,
url:'includes/compare\u process.php?key=compare\u details',
数据:$(“#搜索表单”).serialize(),
成功:功能(响应){
//$('#response').html(response);
警报(响应);
}
} );
});
});
PHP

<div class="bg-popup" style="display: none;" id="open_compare_popup">
//popup code here
    <?php 
     $val2=htmlentities($_GET['compare_id']);
     echo $val2;
     $sql = "SELECT * FROM `request` WHERE Id IN($val2)";
     ?>
//end popup code here

//弹出代码在这里
//结束弹出代码在这里
这是我从AJAX获得的输出

<form action="#" method="get" id="search-form">
<span id="pics_Id">
<input value="4869" id="compare_id" name="compare_id" type="hidden">//output
<input value="4884" id="compare_id" name="compare_id" type="hidden">//output
<input value="5010" id="compare_id" name="compare_id" type="hidden">//output
</span>
<button class="action action--button action--compare" type="button" id="search-button" name="search-button"><span class="action__text">Compare</span></button>
</form>

//输出
//输出
//输出
比较

单击按钮后,我必须将输入值传递给PHP。

为什么不将第二个AJAX放在第一个成功块中?如果您确实需要get方法,则键入:'get',data:'compare_id='+msg.id是否可能因为表单中有一个按钮而提交表单?除非您指定type=“button”“在某些浏览器中,按钮可能会提交formI,但并不理解您的意思。在ajax之后添加另一个字段,然后做些什么?”?您还添加了大量重复的html ID。实际上,您可能根本不需要使用表单字段。如果您没有从用户那里收集任何数据,那么您可以尝试完全删除表单标记,并将onClick操作放在按钮上。实际上,我从数据库中获取Id,并且该Id必须传递给php。我只是想知道我该怎么做。我的逻辑是在输入字段中设置该Id。还有其他选择吗??