【问题标题】:get the result from mysql+php and shows in titanium从 mysql+php 获取结果并以钛显示
【发布时间】:2014-04-02 13:13:56
【问题描述】:

我正在使用钛开发一个应用程序。

此处的值已成功插入。但我没有从 web 服务代码中获得成功消息。

我使用以下代码插入数据库:

在钛侧码:

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(webservice 代码):

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

      ?>

我怎样才能从这个 web 服务 url 中获得钛中的 $response。

【问题讨论】:

    标签: php mysql iphone titanium titanium-alloy


    【解决方案1】:

    @user2218667

    Ti.API.info("Result for registration = " + this.responseText);
    

    永远不会像您在第一段代码中显示的那样工作。

    为什么?因为您发送的请求大约需要 1 秒(例如),显然,您的程序不会在

    之后等待 1 秒
    request.send(params); 
    

    我将继续程序,当请求返回结果时,它将进入

    onload(e) : 
    

    只有在这里你才能得到你的 $result。

    这样好吗?现在好了,如果 this.responseData 无效,我没有解决方案。你能检查你的行:“} else {”,我想代码中有一个 if 吗?你确定 $result 定义在上面吗?

    你能在没有钛的情况下用基本的 html 表单尝试相同的请求,以确保 $result 在这种情况下正确写入,像这样,我们将知道问题是来自 php 还是来自 php 和 Titan 之间的链接。

    【讨论】:

      【解决方案2】:

      好吧,我假设它是异步请求,所以以下可能无法正常工作

      Ti.API.info("Result for registration = " + this.responseText);
      

      你能不能试试:

              onload : function(e) {
                  Ti.API.info(this.responseText); // maybe Ti.API.info(this.reponsedata) according to your php.
              },
              onerror : function(e) {...
      

      在我看来,如果你收到 JSON 信息(看起来真的很像),你需要

      this.responseData //instead of this.responseText
      

      【讨论】:

      • 您好,我已经在上面添加了行。但是我没有在我的钛上得到 $response。我得到以下 [INFO]:注册结果 = 未定义...但是值已成功插入....所以请给我一个解决方案。我怎样才能得到 $response ?
      猜你喜欢
      • 2013-08-11
      • 2016-07-30
      • 1970-01-01
      • 2013-09-11
      • 2013-11-05
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多