簡體   English   中英

如何從.php檢索數據並顯示在 <select>在Cordova混合應用程序中?

[英]How to retrieve data from .php and display in <select> within Cordova hybrid app?

我正在使用Visual Studio和Cordova編寫混合應用程序,並嘗試從www.a.com/b.php提取數據

我的b.php代碼是:

<?php
// Connect to database server
mysql_connect("http://www.yo.com", "ya", "ye") or die (mysql_error());

// Select database
mysql_select_db("oh") or die(mysql_error());

// SQL query
$strSQL = "SELECT * FROM Properties ORDER BY number DESC";

// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);

// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array


echo '<select name="Address" id="address_search" style="width:282px; display:block;" required>';
while($row = mysql_fetch_array($rs)) 
{
   // Write the value of the full address including unit code, address, city, state, zipcode (which is now in the array $row)
   echo '<option value="'. $row['number'] . ", " . $row['address'] . ", " . $row['city'] . ", " . $row['state'] . ", " . $row['zipcode'] .'">' 
    . $row['number'] . ", " . $row['address'] . ", " . $row['city'] . ", " . $row['state'] . ", " . $row['zipcode'] . 
    '</option>';
}
echo '</select>';


// Close the database connection
mysql_close();?>

我已經直接在php代碼中添加了選擇標記表單,但是我不知道如何在.html中顯示整個選擇框(其中的選項正在檢索數據)。 有幫助或教程嗎? 謝謝。

我已經解決了這樣的問題:

首先,在服務器端代碼(在本例中為php)的“ file.php”中,我有一個包含數據庫元素的數組,並且執行以下操作:

$arrayElements = json_encode($arrayElements );
echo $_GET['jsoncallback'] . '(' . $arrayElements . ');';

之后,在應用js代碼中,我使用jQuery方法$ .getJSON()來獲取我們之前准備的php數組。 當函數獲得服務器答案時,然后在其中執行代碼。 請注意,變量“ respuestaServer”是您從php文件發送的數組,因此您可以使用循環將其拋出,並將其值選擇給您(如果您需要將變量傳遞到php文件並通過GET接收它們,在{}中添加js變量,在此示例中,我發送了變量datosUsuario,在php中,我收到了$ _GET ['usuario'])。

var archivoValidacion = "http://example.com/file.php?jsoncallback=?";
var select = document.getElementById("idSelect");
$.getJSON( archivoValidacion, { usuario:datosUsuario ,password:datosPassword})
.done(function(respuestaServer) {
    for(var i = 0; i < respuestaServer.length;i++){          
        var option = document.createElement("option");
        var textNode = document.createTextNode(respuestaServer[i]);
        option.appendChild(textNode);
        select.appendChild(option);
    }

})

希望對您有所幫助。 如果您有任何疑問,請發給我@ulisesveraes;)

不清楚如何調用此代碼

我想你用jQuery ajax函數做到這一點

所以你的代碼會喜歡這樣的

$('box-selector').load('b.php');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM