我正在开发一个使用钛的应用程序。
这里值已成功插入。但我没有从网络服务代码中收到成功消息。
我使用以下代码插入数据库:
钛合金侧码:
function StaffRegistration(){
if($.staff_firstname.value != "" && $.staff_firstname.value != null){
var request = Ti.Network.createHTTPClient({
onload:alert(this.responseText),
onerror: function(e){
Ti.API.debug(e.error);
alert(this.responseText);
},
timeout:1000,
});
request.open("POST","xxx/xxx.php");
var params = ({"staff_firstname": $.staff_firstname.value,"staff_email": $.staff_email.value,"staff_password": $.staff_password.value,});
request.send(params);
}
else{
alert("Please enter the firstname");
}
Ti.API.info("Result for registration = " + this.responseText);
};
我使用了以下 php(网络服务代码):
<?php
$request = base64_decode($_POST['jsondata']);
$data = json_decode($request,true);
$staff_firstname = $data['staff_firstname'];
$staff_email = $data['staff_email'];
$staff_password = md5($data['staff_password']);
include "db_connect.php";
$db = new DB_CONNECT();
$result = mysql_query("SELECT staff_email,staff_firstname from at_staff WHERE staff_email = '$staff_email'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
while($queryresult=mysql_fetch_array($result)) {
$uname[]=$queryresult['staff_firstname'];
$uemail[]=$queryresult['staff_email'];
}
if(in_array($staff_firstname,$uname) and in_array($staff_email,$uemail)) {
$response='{"Error":"1","Message":"Username and Email already exist"}';
echo $response;
} else if (in_array($staff_firstname,$uname)) {
$response='{"Error":"1","Message":"Username already exist"}';
echo $response;
} else {
$response='{"Error":"1","Message":"Email already exist"}';
echo $response;
}
} else {
$response='{"Error":"1","Message":"Successfully Registered"}';
echo $response;
$data=array("staff_firstname"=>"'".$staff_firstname."'",
"staff_email"=>"'".$staff_email."'",
"staff_password"=>"'".$staff_password."'"
);
echo $response;
}
?>
我如何从这个网络服务网址获得钛的 $response。
最佳答案
@user2218667
Ti.API.info("Result for registration = " + this.responseText);
永远不会像您在第一段代码中显示的那样工作。
为什么?因为你发送一个请求大约需要 1 秒(例如),显然,你的程序不会在
之后等待 1 秒request.send(params);
我将继续程序,当请求返回结果时,它将进入
onload(e) :
只有您才能在这里获得 $结果。
这样可以吗?现在好了,如果 this.responseData 不起作用,我没有解决方案。您可以检查一下您的行:“} else {”,我想代码中上面有一个 if 吗?你确定 $result 定义在上面吗?
您能否在没有钛的情况下使用基本的 html 表单尝试相同的请求,以确保在这种情况下 $result 正确写入,就像这样,我们将知道问题是来自 php 还是来自 php 和钛之间的链接。
关于php - 从 mysql+php 获取结果并以钛显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22812646/