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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?