【问题标题】:How to run php files on Phone Gap?如何在 Phone Gap 上运行 php 文件?
【发布时间】:2012-03-29 06:16:55
【问题描述】:

我需要使用我的应用程序运行一些 php 文件,我将它们上传到我的网站。我在这里读过一篇文章,我对我的文件做了同样的事情,但是我的 php 文件正在使用 ajax,所以我无法运行它。我尝试了所有可能的方法,但我仍然错了。

search.html 通过 js 创建一个链接,并将这个链接传递给 get_data.php,并在带有results 标签的同一页面中显示结果。

搜索.html

function abc(target_url) {


    target_url = target_url||(generate_url()||"http://nces.ed.gov/collegenavigator/?");
    ajax = window.XMLHttpRequest?(new XMLHttpRequest()):(new ActiveXObject("Microsoft.XMLHttp"));
    ajax.onreadystatechange=function() {
      if(ajax.readyState===4) {
        html_data = ajax.responseText;
        //Do stuff with it like parsing, etc
        //alert(html_data);
        window.loading.style.visibility="hidden";
        document.getElementById("results").innerHTML = html_data ||"We're sorry";
      }
    };
    ajax.open("GET", "./get_data.php?url="+encodeURIComponent(target_url), true);
    ajax.send(null);
    window.loading.style.visibility="visible";


  }

这是get_data.php

<?php

include_once('simple_html_dom.php');
$target_url = $_REQUEST["url"];
$html = new simple_html_dom();
$html->load_file($target_url);
$gokhan='arik';
#$anchors = array_diff($html->find('table[class=resultsTable] a'), $html->find('td[class=addbutton] a'));
$h2 = $html->find('table[class=resultsTable] h2');
$ipeds = $html->find('p[class=ipeds hoverID]');

foreach($html->find('div[id=ctl00_cphCollegeNavBody_ucResultsMain_divMsg]') as $nOfResults){
            echo "<b>".strip_tags($nOfResults)."</b>";
        }
$loca = $html->find('table[class=itables] tbody tr td[class=pbe]');


for($i=0;$i<count($h2);$i++) {

   if(strip_tags($h2[$i])=="")    continue;
    #echo strip_tags(strtr($ipeds[$i], array("&nbsp;"=>" ")));
    $iped = explode(" ", strip_tags(strtr($ipeds[$i], array("&nbsp;"=>" "))));


    echo "<li data-theme='c' class='ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c'>
            <div class='ui-btn-inner ui-li'>
                <div class='ui-btn-text'>

                    <a href='search2.php?id=".$iped[2]."' class='ui-link-inherit'><h3 class='ui-li-heading'>".strip_tags($h2[$i])."</h3><p class='ui-li-desc'>".strip_tags(strtr($loca[$i], array('</h2>'=>'</h2> ')))."</p></a>
                </div>
                <span class='ui-icon ui-icon-arrow-r ui-icon-shadow'/>
            </div>
          </li> 
          ";


}

?>

【问题讨论】:

  • 我不认为 PhoneGap 允许您在手机上运行 PHP...您需要在自己的服务器上托管 PHP 并设置 HTTP_Access_control,以便 XHR 请求可以从您的 PhoneGap 应用程序中进行。
  • 我就是这么说的。你不能在手机上运行 PHP,我的 php 文件在服务器上。我很难连接它们。在另一篇文章中,它说要使用这样的东西。但它不工作。 $('#content').load('http://www.example.com/test.php');

标签: php ajax mobile cordova


【解决方案1】:

使用 ajax(您正在尝试的方法)是我建议的方法。您正在尝试在不使用框架的情况下执行 ajax 请求,如果您没有在站点的其他任何地方使用框架,我认为这是减少站点加载时间的好主意。但是你在尝试实现 ajax 的过程中发生了一些有趣的事情。

如果您不打算使用某些框架,我建议您使用类似 http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_callback

我注意到你在评论中提到了

$('#content').load('http://www.example.com/test.php');

这是指使用 jQuery 将内容加载到您的页面中。查看您上面提供的示例代码,我看不到您实际导入 jQuery 的位置。首先导入jQuery,然后尝试使用该代码。另外,我建议使用 $.ajax({}) 而不是 $("#content").load("url");

$.ajax({
   url: "http://www.example.com/test.php",
   success: function(x){
      $("#results").html(x);
   }
});

有关使用 jQuery 框架进行 ajax 请求的更多信息,请访问http://api.jquery.com/jQuery.ajax/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2020-07-19
    • 1970-01-01
    • 2021-07-06
    • 2012-01-24
    相关资源
    最近更新 更多