PHP &在构建Phonegap应用程序时使用html文件,以及如何为它们进行ajax调用


where to place the php & html files when building Phonegap app and how to make ajax calls for them

我正在尝试使用PhoneGap为现有的PHP web应用程序构建Android应用程序。我有用于逻辑的PHP文件和用于呈现UI的HTML文件。这些文件放在我系统的Webserver (Wampserver)中。现在我已经看到了一些来源,这是说->使Ajax调用PHP文件的web服务器从phonegap项目。基于此,我创建了一个index.html文件,该文件具有对目标txt.php文件的Ajax调用,并尝试在Android虚拟设备管理器上运行。我看到index.html被加载到AVD上,我在文本区给出了一些输入,然后点击了"保存消息"按钮。但我没看到AVD有任何反应。谁能请指导我如何放置所有这些HTML文件,php文件以及如何使ajax调用..我希望这个问题能对很多像我一样的初学者有所帮助。

目前,下面的代码被命名为index.html,并放置在Phonegap项目的assets/www/index.html中。我正在使用

加载index.html页面
  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);>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br>
</form>
</body>
</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>

哈哈明白了:)没有什么额外的工作要做加载PHP应用程序到android手机…保留所有的资源,如html, PHP和图像,CSS等。所有在你的网络服务器,并确保网络服务器将允许其他人访问。这就是你只需要在你的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;
}
}

你完成了!!

注意:不要创建这样的代码,我提出的问题…其无用…