为什么mysql请求的结果没有出现在弹出窗口中?

时间:2018-07-20 09:42:33

标签: javascript php html mysql add-on

我正在发现网络扩展。从一些示例中,我尝试在弹出窗口中显示mysql查询的结果,但这是出现在弹出窗口中的php的内容。如果我执行php,我的回答很好,如果执行html,我的回答很好。你能告诉我我是否做错了或者有什么遗漏吗?

manifest.json

 {   
   "manifest_version": 2,

   "name": "Click to execute",

   "description": "test extension",
   "version": "1.0",

   "icons": {
   "48": "icon.png"   },

   "permissions": [
       "webRequest",
       "webNavigation",
       "tabs", "<all_urls>"   ],

   "browser_action": {
       "default_icon": "icon.png",
       "default_popup": "popup.html"   }

}

popup.html

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="popup.js"></script>
</head>
<meta charset=utf-8 />
  <body style="width: 300px">
    <div id="clickme"></div>
  </body>
</html>

popup.js

var xhr;

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 8 and older
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
var data = "";
xhr.open("GET", "select-post.php", true); 
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.overrideMimeType("text/plain");
xhr.send(null);
xhr.onreadystatechange = display_data;
function display_data() {
if (xhr.readyState == 4) {
  if (xhr.status == 200) {
    //alert(xhr.responseText);
    console.log("test1");
    console.log(xhr.responseText);
    console.log("test2");
    document.getElementById("clickme").innerHTML = xhr.responseText;
   } else {
        alert('There was a problem with the request.');
   }
 }
}

select-post.php

<?php

$host="localhost"; 
$username="****";  
$password="**********";
$db_name="********"; 

$con=mysql_connect("$host", "$username", "$password");
mysql_select_db("$db_name");

$sql = "select count(*) as count
from ******_posts p
inner join ******_postmeta m on m.post_id = p.ID
inner join ******_term_relationships r on r.object_id = p.ID
inner join ******_terms t on t.term_id = r.term_taxonomy_id
where m.meta_key = '_wpc_expires'
and date_format(from_unixtime(CONVERT(meta_value, SIGNED INTEGER)),'%Y-%m-%d') > current_date
and t.slug = '*************'";

$result = mysql_query($sql);

while($row=mysql_fetch_array($result))
{
echo "<p>".$row['count']."</p>";
}
?>

0 个答案:

没有答案