on my webpage, when the user clicks on a word it should show the definition of the word. When the user clicks on the word, it would send the word variable to the php file that connects to the oxford dictionary api and queries the word for its definition, then returns that definition to the webpage. The problem is when I tried to pass the word variable to the php file through ajax, it doesn't seem to be working. I am using a php wrapper to use the api and there's no problem when I set the word query manually. I am able to connect to the api and retrieve the definitions and all, so I think the problem is the ajax part. I've never used jquery, ajax or php before this assignment. I'm not sure what I did wrong. Please help!
My JS file
function getSelectedText(){
var selectedText = '';
if (window.getSelection)
selectedText = window.getSelection();
return selectedText;
}
// Retrieve definition
$(document).ready(function()
{
var selected_text = getSelectedText();
$('#selectable').on("dblclick", function () {
$('.selection').text(selected_text);
$('.is-selected').text(getSelectedText() !== '');
});
$.ajax({
url: "dictionary.php", // php file path
method: "POST", // send data method
data: {"selected_text": selected_text}, // data to send {name: value}
success: function(data){
alert(data);
} // response of ajax
});
});
My PHP file
$selected_text = $_POST['selected_text'];
echo $selected_text;
$dictionary->queryWord($selected_text);
$dictionary->setResult(0);
/* Get results from dictionary class */
echo "<h1>Dictionary Class Results - ".$dictionary->getWord()."</h1>";
echo "<b>Word:</b> ".$dictionary->getWord();
echo "<br><b>Definition:</b> ".$dictionary->getDefinition();
echo "<br><b>Short Definition:</b> ".$dictionary->getShortDefinition();
echo "<br><b>Example:</b> ".$dictionary->getExample();
/* Displays the current result set */
echo "<br></br>Using result set: <b>".$dictionary->selected_result."</b>";
?>
selected_text.