$ _ POST变量没有在PHP工作钛 [英] $_POST variable doesn't work in php Titanium
本文介绍了$ _ POST变量没有在PHP工作钛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一个JS文件和PHP文件之间传递数据,但是在PHP变量 $ _ POST
doesn't工作,为我不能发展中的应用程序!
JS code:
VAR PARAMS =字符串(input.value);
VAR XHR = Titanium.Network.createHTTPClient();
xhr.open(GET,HTTP://10.0.2.2/jobfinder/teste_demo_grafica/Resources/teste.php');
xhr.send(PARAMS);
xhr.onload =功能(){ VAR响应= this.responseText;
警报(响应);
如果(响应!= NULL)
{
警报(voltou AO JSëFuncA的行);
}
其他
{
警报(-。-);
}
}; xhr.setRequestHeader(内容类型,应用程序/ x-WWW的形式urlen codeD); xhr.onerror =功能(E){警报('传输错误:'+ e.error);};});
和现在的PHP code:
< PHP//连接到数据库中(主机,用户名,密码)
$ CON = mysql_connect('localhost'的,'根','');如果(!$ CON)
{
返回(无法建立连接。);
出口;
}//选择数据库。输入您的数据库的名称(不一样的表名)
$分贝= mysql_select_db('jobfinder');
如果(!$ DB)
{
回声无法选择数据库。
出口;
}
$ pesquisa = $ _ POST [PARAMS]
回声pesquisa
$ SQL =SELECT * FROM oferta WHERE TITULO LIKE'%$ pesquisa%';
$查询=的mysql_query($的SQL);如果(mysql_num_rows($查询)大于0)
{ $行= mysql_fetch_array($查询);
$响应=阵列(
TITULO'=> $行['TITULO'],
oferta'=> $行['descricao_oferta']
);
json_en code($响应);
回声$回应['TITULO'];}
其他
{ //否则用户名和/或密码无效!创建一个数组,json_en code将其和echo出来
$响应=阵列(
'消息'= GT; NAO existem ofertas对ESTA pesquisa
);
json_en code($响应);
}
回声PHP模式关;
?>
解决方案
在猜测,你的JavaScript是通过GET发送数据:
xhr.open(GET,HTTP://10.0.2.2/jobfinder/teste_demo_grafica/Resources/teste.php');
但你的PHP正在寻找POST。改变POST GET和它可能工作。反之亦然
I'm trying to pass data between a JS file and a PHP file, but the variable $_POST
in PHP doesn´t work and for that i can´t evolve in the app!
JS code:
var params = String(input.value);
var xhr = Titanium.Network.createHTTPClient();
xhr.open('GET','http://10.0.2.2/jobfinder/teste_demo_grafica/Resources/teste.php');
xhr.send(params);
xhr.onload = function(){
var response = this.responseText;
alert(response);
if (response != null)
{
alert("voltou ao js e funca");
}
else
{
alert("-.-");
}
};
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.onerror = function(e){alert('Transmission error: ' + e.error);};
});
and now the php code:
<?php
// Connect to the database(host, username, password)
$con = mysql_connect('localhost','root','');
if (!$con)
{
return ("Failed to make connection.");
exit;
}
// Select the database. Enter the name of your database (not the same as the table name)
$db = mysql_select_db('jobfinder');
if (!$db)
{
echo "Failed to select db.";
exit;
}
$pesquisa= $_POST[params];
echo "pesquisa";
$sql = "SELECT * FROM oferta WHERE titulo like '%$pesquisa%'";
$query = mysql_query($sql);
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_array($query);
$response = array(
'titulo' => $row['titulo'],
'oferta' => $row['descricao_oferta']
);
json_encode($response);
echo $response['titulo'];
}
else
{
// Else the username and/or password was invalid! Create an array, json_encode it and echo it out
$response = array(
'message' => 'Não existem ofertas para esta pesquisa'
);
json_encode($response);
}
echo "php mode off";
?>
解决方案
At a guess, your JavaScript is sending data via GET:
xhr.open('GET','http://10.0.2.2/jobfinder/teste_demo_grafica/Resources/teste.php');
but your PHP is looking for POST. Change the POST to GET and it may work. Or vice versa
这篇关于$ _ POST变量没有在PHP工作钛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文