1

I have a script that gets tha data on the row of a button when that button is clicked. The id of the button is id='show-button'. This is the script:

<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>

The last two significant lines open a jquery dialog box. With that, i mean these lines:

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

Now, I need to pass the value of var lecturer_id to a php script outside this code, but inside the same document. This php code will generate the content of the dialog crated by these two lines. Lets assume that I just want to echo the variable passed inside the dialog box (with the php).

Any idea on how to make it work?

5
  • You'll need to use AJAX. No matter what the PHP will run on the server-side so you will not be accessing the already rendered page though. Commented Feb 5, 2015 at 14:08
  • I figured that much out. But I dont know how to do it. ajax post seems to pass data on other files. I need to pass it within the last two lines that make the call for the dialogbox Commented Feb 5, 2015 at 14:10
  • Is this something that PHP has already rendered and you just need to access it? Commented Feb 5, 2015 at 14:11
  • No, it's not. I use jquery to get the the data in the same row as the button (name and surname). Now lets assume that I want to create a dialog box with $("#show_dialog").dialog({autoOpen: false}); $(".show-button").on("click", function() {$("#show_dialog").dialog("open");}); In this dialogbox I want to echo the name and surname. Commented Feb 5, 2015 at 14:14
  • You would then just use the info from the JS variables to display the name and surname. No need for AJAX or PHP in this case. $('#show_dialog').html(name + ' ' + surname); if #show_dialog is the id of the dialog box. Commented Feb 5, 2015 at 14:18

2 Answers 2

1

Your question is not 100% clear, but, just an idea, if I got you right:

<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>

And I agree with @JayBlanchard you don't even need any ajax call here, just generate your html like:

 $(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");});


        });
    });
Sign up to request clarification or add additional context in comments.

Comments

0

You can use jQuery post or ajax.

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

test.php will be the reciving end of php, where it expects data sent by jquery.
{ name: "John", time: "2pm" } will be the data you are wishing to send off to php.
data will be the the data output by php.

refer to http://api.jquery.com/jquery.post/ for more information

4 Comments

what do you want to do..? the question didn't say you didnt want to do ajax call.
Ok, to clarify it. The data that I take with jquery, I simply want to post it. With php. Inside the dialog box that I create inside the jquery. It should be something like this : <?php echo '<div id="show_dialog" class="ui-dialog-content ui-widget-content">'; echo $jquery_data; echo '</div>'; ?>
so simply inject that post call inside your dialog box. if you are not waiting for a response, then you don't need to use that data returned by the post call.
but the dialog box is created inside the jquery. And I cannot write php inside it (cant write jquery inside php)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.