2016-09-20 56 views
1

我有一个页面(products.php),它有一个动态填充的表格。每一行都有一个打开模式的按钮,但为了将模式传递给模态:首先,我在当前页面(products.php)中声明模态。其次,我使用另一个页面(modal.php)中的ajax调用模态的其余部分。在使用ajax之前,我已经在选择下拉菜单中选择了所有选项,但我似乎无法通过显示该模式来做同样的事情。我可以使用PHP的href,但这会导致我的jquery脚本,我提交到我的模式数据库不再工作,这就是为什么我想通过AJAX做到这一点。我想知道为什么这不适合ajax。试图将params传递给使用ajax的模态?

继承人的示例代码:

products.php

<html> 
    <header> 
    <script> 
     function modalValues(val1,val2){ 
     if(window.XMLHttpRequest){ 

     xhttp = new XMLHttpRequest(); 

     }else{ 

     xhttp = new ActiveXObject("Microsoft.XMLHTTP"); 

     } 

     xhttp.onreadystatechange = function(){ 
      if(xhttp.readyState == 4 && xhttp.status == 200){ 
       document.getElementById("load_here").innerHTML = xhttp.responseText; 
      } 
     }; 

     xhhtp.open("POST","modal.php?id3="+val1+"&id="+val2,true); 
     xhttp.send(); 
     } 
    </script> 
    </header> 

    <body> 

    <div class="modal fade" data-keyboard="false" id="product_customerModal" tabindex="-1" role="dialog" aria-labelledby="product_customerModalLabel" aria-hidden="true"> 
    <div class="modal-dialog modal-lg"> 
<div id="load_here" class="modal-content"> 
</div> 
    </div> 
    </div> 

    <table> 
    <?php 
    global $link; 

    $query = "blah,blah"; 
    $result_set = mysqli_query($link,$query); 

    $number = mysqli_num_rows($result_set); 

    for($count=0;$count<$number;$count++){ 
    $result = mysqli_fetch_array($result_set); 
    echo "<tr>"; 
    echo "<td>{$result['field1']}</td>"; 
    echo "<td>{$result['field2']}</td>"; 
    echo "<td><button onclick='modalValues(<?php echo $result['field1'];?>,<?php echo $result['field2'];?>) data-toggle="modal" data-target='#product_customerModal'></button></td>"; 
    } 
    ?> 

    </table> 

    </body> 
    </html> 

而且

modal.php

<div class="modal-header"> 
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
     <h4 class="modal-title" > Date: (<?php echo $date;?>)</h4></div 
    <div class="modal-body"> 
<?php 
//some php code 
?> 
    <div> 
    <div class="modal-footer"> 
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
    </div> 

回答

0

您需要更正两行有如下几点,然后您的模型html将在那里: xhttp.open("POST","test2.php?id3="+val1+"&id="+val2,true);不是xhhtp

echo "<td><button onclick='modalValues('".$result['field1']."','".$result['field2']."') data-toggle='modal' data-target='#product_customerModal'></button></td>";

+0

谢谢你,这对我有效 –