通过javascript代码发送php变量onclick

时间:2017-06-28 16:38:13

标签: javascript php jquery html5

我有一个表单,当我点击显示按钮时,会打开一个弹出窗口。直到现在一切都很好。

但是点击显示按钮后,我还想在弹出窗口中发送php变量,并希望显示与该变量相关的值。怎么可能?

我的表位于

之下
<table align="center">
    <?php 
       include("connection.php");
       $query1=mysql_query("select * from career") or die(mysql_error());
       while($row1=mysql_fetch_array($query1))
       {
            extract($row1);
    ?>
    <tr>
        <td>Designation</td>
        <td><input type="text" name="des" value="<?php echo $designation; ?>"></td>
        </td>
    </tr>
    <tr>
        <td>Number of position</td>
        <td><input type="text" name="des" value="<?php echo $no_of_position; ?>"></td>
        </td>
    </tr>
    <tr>
        <td>Experience</td>
        <td><input type="text" name="des" value="<?php echo $experience; ?>"></td>
        </td>
    </tr>
    <tr>
        <td>Qualification</td>
        <td><input type="text" name="des" value="<?php echo $qualification; ?>"></td>
        </td>
    <tr>
        <td>Job profile</td>
        <td><input type="text" name="des" value="<?php echo $job_profile; ?>"></td>
    </tr>
    <tr>
        <td><?php echo"$id"; ?></td>
        <td><button onclick="document.getElementById('id01').style.display='block'" style="width:auto;" id="id1">Show</button>
       </td>
    </tr>
    <tr>
        <td></td>
        <td>&nbsp;</td>
    </tr>
    <?php } ?>
</table>

在上面的代码中,当我点击Show Button,然后弹出窗口打开,代码如下:

<div id="id01" class="modal1">
    <form class="modal1-content animate" action="xyz.php">
        <div class="imgcontainer">
            <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close modal1">&times;</span>
        </div>
        <div class="container">
           <div>Content</div>
        </div>
    </form>
</div>

<script>
    // Get the modal1
    var modal1 = document.getElementById('id01');

    // When the user clicks anywhere outside of the modal1, close it
    window.onclick = function(event) {
        if (event.target == modal1) {
            modal1.style.display = "none";
        }
    }
</script>

我想发送 id变量,我可以在弹出窗口中从数据库中获取值。

谢谢。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要对这样的php页面进行ajax调用:

<强> modalService.php

if(!isset($_GET['id']){
     echo "no request";
     exit;
}
$id = $_GET['id'];
//Do your query and echo the modal content

表格

<td><?php echo $id; ?></td>
<td>
    <button onclick="myFunction('<?php echo $id; ?>')" >Show</button>
 </td>

<强> JS

function myFunction(id){
    var modal = document.getElementById("id01");
    modal!==null && (modal.style.display="block");
    var modalServiceUrl = "./modalService.php?id="+id;
    //xhr to get modalService
}

编辑:使用JQUERY

<button data-queryid="<?php echo $id; ?>" class="showModal">Show</button>

jQuery(document).ready(function($){
    var modal = document.getElementById("id01");
    $("button.showModal").on("click", function(){
        modal.fadeIn();
        var id = $(this).attr("data-queryid");
        var modalServiceUrl = "./modalService.php?id="+id;
        modal.load(modalServiceUrl);
    });
});