using jquery variable in php

I've got a dynamic table filled by a recordset from MYSQL. Each row has it's own delete button (image) to delete the specific row. This button has a class="button".

I'm using a JQuery popup modal to get a popup when a delete button is clicked. In this JQuery script i'm creating a variable which contains the numeric value of the first td cel of the row that has been clicked on. This all works perfectly.

What i'm trying to accomplish is to use this variable on the same php page. Here is where my knowledge runs out. I've read some examples where Ajax is the solution for this, but i lack the knowledge to use these examples for this solution.

JQuery code:

<script src="../javascript/jquery-1.8.2.js"></script>
<script src="../javascript/jquery.reveal.js"></script>
<script type="text/javascript">
   $(document).ready(function() {
      $('.button').click(function(e) { // Button which will activate our modal
         var value=$(this).closest('tr').children('td:first').text();
         alert(value); // this works
         $('#modal').reveal({ // The item which will be opened with reveal
            animation: 'fade', // fade, fadeAndPop, none
            animationspeed: 500, // how fast animtions are
            closeonbackgroundclick: false, // if you click background will modal close?
            dismissmodalclass: 'close' // the class of a button or element that will close an open modal
         });
      return false;
      });
   });
</script>

I've been trying too much that i don't see the logic anymore. I hope that someone can help me with this.

Answers


The issue is that your JavaScript runs in the client -- the user's web browser -- while your PHP runs on your server. It's a two-stage process: first, all the PHP is executed on the server and the HTML is rendered, which is then sent to the client (the browser). Then, the client executes all the JavaScript on the page.

You need some way to communicate the JS variable (value) to your server if you want to be able to use it in your PHP code. AJAX is one such way, but it would be helpful to have more information on how exactly you want to use this information in your PHP.

Edit: based on your comments above, something like this should work. You'll have to give your Yes button an id attribute (here I'm assuming the id is yesButton).

$(.button).click(function() {
    var value=$(this).closest('tr').children('td:first').text();
    $("#yesButton").attr("href", "delete_verlof.php?id=" + value);
    $('#modal').reveal({ // The item which will be opened with reveal
        animation: 'fade', // fade, fadeAndPop, none
        animationspeed: 500, // how fast animtions are
        closeonbackgroundclick: false, // if you click background will modal close?
        dismissmodalclass: 'close' // the class of a button or element that will close an open modal
    });
    return false;
});

The important thing to note is that the JS variable does not exist yet at the time at which your PHP executes, so it is not available to the PHP. What I've done here instead is to dynamically change the href of the Yes button whenever the user clicks a td, which should have the desired effect.


Need Your Help

How to check if a program is using .NET?

.net process executable

Can we check if a running application or a program uses .Net framework to execute itself?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.