将jquery - 生成的变量传递给同一页面中的php

时间:2015-02-05 14:07:54

标签: php jquery variables

我有一个脚本,可以在单击该按钮时获取按钮行上的数据。按钮的ID是id ='show-button'。这是脚本:

<script>
        $(document).ready(function(){
             $(".show-button").click(function() {
             var $row = $(this).closest("tr");    // Find the row
             var names = $row.find(".name").text(); // Find the name
             var surname = $row.find(".surname").text(); // Find the surname 
             var lecturer_id = names."_".surname;
             $("#show_dialog").dialog({autoOpen: false});
             $(".show-button").on("click", function() {$("#show_dialog").dialog("open");});
         });
        });
    </script>

最后两行显示一个jquery对话框。 有了这个,我的意思是这些线:

$("#show_dialog").dialog({autoOpen: false});
$(".show-button").on("click", function() {$("#show_dialog").dialog("open");});

现在,我需要将var lecturer_id的值传递给此代码之外的php脚本,但是在同一文档中。这个PHP代码将生成由这两行创建的对话框的内容。让我们假设我只想回显对话框中传递的变量(使用php)。

关于如何使其发挥作用的任何想法?

2 个答案:

答案 0 :(得分:1)

你的问题不是100%清楚,但是,只是一个想法,如果我让你做对:

<script>
$(document).ready(function(){

     $(".show-button").click(function() {
         var $row = $(this).closest("tr");    // Find the row
         var names = $row.find(".name").text(); // Find the name
         var surname = $row.find(".surname").text(); // Find the surname 
         var lecturer_id = names."_".surname;
        $.post( "test.php", { names: names, surname: surname; lecturer_id: lecturer_id })
            .done(function( data ) {
            $("#show_dialog")[0].innerHTML = data ;
            $("#show_dialog").dialog({autoOpen: false});
            $(".show-button").on("click", function() {$("#show_dialog").dialog("open");});
        });

    });
});
    </script>

我同意@JayBlanchard你甚至不需要任何ajax调用,只需生成你的html:

 $(document).ready(function(){

         $(".show-button").click(function() {
             var $row = $(this).closest("tr");    // Find the row
             var names = $row.find(".name").text(); // Find the name
             var surname = $row.find(".surname").text(); // Find the surname 
             var lecturer_id = names."_".surname;

            $("#show_dialog")[0].innerHTML = ' Name = '+names +'; Surname = '+surname ;
                $("#show_dialog").dialog({autoOpen: false});
                $(".show-button").on("click", function() {$("#show_dialog").dialog("open");});


        });
    });

答案 1 :(得分:0)

您可以使用jQuery post或ajax。

$.post( "test.php", { name: "John", time: "2pm" })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

test.php将成为php的回归结束,它需要jquery发送的数据 { name: "John", time: "2pm" }将是您希望发送给php的数据 data将是php的数据输出。

有关详细信息,请参阅http://api.jquery.com/jquery.post/