必承其重 | 欲带皇冠 2017-06-26 14:47 采纳率: 0%
浏览 16

Ajax调用返回相同的结果

I have a jquery modal button, where I want to display information from my database.

I have a field in the database where I want everything to be identified by it is called "number" The modal button is displayed in a table which is already being looped from a SQL query displaying all entries

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>

Ideally, the information should be changed based on the different items that are being clicked. but it is staying the same.

  • 写回答

1条回答 默认 最新

  • weixin_33743703 2017-06-27 07:08
    关注

    Please try to use following code , there are so many typo mistakes you have in your code.

    <?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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。