2012-03-29 99 views
1

我需要運行一些PHP文件與我的應用程序,我上傳到我的網站。我在這裏閱讀了一篇文章,我爲我的文件做了同樣的事情,但是我的php文件與ajax一起工作,所以我無法運行它。我嘗試了所有可能的方法,但我仍然錯誤。如何在手機差距上運行php文件?

search.html通過js創建鏈接並將此鏈接傳遞給get_data.php,並在results標記的同一頁面中顯示結果。

search.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> 
      "; 


} 

?>

+0

我不認爲'PhoneGap'可以讓你在手機上運行PHP更多的信息.​​.....你需要舉辦在你自己的服務器上設置PHP,並設置[HTTP_Access_control](https://developer.mozilla.org/En/HTTP_access_control),以便從你的PhoneGap應用程序對它進行XHR請求。 – Sathvik 2012-03-29 17:08:07

+0

這就是我所說的。你不能在手機上運行PHP,我的PHP文件在服務器上。我無法連接它們。 在另一篇文章中說它使用類似的東西。但它不起作用。 '$('#content').load('http://www.example.com/test.php');' – 2012-03-29 20:20:08

回答

0

使用AJAX(方法您正在嘗試)是我建議的方法。你試圖在不使用框架的情況下執行ajax請求,如果你沒有在網站中使用任何其他框架,我認爲這是減少站點加載時間的好主意。但是你有一些有趣的事情正在以你試圖實現AJAX的方式進行。

如果你不打算使用一些框架,我建議像http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_callback

我在你的評論中提到

$(「#內容」)的通知。負載(」 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/