Javascript 如何从.php检索数据并显示在<;选择>;在Cordova混合应用程序中?
我正在使用Visual Studio和Cordova exetnstion编写一个混合应用程序,并试图从www.a.com/b.php中提取数据 我的b.php代码是:Javascript 如何从.php检索数据并显示在<;选择>;在Cordova混合应用程序中?,javascript,php,jquery,html,cordova,Javascript,Php,Jquery,Html,Cordova,我正在使用Visual Studio和Cordova exetnstion编写一个混合应用程序,并试图从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_
<?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代码中直接添加了select标记表单,但我不知道如何在.html中显示整个select框(其中的选项是检索到的数据)。
有什么帮助或指导吗?谢谢。不清楚如何称呼此代码 我想您可以使用jqueryajax函数来实现这一点 所以你的代码会像这样
$('box-selector').load('b.php');
我已经这样解决了这个问题: 首先,在服务器端代码(本例中为php)中的“file.php”中,我有一个包含数据库元素的数组,并执行以下操作:
$arrayElements = json_encode($arrayElements );
echo $_GET['jsoncallback'] . '(' . $arrayElements . ');';
之后,在appjs代码中,我使用jQuery方法$.getJSON()获取我们之前准备的php数组。当函数得到服务器的答案时,执行里面的代码。请注意,变量“respuestaServer”是您从php文件发送的数组,因此您可以通过循环将其抛出,并将其值带到您的select(如果您需要将变量传递到php文件并通过GET接收它们,只需在{}中添加js变量,在本例中,我发送变量Datosuario,在php中我接收它$\u GET)['usuario'])
var archivoValidacion=”http://example.com/file.php?jsoncallback=?";
var select=document.getElementById(“idSelect”);
$.getJSON(archivoValidacion,{usuario:datosuario,密码:datopassword})
.done(函数(respuestaServer){
对于(var i=0;i
})
我希望这能对你有所帮助。如果你有什么问题,请发推特给我@ulisesveraes;)
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);
}