Quote:
$(document).dblclick(function(e) {
var t = get_selection();
document.getElementById("word").innerHTML = t;
});
var var_data = t;
The
var var_data = t;
line executes
before the
dblclick
event handler. It also has no access to the variable
t
defined in the event handler function, so it will default to "undefined".
Change the event handler code so that it updates the global variable:
var var_data = null;
$(document).dblclick(function(e) {
var_data = get_selection();
document.getElementById("word").innerHTML = var_data;
});
$(document).on("click", "#sub", function() {
$.ajax({
url: 'phpPage.php?var_PHP_data=' + encodeURIComponent(var_data),
type: 'GET',
success: function(data) {
$('#result').html(data);
}
});
});
To access the variable from PHP:
<?php
if(isset($_GET["var_PHP_data"])){
$str = $_GET["var_PHP_data"];
$fileName="https://users.cs.cf.ac.uk/KurtevaA/searchfile.txt";
$lines = file($fileName);
foreach ($lines as $lineNumber => $line) {
if (strpos($line, $str) !== false) {
echo $line;
return $line;
}
}
return -1;
}
?>