试图使用ajax将params传递给模态?

时间:2016-09-20 17:05:22

标签: php ajax

我有一个页面(products.php),它有一个动态填充的表。每一行都有一个打开模态的按钮,但为了将参数传递给模态:首先,我在当前页面上声明了模态(products.php)。其次,我使用来自另一个页面(modal.php)的ajax调用其余的模态。我在使用ajax之前已经在选择下拉列表中弹出了所有选项,但这次我似乎无法通过显示模态来做同样的事情。我可以使用php href,但这导致我用于提交到我的模态数据库的jquery脚本不再工作,这就是为什么我想通过ajax这样做。我想知道为什么这不适合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>

1 个答案:

答案 0 :(得分:2)

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

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