2013-05-26 38 views
0

您好,我試圖從JavaScript傳遞一個名爲url的變量到一個名爲解析所有在同一頁面內的PHP函數,下面是我的下面的嘗試,但它沒有渲染任何東西。我也試圖innerthml函數解析。我做錯了什麼?注意:爲了簡單起見,函數解析被完全截斷,它實際上不僅僅是回顯一個變量,而是回聲更多的html。在同一頁面傳遞一個JavaScript變量到一個PHP函數問題

這裏是我的AJAX請求裏面的一個JavaScript腳本內的PHP腳本。

"<script type=\"text/javascript\">\n". 
"\n". 
"// create and return an XMLHttpRequest object\n". 
"function createRequest() {\n". 
" var ajaxRequest; // the xmlrequest object\n". 
" try{\n". 
"  // Opera 8.0+, Firefox, Safari\n". 
" ajaxRequest = new XMLHttpRequest();\n". 
" } catch (e){\n". 
"  // Internet Explorer Browsers\n". 
" try{\n". 
"  ajaxRequest = new ActiveXObject(\"Msxml2.XMLHTTP\");\n". 
" } catch (e) {\n". 
"  try{\n". 
"  ajaxRequest = new ActiveXObject(\"Microsoft.XMLHTTP\");\n". 
"  } catch (e){\n". 
"   // Something went wrong\n". 
"  alert(\"Your browser broke!\");\n". 
"  return false;\n". 
"  }\n". 
" }\n". 
" }\n". 
" return ajaxRequest;\n". 
"} // end of createRequest\n". 

這裏是我的實際JavaScript和我想要傳遞變量URL和功能的innerHTML解析

"var spellcheck = function(data){\n". 
" var found=false;var url='';\n". 
" var text=data[0];\n". 
"  if(text!=document.getElementById('spellcheckinput').value)return;\n". 
"  for(i=0;i<data[1].length;i++){if(text.toLowerCase()==data[1][i].toLowerCase()){found=true;url='http://en.wikipedia.org/wiki/'+text;document.getElementById('spellcheckresult').innerHTML='<b style=\"color:green\">Correct</b> - <a target=\"_top\" href=\"'+url+'\">link</a>';}}\n". 
"  if(!found)\n". 
"  document.getElementById('spellcheckresult').innerHTML='<b style=\"color:red\">Incorrect</b>';};\n". 
"  var getjs= function(value){\n". 
"  if(!value)return;". 
" var url='http://en.wikipedia.org/w/api.php?action=opensearch&search='+value+'&format=json&callback=spellcheck';". // this is the variable I want to pass 
"document.getElementById('spellcheckresult').innerHTML='Checking ...';". 
"var elem=document.createElement('script');". 
"elem.setAttribute('src',url);". 
"elem.setAttribute('type','text/javascript');". 
"document.getElementsByTagName('head')[0].appendChild(elem);};\n". 
"document.getElementById('resultdiv').innerHTML='" . parse() . "';\n". //here wanting to innerhtml my php function 

這是我成功的AJAX JavaScript變量傳遞給我的PHP函數樓下

" var requestone = createRequest();" 
     "var variabletosend = url;" 
     "var vars = "deletenode=" + encodeURIComponent(variabletosend);" 
      "requestone.open("POST", "parse()", true);" 
      "requestone.setRequestHeader("Content-type", "application/x-www-form-urlencoded");" 
      "requestone.onreadystatechange = function(){" 
      "handleRequest(requestone);" 
      "}" 
      "requestone.send(vars);" 


"</script>\n". 
"\n". 
"<form action=\"#\" method=\"get\" onsubmit=\"return false\">\n". 
"<p>Enter a species here : <input id=\"spellcheckinput\" onkeyup=\"getjs (this.value);\" type=\"text\"> <span id=\"spellcheckresult\"></span></p>\n". 
"</form>\n". 
"<div id=resultdiv></div>". 
"</body>\n". 
"</html>\n". 
"\n"; 

function parse($url){ 
print $url; 
} 
?> 
+0

增加的功能解析注 – Squirtle

+3

爲什麼是一個PHP字符串中您的JavaScript?它是否被迴應?爲什麼不把腳本保存在'.js'文件中,從HTML文檔中引用它呢? –

+0

@PhpMyCoder你的權利,抱歉的格式。 – Squirtle

回答

0

Mhh,你的ajax調用是錯誤的。您無法使用AJAX調用php函數。你可以調用一個php頁面,它有一個php函數。所以你需要用你的參數來調用你的頁面。 如果您檢測到param,則執行parse(),如果沒有,則回顯javascript代碼。

假設你的代碼是在一個名爲「parser.php」頁面,這裏是應該工作代碼:

<?php 
//We check if we have the param within the page call 
$theDeletenode = false; 
if(isset($_POST['deletenode'])) 
    $theDeletenode = $_POST['deletenode']; 

if($theDeletenode) 
{ 
    //The param 'deletenode' is given, so we juste have to call the php function "parse", to return the value 
    parse($theDeletenode); 
}else 
{ 
    // No parametre, so we echo the javascript, and the form (without the quote and \n, it's much better) 
?> 
<html><head> 
<script type="text/javascript"> 
// create and return an XMLHttpRequest object 
function createRequest() { 
    var ajaxRequest; // the xmlrequest object 
    try{ 
     // Opera 8.0+, Firefox, Safari 
     ajaxRequest = new XMLHttpRequest(); 
     } catch (e){ 
     // Internet Explorer Browsers 
     try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
      try{ 
       ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
       } catch (e){ 
       // Something went wrong 
       alert("Your browser broke!"); 
       return false; 
       } 
      } 
     } 
    return ajaxRequest; 
}; // end of createRequest 

var spellcheck = function(data){ 
    var found=false;var url=''; 
    var text=data[0]; 
    if(text!=document.getElementById('spellcheckinput').value) return; 
     for(i=0;i<data[1].length;i++) 
      { 
      if(text.toLowerCase()==data[1][i].toLowerCase()) 
       {found=true; 
       url='http://en.wikipedia.org/wiki/'+text;document.getElementById('spellcheckresult').innerHTML='<b style=\"color:green\">Correct</b> - <a target=\"_top\" href=\"'+url+'\">link</a>'; 
       } 
      } 
     if(!found) 
     document.getElementById('spellcheckresult').innerHTML='<b style=\"color:red\">Incorrect</b>'; 
     }; 


var requestone = createRequest(); 

var getjs= function(value) 
{ 
    if(!value) return; 
    var url='http://en.wikipedia.org/w/api.php?action=opensearch&search='+value+'&format=json&callback=spellcheck'; // this is the variable I want to pass 
    document.getElementById('spellcheckresult').innerHTML='Checking ...'; 
    var elem=document.createElement('script'); 
    elem.setAttribute('src',url); 
    elem.setAttribute('type','text/javascript'); 
    document.getElementsByTagName('head')[0].appendChild(elem); 

    // Ajax call to this page, this time with the 'deletenode' parameter 
    var vars = "deletenode=" + encodeURIComponent(url); 
    requestone.open("POST", "parser.php", true); // parser.php : the name of this current page 
    requestone.onreadystatechange = handleRequest; // function to handle the response 
    requestone.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
    requestone.send(vars); 

};  

    function handleRequest() 
    { 
    //here we handle the php page response by echoing de result of the php page (normally the result of the parse function) 
    try{ 
     if(requestone.readyState == 4 && requestone.status == 200) 
      document.getElementById('resultdiv').innerHTML= requestone.responseText; 
    } catch (e) { 
     //Wrong server answer... 
    } 
    }; 
</script> 
</head> 
<body> 

<form action="#" method="get" onsubmit="return false"> 
<p>Enter a species here : <input id="spellcheckinput" onkeyup="getjs (this.value);" type="text"> <span id="spellcheckresult"></span></p> 
</form> 
<div id=resultdiv></div> 
</body> 
</html> 

<?php 
// end of the javascript+form echo 
} 


function parse($url){ 
// this function is only called when the POST param is given 
print $url; 
} 

?> 
+0

嘿CrtlX我進入這個,但我從字面上剛剛得到的PHP功能吐出來的動態和JavaScript和PHP不互相交談,這裏是我得到 – Squirtle

+0

在這裏輸入一個物種:[文本框] - 連接 查找( 'table.infobox'); ($ html-> find('img')as $ element){$ image [] = $ element; }打印$ image [1]; $ data = array(); ($ td = $ row-> find('> td'); if(count($ td)== 2){$ name = $ td [0] - > innertext; $ td = $ td [1] - > find('a'); $ text = $ td [0] - > innertext; $ data [$ name] = $ text; }} print「」; foreach($ data爲$ value => $ before){print「」; } print「 $ value \t $ before 」; }?> – Squirtle

+0

對不起,我會忘記代碼中的一行。我錯過了POST請求的編碼部分。該行: requestone.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 我已經用正確的線更新了代碼示例。現在工作正常 – CtrlX

相關問題