Php 如何使用ajax在while循环中捕获变量?

Php 如何使用ajax在while循环中捕获变量?,php,ajax,while-loop,Php,Ajax,While Loop,我有一个循环来回答类似这样的问题: <?php while ($u=mysql_fetch_array($result)){ ?> <table> <tr> <td>Question_ID</td> <td>Question</td> <td>Answer</td> </tr> <tr> <td>&

我有一个循环来回答类似这样的问题:

<?php
while ($u=mysql_fetch_array($result)){
?>     
<table>
 <tr>
     <td>Question_ID</td>
     <td>Question</td>
     <td>Answer</td>
 </tr>
 <tr>
     <td><? echo $u['question_id'];?></td>
     <td><? echo $u['question'];?></td> 
     <td> 
         <form>
         <input type="hidden" value="echo $u['question_id'];?>" />
         <input type="text"/>
         <a href="#" onClick="ajax_answer();">Send Answer</a>
         </form>
     </td>
 </tr>
</table>
<?php
} 
?>  
  question_id = $("#question_id").val();
var theid = $(this).attr('id'); // this being the a tag that was clicked

// then just strip off the leading "q" and you have your id.
var thehiddenid = theid.replace('q', '');

问题编号
问题:
答复
例如,如果用户回答了页面上出现的第三个问题,我的问题是如何捕获编写的文本和问题id,以便将这些变量发送到php页面

<script>
   function ajax_answer(){
       $.ajax({
       question_id = ??? //how do I capture this variable?
       answer = ??? //how do I capture this variable?
       url:'answers.php',
       type:'POST',
       dataType:'text/html',
       data:'question_id='+question_id + '&answer='+answer,
       success: function(){
                          };
             });
       };
</script>

函数ajax_answer(){
$.ajax({
问题_id=???//如何捕获此变量?
答案=???//如何捕获此变量?
url:'answers.php',
类型:'POST',
数据类型:'text/html',
数据:“问题id=”+问题id+”&答案=”+答案,
成功:函数(){
};
});
};

谢谢

您可以更改表单并添加
id
以便于选择:

     <form>
     <input type="hidden" value="<?php echo $u['question_id'];?>" id="question_id" />
     <input type="text"/>
     <a href="#" onClick="ajax_answer();">Send Answer</a>
     </form>
或者,如果您的表单只有一个问题id的隐藏字段:

question_id = $("input[type=hidden]").val();

注:请考虑使用<代码> 你不给他们一个ID。我会给一个标签一个前缀的ID,这样你就可以使用同一个ID来获得你的相关输入值:

<a href="#" id="q<php echo $u['question_id'];?>" // note that I added a "q" prefix

如果您想在纯javascript中执行此操作,
然后从
onclick
事件将
this
传递给您的
ajax\u答案
函数,如下所示

<?php
  while( $u = mysql_fetch_array( $result ) ) {
?> 
  <tr>
    <td><?php echo $u['question_id'];?></td>
    <td><?php echo $u['question'];?></td> 
    <td>
      <input type="hidden" value="<?php echo $u['question_id'];?>" />
      <input type="text" />
      <a href="#" onclick="ajax_answer( this );">Send Answer</a>
    </td>
  </tr>
<?php
  }
?>
Javascript
动态添加
onclick
事件处理程序。现在,您的函数
ajax\u answer
接受两个参数
question\u id
answer
,我们将通过
单击
事件处理程序传递这两个参数

<script type="text/javascript">
  $(function() {
    $("a.send_answer").on("click", function( event ) {
      event.preventDefault();
      var td  = $(this).parents("td:first");
      var qid = $("input[name=question_id]", td).val();
      var ans = $("input[name=answer]", td).val();

      ajax_answer( qid, ans );
    });
  });
  function ajax_answer( question_id, answer ) {
    /// do your request here
  }
</script>

$(函数(){
$(“a.send_-answer”)。在(“单击”)上,函数(事件){
event.preventDefault();
var td=$(this.parents(“td:first”);
var qid=$(“输入[name=question_id]”,td).val();
var ans=$(“输入[name=answer]”,td).val();
答案(qid、ans);
});
});
函数ajax\u-answer(问题id,答案){
///你的要求在这里吗
}

如果在元素中添加一个
id
属性,然后使用domeElement使用javascripts'
getElementById
,那么会更容易。html没有打开的php标记
这是另一个问题。当然,我只是说这是一个打字错误。。别担心,没有否决票,兄弟,冷静lol:)嗨,谢谢你的回答,但是表单在一个循环中,所以我可能有很多表单和很多ID。如何调用正确的id?好的,但是如何将文本写入输入字段?同样,您需要为HTML元素提供一个唯一的id,以便以后可以通过该id引用它们。如果你想,你可以使用相同的问题编号,只需在其前面加上“t”之类的前缀。
<?php
  while( $u = mysql_fetch_array( $result ) ) {
?> 
  <tr>
    <td><?php echo $u['question_id'];?></td>
    <td><?php echo $u['question'];?></td> 
    <td>
      <input type="hidden" name="question_id" value="<?php echo $u['question_id'];?>" />
      <input type="text" name="answer" />
      <a href="#" class="send_answer">Send Answer</a>
    </td>
  </tr>
<?php
  }
?>
<script type="text/javascript">
  $(function() {
    $("a.send_answer").on("click", function( event ) {
      event.preventDefault();
      var td  = $(this).parents("td:first");
      var qid = $("input[name=question_id]", td).val();
      var ans = $("input[name=answer]", td).val();

      ajax_answer( qid, ans );
    });
  });
  function ajax_answer( question_id, answer ) {
    /// do your request here
  }
</script>