Here is my code of Qt creator to send the data to PHP server and get the response.
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkAccessManager::connect(manager, SIGNAL(finished(QNetworkReply*)),this , SLOT(replyFinished(QNetworkReply*)));
QNetworkRequest *request = new QNetworkRequest(QUrl("http://68.169.55.41/fss/verifylogindetails.php"));
QByteArray postData =" {\"estate_id\":\"hsr\",\"emp_pin\":1234,\"emp_id\":\"santhosh\"}";
request->setRawHeader( "User-Agent" , "Mozilla Firefox" );
request->setRawHeader( "charset", "utf-8" );
request->setRawHeader( "Connection", "keep-alive" );
manager->post(*request, postData );
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
QString data = reply->readAll().trimmed();
qDebug() << data;
}
And here is my php code to receive the login data and comparing with database values and sending the response.
<?php
include 'config.php';
$data = json_decode($_SERVER['HTTP_JSON']);
$con = mysql_connect($host, $user_name, $password);
if (!$con)
{
die('Could not connect: '.mysql_error());
}
mysql_select_db($db_name, $con);
$result = mysql_query("SELECT * FROM login_data_table");
while($row = mysql_fetch_array($result))
{
if((strcasecmp( $row['Emp_Id'], $data->emp_id) == 0) && ($row['Emp_Pin'] == $data- >emp_pin) && (strcasecmp( $row['Estate_Id'], $data->estate_id) == 0))
{
echo "SUCCESS";
}
else
{
echo "FAIL";
}
}
mysql_close($con);
?>
My problem is..... Always i am getting FAIL message from server. Even i am sending correct login details. please help me where i am wrong in my code.
I will post a piece of code from my own program. Hopefully this will help:
void MainWindow::sendPost(url)
{
QUrl params;
params.addQueryItem("usrname", login);
params.addQueryItem("passwrd", password);
QByteArray data;
data.append(params.toString());
data.remove(0,1);
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkReply *reply = manager->post(QNetworkRequest(url), data);
CONNECT(reply, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
QString data = reply->readAll().trimmed();
qDebug() << data;
}
PHP code will get values from program using $_POST array ($_POST['usrname'] and $_POST['password'] hold both values sent from the program), not $_SERVER. I have no idea why did you use $_SERVER.
来源:https://stackoverflow.com/questions/8076745/how-can-i-post-the-data-in-qtcreator