[英]How do I get javascript to read the output of a php file
First off hello, I am new here. 首先,你好,我是新来的。
My problem is that I have a php file pulling info from a database. 我的问题是我有一个php文件从数据库中提取信息。 I will post the code below.
我将在下面发布代码。 What I need is for my JavaScript to take the output and load it into a list that generates some flash cards.
我需要的是我的JavaScript接收输出并将其加载到生成一些闪存卡的列表中。
code sample `$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 代码示例`$ connection = mysql_connect($ host,$ user,$ pass)或死(“无法连接!”);
// select database
mysql_select_db($db) or die ("Unable to select database!");
$query1 = "SELECT * FROM category_tb WHERE cat_name = '$category'"; $ query1 =“选择*从category_tb,其中cat_name ='$ category'”; $result1 = mysql_query($query1) or die ("Error in query: $query1. " . mysql_error());
$ result1 = mysql_query($ query1)或死(“查询错误:$ query1。”。mysql_error()); while ($row = mysql_fetch_array($result1)) {
while($ row = mysql_fetch_array($ result1)){
$cat_num = $row[1];
}
// This establishes a link to MySQL $query = "SELECT * FROM english_lang, finnish_lang ". //这将建立指向MySQL $ query =“ SELECT * FROM english_lang,finnish_lang”的链接。 "WHERE english_lang.lang_id = finnish_lang.lang_id AND english_lang.cat_id = $cat_num";
“在哪里english_lang.lang_id = finish_lang.lang_id和english_lang.cat_id = $ cat_num”; $rt = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$ rt = mysql_query($ query)或死(“查询错误:$ query。”。mysql_error());
while($nt=mysql_fetch_array($rt)){ echo "{\\"english\\": \\"$nt[1]\\", \\"finnish\\": \\"$nt[6]\\" , \\"asked\\": states.notAsked},"; while($ nt = mysql_fetch_array($ rt)){echo“ {\\” english \\“:\\” $ nt [1] \\“,\\” finish \\“:\\” $ nt [6] \\“,\\”询问\\“:states.notAsked},”;
} ` }`
So this basicly gets some data and formats it to be used by the javascript. 因此,这基本上会获取一些数据并将其格式化以供javascript使用。 if you want to look at the output of this to get a better idea the go here
如果您想查看此输出以更好地了解,请转到此处
http://languagelearner.byethost2.com/vocabulary2.php http://languagelearner.byethost2.com/vocabulary2.php
select 1 of the first 2 categories as they are the only ones with data right 请从前2个类别中选择1个,因为它们是唯一具有数据权限的类别
now. 现在。
the javascript is this: JavaScript是这样的:
code sample ` 代码示例
var string1;
var string2;
var number;
var states = {"oneVisible": 0, "bothVisible": 1, "notAsked": 2, "asked": 3}
var state = states.bothVisible;
var numberOfWordsAsked = 0;
var words = {"list": [
]
}
function displayWords(){
if (state == states.bothVisible) {
if (numberOfWordsAsked < words.list.length) {
state = states.oneVisible;
number = Math.floor(Math.random() * words.list.length);
while (words.list[number].asked == states.asked) {
number = Math.floor(Math.random() * words.list.length);
}
string1 = words.list[number].english;
string2 = words.list[number].finnish;
document.getElementById("fin").style.display = 'none';
document.getElementById("eng").innerHTML = words.list[number].english;
document.getElementById("fin").innerHTML = words.list[number].finnish;
document.getElementById("b").value = "Show word";
document.getElementById("correct").style.display = 'none';
}
else {
document.getElementById("eng").innerHTML = "You know all the words in this category, congratulations!";
document.getElementById("fin").style.display = 'none';
document.getElementById("b").style.display = 'none';
document.getElementById("correct").style.display = 'none';
}
}
else {
document.getElementById("fin").style.display = 'inline';
state = states.bothVisible;
document.getElementById("b").value = "Wrong";
document.getElementById("correct").style.display = 'inline';
}
}
function setCorrect(){
words.list[number].asked = states.asked;
numberOfWordsAsked += 1;
displayWords();
}
//-->
</script>
` `
so the output needs to go in here. 所以输出需要进入这里。
var words = {"list": [ var words = {“ list”:[
] ]
Any help would be appreciated. 任何帮助,将不胜感激。 I did not write the javascript, a friend did.
我没有写JavaScript,朋友写了。
He used static info in the list. 他在列表中使用了静态信息。
Try AJAX. 试试AJAX。 Check out http://www.w3schools.com/PHP/php_ajax_database.asp
查看http://www.w3schools.com/PHP/php_ajax_database.asp
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4) {
alert(ajax.responseText);
}
};
ajax.open("GET", "ajax.php", true);
ajax.send(null);
outputs "hello world" when used in the same directory as a php file ajax.php: 在与php文件ajax.php相同的目录中使用时,输出“ hello world”:
<php
echo 'hello Word!';
?>
To put php data structures into something javascript can parse, use json_encode. 要将php数据结构放入javascript可解析的内容中,请使用json_encode。 That should be enough to help you on your way.
那应该足以帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.