douchi0028 2014-08-19 13:57
浏览 38
已采纳

如何从提交的do ... while表单中获取值

I have the following code with a form inside a list that should submit the element through Ajax. The values in the form is repeated through a do ... while loop, so that the list become a dynamically list, like in this picture:

SS of the GUI

But when I click a button, the only element that is sent through the Ajax code is the value of the last button, even though I click on Oranges for example.

The code is as follow:

<script>
function submitForm() {
    $.ajax({type:'POST', url: 'jQuery-ajax-demo.php', data:$('#MyJobsForm').serialize(), success: function(response) {
    $('#myJobs_Right').find('.form_result').html(response);
    }});

    return false;
}
</script>

</head>

<body>

<ul id="btn_MyJobs" data-role="listview" data-inset="true">
<li id="MyJobs_List" class="push">
  <form id="MyJobsForm"  onsubmit="return submitForm();">
    <?php do { ?>
      <input type="hidden" name="name" value="<?php echo $row_Recordset1['cargo']; ?>">
      <input type="submit" name="submit" value="<?php echo $row_Recordset1['cargo']; ?>">
      <br/>
      <br/>
      <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
  </form>
</li>
</ul>

<div id="myJobs_Right">
 <div class="form_result"> </div>
</div>

</body>

The jQuery-ajax-demo.php page looks like this:

<?php
if(isset($_POST['name'])) {
    $name = $_POST['name'];

    ?>
    Your Name Is: <?php echo $name; ?><br />
    <?php
    die();
}
?>
  • 写回答

1条回答 默认 最新

  • douzhao6584 2014-08-19 14:08
    关注

    If you have to do it this way, then you might try creating a new form for each button:

    <?php do { ?>
      <form onsubmit="return submitForm(this);"> <!--Note I added "this" -->
          <input type="hidden" name="name" value="<?php echo $row_Recordset1['cargo']; ?>">
          <input type="submit" name="submit" value="<?php echo $row_Recordset1['cargo']; ?>">
          <br/>
          <br/>
      </form>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    

    Then the only value that will get posted will be the hidden value that is in the form whose button you clicked. I updated the onsubmit call above to include this in the call. So you can do the below in your function:

    function submitForm(form) {
        var $form = $(form);
        $.ajax({
            type:'POST', 
            url: 'jQuery-ajax-demo.php', 
            data:$form.serialize(), 
            success: function(response) {
                $('#myJobs_Right').find('.form_result').html(response);
            }
        });
    
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的