php - 尝试使用 ajax 将参数传递给模式?

标签 php ajax

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

这是示例代码:

产品.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>

还有

模态.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>

最佳答案

您需要更正如下两行,然后您的模型 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>";

关于php - 尝试使用 ajax 将参数传递给模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39599962/

相关文章:

jquery - Ruby on Rails 中的简单多人游戏?

php - PHP 脚本中的特殊字符停止执行

php - 不使用 Zend_Application 的 Zend Framework 项目

php - 我需要有关我的网站上已安装的自动检测语言的帮助

php - 这种ajax行为是否正常,安全方面

JQuery AJAX 使用 SOAP Web 服务

php - 如何使用下拉菜单使用 mysql 数据库中的数据自动填充表单

php - 错误:mysql期望参数1为资源

php - Redis key 不会过期 - Laravel、Predis

Javascript 和 Python - 最佳通信方法