在哪里放置php&;构建Phonegap应用程序时的html文件以及如何为它们调用ajax
我正在尝试使用PhoneGap为现有的PHP web应用程序构建Android应用程序。我有用于逻辑的PHP文件和用于呈现UI的HTML文件。这些文件放在我的系统的Webserver(Wampserver)中。现在我看到了一些源代码,它们说->从phonegap项目对Web服务器中的PHP文件进行Ajax调用。在此基础上,我创建了一个index.html文件,其中包含对目标txt.php文件的Ajax调用,并尝试在Android虚拟设备管理器上运行。我看到index.html被加载到AVD上,我在textarea中给出了一些输入,并单击了“保存消息”按钮。但我看不到AVD有任何反应。谁能告诉我如何放置这些HTML文件、php文件以及如何进行ajax调用。。我希望这个问题能帮助很多像我这样的初学者 到目前为止,以下代码命名为index.html,并放置在Phonegap项目的assets/www/index.html中。我正在使用加载index.html页面在哪里放置php&;构建Phonegap应用程序时的html文件以及如何为它们调用ajax,php,cordova,Php,Cordova,我正在尝试使用PhoneGap为现有的PHP web应用程序构建Android应用程序。我有用于逻辑的PHP文件和用于呈现UI的HTML文件。这些文件放在我的系统的Webserver(Wampserver)中。现在我看到了一些源代码,它们说->从phonegap项目对Web服务器中的PHP文件进行Ajax调用。在此基础上,我创建了一个index.html文件,其中包含对目标txt.php文件的Ajax调用,并尝试在Android虚拟设备管理器上运行。我看到index.html被加载到AVD上,我
super.loadUrl("file:///android_asset/www/index.html");
Index.html页面如下所示
<html>
<head>
<script language=javascript>
function chk_length(myform)
{
maxLen=767;
if(myform.txt.value.length>=maxLen)
{
alert('You Reached max Length in text area');
myform.txt.value=myform.txt.value.substring(0,maxLen);
}
else
{
myform.num.value=maxLen-myform.txt.value.length;
}
}
</script>
<script>
function myCall() {
$.ajax({
url: "localhost:8080/yours/txt.php",
type: "POST",
dataType: "html"
});
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body bgcolor="green">
<form name="myform" method="POST">
<p style="align=:center"> <textarea rows="10" cols="30" name="txt" id="txt" onclick=chk_length(this.form); onkeypress=chk_length(this.form); ></textarea></p>
<input style="background-color:Yellow" size="4" name="num"> <i style="color:yellow"> Characters left</i><br>
<input type="submit" value="Save Message" id="Save" name="button" onclick="myCall()" onmouseover=chk_length(this.form);>
<br>
</form>
</body>
</html>
函数chk_长度(myform)
{
maxLen=767;
如果(myform.txt.value.length>=maxLen)
{
警报(“您已达到文本区域的最大长度”);
myform.txt.value=myform.txt.value.substring(0,maxLen);
}
其他的
{
myform.num.value=maxLen-myform.txt.value.length;
}
}
函数myCall(){
$.ajax({
url:“localhost:8080/yours/txt.php”,
类型:“POST”,
数据类型:“html”
});
}
左字符
放置在我的系统的webserver(Wampserver)位置的txt.php页面如下所示
<html>
<body>
<?php
$txt=$_POST['txt'];
$dt=date("y/m/d");
$button=$_POST['button'];
echo "$txt";
include("pg2.php");
?>
<p style="color:orange">* Max 767 characters only *</p>
<?php
mysql_connect('localhost','root','');
mysql_query("create database mydb");
mysql_select_db('mydb');
$querys="create table mydb.events(edate Date,event varchar(767), UNIQUE(event))";
mysql_query($querys);
mysql_query('create table mydb.like(edate Date,likes varchar(767),UNIQUE(likes))');
if($button=="Save Message")
{
if($txt!="")
{
$quer="insert into mydb.events(edate,event) values('$dt','$txt')";
$quer=str_replace("\r\n"," ",$quer);
if(mysql_query($quer))
{
$file=fopen('./backup.txt','a');
fputs($file,$quer."; \r\n");
fclose($file);
}
$num=mysql_affected_rows();
//echo "$num rows affected";
}
$query="select * from mydb.events";
$result=mysql_query($query);
?>
<table>
<?php
while($row=mysql_fetch_row($result))
{
?>
<tr><td><p style="color:yellow"><?php print("$row[0]"); echo ":"; ?></p></td><td><p style="color:yellow"><?php print("$row[1]"); echo "<br>------------------------------<br>"; ?></p></td></tr>
<?php
}
?>
</table>
<?php
}
if($button=="Like it")
{
if($txt!="")
{
$quer1="insert into mydb.like(edate,likes) values('$dt','$txt')";
$quer1=str_replace("\r\n"," ",$quer1);
if(mysql_query($quer1))
{
$file=fopen('./backup.txt','a');
fputs($file,$quer1."; \r\n");
fclose($file);
}
$num=mysql_affected_rows();
}
$query2="select * from mydb.like";
$results=mysql_query($query2);
?>
<table>
<?php
while($rows=mysql_fetch_row($results))
{
?>
<tr><td><p style="color:yellow"><?php print($rows[0]); echo ":"; ?></p></td><td><p style="color:yellow"><?php print("$rows[1]"); echo "<br>------------------------------ <br>"; ?></p></td></tr>
<?php
}
?>
</table>
<?php
}
?>
</body>
</html>
*最多767个字符*
哈哈明白了:)把PHP应用程序加载到android手机上没有什么额外的工作要做。。。保留所有资源,如html、php和图像、css等。。请确保Web服务器允许其他人访问。。也就是说,您只需要在phonegap应用程序的mainActivity.java中包含以下代码片段
package com.example.mobility;
import org.apache.cordova.DroidGap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.loadUrl("http://IPAddress:port/folder_name/index.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
你完了
注意:不要创建我所提出的代码。。没用的