JS嵌入在一个名为(check.php)的php页面中,如下所示
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" charset="utf-8">
function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection &&
document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}
/* creating sniffer */
$(document).ready(function() {
$('#content-area').mouseup(function() {
var selection = getSelected();
if(selection &&
(selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
alert('Sending the following text to the server via AJAX: '
+ selection);
$.ajax({
type: "POST",
url : "calculate.php",
data: {selection: encodeURI(selection)},
success: function(data, status){
console.log(status);
},
});
}
});
});
</script>
在calculate.php,我正在做以下事情。
if(isset($_POST['selection']) && !empty($_POST['selection'])){
$selection = trim($_POST['selection']);
}else{
$selection = "no selection text received";
}
我正在使用JS选择一个文本并将其发送到另一个php脚本,然后将其插入到DB中。我在选择文本时收到alert
,但$selection
中的calculate.php
变量从else循环获取值。我想访问我目前无法访问的POSTed值。请帮助我,我是这些事情的完全初学者