简体   繁体   English

从javascript调用php并从php返回值到javascript

[英]Calling php from javascript and return a value from php to javascript

I send a variable from js to php and in php i am running a query using the variable i got from js. 我将变量从js发送到php,在php中,我使用从js获得的变量运行查询。 After running the query i need to return the output of the query to js. 运行查询后,我需要将查询的输出返回给js。 I succeeded in sendind the variable and running the query.But unable to return the output.How can i return it? 我成功了sendind变量并运行了查询。但是无法返回输出,我该如何返回呢?

Here is the code 这是代码

JS JS

function abc () {
    var url = 'server/set_data.php';
    var paramstring = "vr=ViewLog&val=" + activityID1;
    ctype = "TEXT";
    AJAXRequest(url, paramstring, ctype, "VerifyData");
}

PHP 的PHP

if(trim($_REQUEST[vr]) == 'ViewLog')
{
    if (trim($_REQUEST[val]) != "")
    {
        $sql="select abc from table where id='".$_REQUEST[val]."'";
        echo $sql;    
    }
}

Try this jQuery code, it will work 试试这个jQuery代码,它将起作用

$.ajax({
    type   :"POST",
    data   :"vr=ViewLog&val="+activityID1,
    url    :'server/set_data.php', 
    success: function(msg){
        alert(msg); // here you can get the result from php page
     }
});

Are you sure, that the two conditional statements are executed the right way? 您确定这两个条件语句执行正确吗? I would set the array index in quotes: 我将用引号设置数组索引:

if(trim($_REQUEST["vr"]) == 'ViewLog')
{
  if (trim($_REQUEST["val"]) != "")
  {
    $sql="select abc from table where id='".$_REQUEST[val]."'";
    echo $sql;   
  }
}

Additionally: You only return the SQL request string and not the result of a SQL request. 此外:您只返回SQL请求字符串,而不返回SQL请求的结果。 Is this intended? 这是故意的吗?

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

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