【问题标题】:Passing variable to remote php file for JSON data将变量传递给远程 php 文件以获取 JSON 数据
【发布时间】:2013-03-25 15:23:05
【问题描述】:

我正在使用 Titanium Studio 构建一个移动应用程序,构建:3.1.1.201303242433,CLI 版本 3.0.24,Titanium SDK 版本 3.0.2.GA,iOS SDK:6.1,iPad 模拟器:6.1 在 MAC OS X10.7.5 上。 我正在尝试从远程数据库中检索 JSON 数据。数据库有带和不带“sectionid”的记录(sectionid 是数字 1-12)。我想用传递给我的查询的“sectionid”检索记录。以下是我要遵循的步骤:首先,我将“sectionid”分配给 tableViewRow,如下所示:

leftImage:'images/advertising.png', 
            title:'Advertising',
            sectionid: '1', 
            color:'#000',
            font: {fontWeight:'bold', fontSize:16},
            height:'44dp', 
            hasChild:true, 
            test:'testcatmenu.js'},

然后我将 sectionid 传递给另一个文件,如下所示:

var section = e.rowData.sectionid;
win.section = section;

在下一个文件中,我获取 sectionid 并将其传递给远程 php 文件,如下所示:

var section = Ti.UI.currentWindow.section;
var url = "http://mydomain.com/myfile.php?sectionid='section'";

最后,我在 php 文件中 $_GET 获取 sectionid 并在我的 MySQL 查询中使用它,如下所示:

$result = mysql_query("SELECT name FROM `cms_client` WHERE sectionid = '" .     mysql_real_escape_string($_GET['section']) ."'") or die('Could not query');

php 文件正在返回没有 sectionid 的记录,所以我在某处丢失了 sectionid?我不知道我的代码语法哪里错了,有人能告诉我正确的语法吗?

【问题讨论】:

  • 也许在 SELECT 中包含 sectionidSELECT name, sectionid FROM `cms_client`
  • 感谢回帖,我添加了但没有效果。

标签: php javascript database titanium


【解决方案1】:

在 php 中,您使用的是变量 $_GET['section'],但您传入的 GET 变量称为 sectionid (?sectionid=)。所以要么都改成section,要么都改成sectionid。

另外,假设

var url = "http://mydomain.com/myfile.php?sectionid='section'";

是 javascript 那么它应该是这样的:

var url = "http://mydomain.com/myfile.php?sectionid="+section;

【讨论】:

    【解决方案2】:
    var url = "http://mydomain.com/myfile.php?sectionid='section'";
    

    改成

    var url = "http://mydomain.com/myfile.php?sectionid="+section;
    

    Javascript 允许您使用 + variableName 将变量添加到字符串中

    喜欢

    var variableT = "foobar";
    var strNew = "This string contains the word: "+variableT+" and it works!";
    

    【讨论】:

    • 感谢您的回帖,我更改了我的网址,还特别要求提供上面推荐的“SSH This”的sectionid,但没有更改。我打开浏览器并尝试强制使用mydomain.com/myfile.php?sectionid=3 之类的部分ID,但文件仍然只返回没有sectionid 的记录。 php 输出的摘录是:{"name":"C J McCall Mfr","sectionid":""}。所以也许我的问题出在 PHP 文件中?
    • $_GET['section'] 到 $_GET['sectionid'] 因为您创建了一个名为 sectionid 的 GET 属性,而不是 section
    猜你喜欢
    • 2018-03-05
    • 2019-03-11
    • 1970-01-01
    • 2023-04-03
    • 2015-11-16
    • 2018-08-07
    • 2020-03-21
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多