0

I know this sound weird but I need to be able to use a variable in javascript and use it inside a php mysqli query

I use the jQuery File Upload from blueImp. It's variable are store like this

{%=file.name%}

and I need to do something like this

$iq = $mysqli->query("SELECT * FROM image WHERE mId='".$_GET['mId']."' AND file_name = '".{%=file.name%}."'");

of course this is not working because of the {}.

Anyone have a clue how to work with those kind of programming ?

4

2 回答 2

2

在 JavaScript 中:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
        //POST request sent
    }
};

xmlhttp.open("POST", "url_to_phpfile.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("yourvariable="+yourvariable);

然后你可以在你的 php 文件中使用该变量,如下所示:

$iq = $mysqli->query("SELECT * FROM image WHERE mId='".$_GET['mId']."' AND file_name = '".$_POST["yourvariable"]."'");
于 2013-06-10T18:32:14.333 回答
0

,您不能直接这样做**。

  • Javascript 是在客户端(浏览器)执行的东西。
  • PHP 是一种服务器端脚本语言。

如果你想传递一个 Javascript 变量PHP,那么你可以通过AJAX

于 2013-06-10T18:13:04.133 回答