$_POST variable doesn't work in php Titanium

北城以北 提交于 2019-12-12 02:33:05

问题


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";
?>

回答1:


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




回答2:


I have the solution now! If we see this part of 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);};

});

the codw between ** is the problem... so i do like this:

var url = "http://10.0.2.2/jobfinder/tessssssste/Resources/teste.php";//bug do titanium resolvido 
    var params = "?params=" + input.value;
    var encodedURI = encodeURI(url + params);

    var xhr = Titanium.Network.createHTTPClient();
    xhr.open("GET", encodedURI);
    xhr.send();

    xhr.onload = function(){

and now it works TY EVERYBODY!!!!



来源:https://stackoverflow.com/questions/10171202/post-variable-doesnt-work-in-php-titanium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!