0

myscript.js

$(".tablestyleorchideeaanbod").on("click", function() {
     var value = $(this).find('tr:first td:first').html();
     lastClickedValue = value;
     console.log( value );
     var test = lastClickedValue
});

So when got clicked on my table with class tablestyleorchideeaanbod, var test will get a value. Let's say for example: hi;

So, var test = hi

Now I need to use this variable in my homepage, index.php. In index.php I want to use the variable; var test, to use in a MYSQL Query.

For example:

SELECT * FROM thisismytable WHERE rowname = var test

How to achieve this?

6
  • You need to use AJAX. Commented May 14, 2015 at 18:54
  • PHP runs on the server. Javascript runs in the client's browser. They cannot communicate directly, you need to use AJAX or post back the data in a basic form. Commented May 14, 2015 at 18:55
  • you say you need to use their selection on the homepage, is this table on the homepage? It could be as simple as bringing them to index.php?test=value with a link Commented May 14, 2015 at 18:57
  • It has to be done without pagerefresh, can someone explain me how to do this with AJAX? Commented May 14, 2015 at 19:00
  • Click the link in my comment above - it explains everything. Commented May 14, 2015 at 19:01

1 Answer 1

1
$(".tablestyleorchideeaanbod").on("click", function() {
     var value = $(this).find('tr:first td:first').html();
     lastClickedValue = value;
     console.log( value );
     var test = lastClickedValue
});

Then you most use ajax to send the data to de server

   $(".tablestyleorchideeaanbod").on("click", function() {
      var value = $(this).find('tr:first td:first').html();
      lastClickedValue = value;
      console.log( value );
      var test = lastClickedValue;
      $.ajax({
            url: 'data.php',
            type: "POST",
            /*contentType: "application/json; charset=utf-8",*/
            data: {val : test },
            dataType: 'json',
            success: function (data) {
                console.log(data);

            }
        });
});

php script data.php

<?php
$val = $_POST['val'];
// do whatever you want here .....
//insert sql or select
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi can't get it to work so far, can u may demonstrate in demo or jsfiddle? I'd like to show my code but this can't be done in a comment
this is the link for jsfiddle link. @KevinAmmerlaan if you click on any td of the table you will see the data on a div call result

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.