Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中访问ajax变量_Php_Javascript_Html_Ajax - Fatal编程技术网

如何在php中访问ajax变量

如何在php中访问ajax变量,php,javascript,html,ajax,Php,Javascript,Html,Ajax,我试图将ajax变量传递到php页面 我在这里警告这个值,它显示了这个值。但当我将值发送到php页面时,它并没有获取值并执行删除操作 这是我尝试过的 ajax代码: function remove() { if(a=="") { document.getElementById("txtHint").innerHTML=""; return; } if(window.XMLHttpRequest) {

我试图将ajax变量传递到php页面

我在这里警告这个值,它显示了这个值。但当我将值发送到php页面时,它并没有获取值并执行删除操作

这是我尝试过的

ajax代码:

function remove() 
{
    if(a=="") 
    {
        document.getElementById("txtHint").innerHTML="";
        return;
    }

    if(window.XMLHttpRequest) 
    {
        xmlhttp=new XMLHttpRequest();
    } 
    else 
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() 
    {
       if(xmlhttp.readyState==4 && xmlhttp.status==200) 
       {
           document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
       }
    }

    xmlhttp.open("GET","theaterdel.php?q="+a,true);
    alert(a)
    xmlhttp.send();
}
<?php
$q = strtolower(trim($_GET["q"]));
try 
{
   $dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} 
catch (PDOException $e) 
{
   echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'DELETE  FROM theater WHERE LOWER(address) = :q';
$sth = $dbh->prepare($sql);
$sth->bindValue(':q', $q);
$sth->execute();
$dbh = null;
?>
php代码:

function remove() 
{
    if(a=="") 
    {
        document.getElementById("txtHint").innerHTML="";
        return;
    }

    if(window.XMLHttpRequest) 
    {
        xmlhttp=new XMLHttpRequest();
    } 
    else 
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() 
    {
       if(xmlhttp.readyState==4 && xmlhttp.status==200) 
       {
           document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
       }
    }

    xmlhttp.open("GET","theaterdel.php?q="+a,true);
    alert(a)
    xmlhttp.send();
}
<?php
$q = strtolower(trim($_GET["q"]));
try 
{
   $dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} 
catch (PDOException $e) 
{
   echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'DELETE  FROM theater WHERE LOWER(address) = :q';
$sth = $dbh->prepare($sql);
$sth->bindValue(':q', $q);
$sth->execute();
$dbh = null;
?>


如何解决这个问题?

您正在使用AJAX发送HTTP GET请求,并使用JavaScript中生成的值将“q”作为参数发送

这与有人执行此url http://:/my app?q=值%20相同

因此,当这个请求到达PHP时,您应该能够像往常一样处理Get请求和Get参数,没有区别。另外,在通过AJAX从JavaScript发送值时,请尝试对URI值进行编码,以便在URL中正确地转义它们

做一个
var\u转储($\u GET[“q”])。
无论它产生什么,它都应该出现在
#txtHint
元素中(至少应该得到
null
)。
如果你没有得到任何东西,那么你可能是把PHP文件的路径弄错了,所以试着使用绝对URL看看它是否有效。

你有没有
echo$\u get[“q”]
响应中有什么?(查看开发者工具)如果将php代码替换为@Shadowfax echo语句,会有什么反应。@Shadowfax是的,我尝试使用echo$\u GET[“q”],但没有显示结果