Ajax调用返回相同的结果

时间:2017-06-26 14:47:01

标签: javascript php jquery sql ajax

我有一个jquery模式按钮,我想在其中显示数据库中的信息。

我在数据库中有一个字段,我希望所有内容都被识别出来,称为" number" 模态按钮显示在一个表中,该表已从显示所有条目的SQL查询中循环

while ($row = getarray($res)){

    <tbody>
    <tr>
    <td> <? $row["state"] ?> </td>
    <td><? $row["number"] ?></td>
    <td>
    <button type="button" class="btn btn-primary" onclick="detailsmodal(<?
    $row["number"])">Description</button>
  </td>
  </tr>


<!-- Modal Button -->
<button type ="button" class="btn btn-primary" onclick="detailsmodal(<?= $row['number']; ?>)>Modal Button</button>
<!-- Code for modal -->
<?php
$number =$_POST["number"];
$number =(int)$number;
$modalsql = myquery($dbvariable,"SELECT * FROM myTable WHERE Number = '$number' ");
$result2 = myfunction_fetch_array($modalsql);
ob_start();

?>

<div class="modal fade" id="myModal" data-keyboard="false" data-backdrop="static" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" onclick="closeModal()" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title"> <?= $result2['number']?></h4>
  </div>
  <div class="modal-body">
    <p><strong>Field 1:</strong><?= $result2['number']?> </p>
    <p><strong>Field 2:</strong><?= $result2['field_2']?>  </p>
    <p><strong>Field 3</strong><?= $result2['field_3']?> </p>
    <p><strong>Field 4:</strong><?= $result2['field_4']?> </p>
    <p><strong>Field 5:</strong><?= $result2['field_5']?></p>
    <p><strong>Field 6:</strong><?= $result2['field_6']?> </p>
    </div>
    <div class="modal-footer">
     <button type="button" class="btn btn-default" onclick="closeModal()">Close</button>
  </div>
  </div>
  </div>
  </div> 

<?php 
ob_get_clean();
?>

<!-- Script for the modal-->
<script>
    function detailsmodal(number){

        var data = {"number" : number};
        jQuery.ajax({
        url : "/page1/page2",
        method : "post",
        data : data,
        success: function(){
            jQuery("body").append(data);
            jQuery("#myModal").modal("toggle");
        },
        error: function(){
            alert("Something went wrong!");
        }
        });
    }
</script>

理想情况下,应根据所点击的不同项目更改信息。但它保持不变。

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码,您的代码中有很多错字错误。

<?php while ($row = getarray($res)){ ?>

        <tbody>
          <tr>
            <td> <? $row["state"] ?> </td>
            <td><? $row["number"] ?></td>
            <td>
              <button type="button" class="btn btn-primary" onclick="detailsmodal(<?=
              $row["number"]) ?>">Description</button>
            </td>
          </tr>


          <!-- Modal Button -->
          <button type ="button" class="btn btn-primary" onclick="detailsmodal(<?= $row['number']; ?>)">Modal Button</button>
          <!-- Code for modal -->
          <?php
          $number =$_POST["number"];
          $number =(int)$number;
          $modalsql = myquery($dbvariable,"SELECT * FROM myTable WHERE Number = '".$number."' ");
          $result2 = myfunction_fetch_array($modalsql);
          ob_start();

          ?>

          <div class="modal fade" id="myModal" data-keyboard="false" data-backdrop="static" role="dialog">
            <div class="modal-dialog">
              <!-- Modal content-->
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" onclick="closeModal()" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                  <h4 class="modal-title"> <?= $result2['number']?></h4>
                </div>
                <div class="modal-body">
                  <p><strong>Field 1:</strong><?= $result2['number']?> </p>
                  <p><strong>Field 2:</strong><?= $result2['field_2']?>  </p>
                  <p><strong>Field 3</strong><?= $result2['field_3']?> </p>
                  <p><strong>Field 4:</strong><?= $result2['field_4']?> </p>
                  <p><strong>Field 5:</strong><?= $result2['field_5']?></p>
                  <p><strong>Field 6:</strong><?= $result2['field_6']?> </p>
                </div>
                <div class="modal-footer">
                 <button type="button" class="btn btn-default" onclick="closeModal()">Close</button>
               </div>
             </div>
           </div>
         </div>

         <?php
         ob_get_clean();
         ?>

         <!-- Script for the modal-->
         <script>
          function detailsmodal(number){

            var data = {"number" : number};
            jQuery.ajax({
              url : "/page1/page2",
              method : "post",
              data : data,
              success: function(){
                jQuery("body").append(data);
                jQuery("#myModal").modal("toggle");
              },
              error: function(){
                alert("Something went wrong!");
              }
            });
          }
        </script>