简体   繁体   English

从javascript获取php函数值

[英]Get php function value from javascript

Allow me to preface this by saying that I did look over other answers, but I am still uncertain how to execute it. 首先,请允许我说我确实查看了其他答案,但仍然不确定如何执行它。

Here is my problem: 这是我的问题:

I have a .txt file from which I am reading and parsing the meaning of a name (I know, DB is better, but this is what I was given). 我有一个.txt文件,可以从中读取和解析名称的含义(我知道DB更好,但这就是我给的名称)。 I wish to fetch the return value of my php function into my javascript function. 我希望将我的php函数的返回值提取到我的javascript函数中。 How would I go about doing this? 我将如何去做呢?

PHP Code: PHP代码:

function populateMeaning() {
    $meanings = fopen("meanings.txt", "r") or die("Unable to open file");
    $meaning = "";

    $selectOption = $_POST['allnames'];

    while(!feof($meanings)) {
        $selectOption = strtoupper($selectOption);
        $line = fgets($meanings);

        if(strpos($line,$selectOption ) !== false) {
            $meaning = substr($line, strpos(0, ' '), strlen($line));
        }
    }

    return $meaning;
}

Javascript function in question: 有问题的JavaScript函数:

function meaning(){

    document.getElementById("meaning").innerHTML = "hello";
}

So I have a div element titled "meaning" and I would like to replace it's inner html value. 因此,我有一个名为“含义”的div元素,我想替换它的内部html值。 The string "hello" is currently simply a placeholder to make sure that the JS is called correctly. 字符串“ hello”当前只是一个占位符,以确保正确调用JS。

jQuery Ajax简而言之,它是从前端(Javascript)到后端(PHP)值http://www.w3schools.com/jquery/ajax_ajax.asp或Google jQuery AJAX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM