2016-07-21 123 views
0

我想弄清楚當前頁面的php $ var是否可以傳遞給XMLHttpRequest2。被調用的文件位於/ assets/js目錄中的視圖(當前php頁面所在位置)的文件夾之外。我也使用CodeIgniter。嘗試傳遞$ user_id以在XMLHttpRequest2請求文件的側面使用SQL查詢。

publication_call.php(當前文件)

<form> 
    <input type="hidden" id="someid" value="<?= $idz ?>"/> 
    <?php 
     echo form_label('Validation: (Enter Publication keywords, Matches will appear in Dropdown >)'); 
     echo form_label('Matching<br>Publications:'); 
    ?> 
    <select name="matched_pub" id="matched_pub"></select> 
    </form> 

<script> 
    jQuery(function($){ 
    //still want to bind the change event 
    $('#matched_pub').bind('change', function(){ 
     $('#title').val($('#matched_pub option:selected').text()); 
    }); 
    $('#validation').keyup(function() { 
     showKeywords($('#validation').val()); 
     document.getElementById('matched_pub').style.display='block'; 
    }); 
    }); 
</script> 


    <script> 
    function showKeywords(str) 
    { 

     if (document.getElementById("matched_pub")) { 

      if (str.length==0) 
      { 
       document.getElementById("matched_pub").innerHTML=""; 
       document.getElementById("matched_pub").innerHTML=xmlhttp2.responseText; 
       return; 
      } 
      if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp2=new XMLHttpRequest(); 
      } 
      else 
      {// code for IE6, IE5 
       xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp2.onreadystatechange=function() 
      { 
       if (xmlhttp2.readyState==4 && xmlhttp2.status==200) 
       { 
        document.getElementById("matched_pub").innerHTML=xmlhttp2.responseText; 
       } 
      } 
      xmlhttp2.open("GET","/assets/keywordsearch.php?b="+str,true); 
      xmlhttp2.send(); 

     } 

    } 
</script> 

searchwords.php(請求/外部文件)

<?php 

$user = 'root'; 
$pass = 'root'; 
$db  = 'hey_there'; 
$host = 'localhost'; 

$conn = mysql_connect($host, $user, $pass); 
$db_selected = mysql_select_db($db, $conn); 

//trying to display special chars 
mysql_query("set names 'utf8'"); 
if(!$db_selected) { 
    echo 'broke'; 
} 
//echo 'db connected'; 
$q = $_GET["b"]; 
//explode and parse $q into all the fragments separated by spaces and do full text search +word1 +word2 +word3, this will ignore HTML tags as it ignores word order, will also solve the middle initial problem [db setup is not compatible with full text search, but can do likes per word, less efficient, but how it must be done] 
$queryWords = explode(' ', $q); 

//for services query, explode the query into words and search for each separately 
$query = "SELECT DISTINCT(pub_title) 
    FROM teacher_publications 
    JOIN users ON teacher_publications.user_id = users.id 
    WHERE keywords IS NOT NULL 
    AND pub_title IS NOT NULL 
    AND teacher_publications.user_id = 103 <-- $var will go here 
"; 
$queryServicesLoop = ''; 
$queryServicesEnd = ' ORDER BY pub_title ASC'; 

//loop through all words in string 
foreach($queryWords as $queryWord) { 
    $queryServicesLoop .= " AND (keywords LIKE '%{$queryWord}%')"; 
} 
$queryServices = $queryServices.$queryServicesLoop; 
$queryServices = $queryServices.$queryServicesEnd; 

$resultServices = mysql_query($queryServices); 
$services =''; 

if(mysql_num_rows($resultServices) > 0){  
    while($rowServices = mysql_fetch_assoc($resultServices)) { 
     $services .= '<option value="' . $rowServices['pub_title'] . '">' . $rowServices['pub_title'] . '</option>'; 
    } 
} 



if(mysql_num_rows($resultServices) == 0) 
{ 
    echo '<option value="">Your search failed to find any matching results.</option>'; 
} 
else 
{ 
    echo '' . $services . ''; 
} 

/* ============== ================ 編輯代碼 ============================== */

publication_call.php(當前文件)

<input type="hidden" id="someid" value="<?= $user_id ?>"/> 

<script> 
    function showKeywords(str) 
    { 

     if (document.getElementById("matched_pub")) { 


      if (str.length==0) 
      { 
       document.getElementById("someid"); 
       document.getElementById("matched_pub").innerHTML=""; 
       document.getElementById("matched_pub").innerHTML=xmlhttp2.responseText; 
       return; 
      } 
      if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp2=new XMLHttpRequest(); 
      } 
      else 
      {// code for IE6, IE5 
       xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp2.onreadystatechange=function() 
      { 
       if (xmlhttp2.readyState==4 && xmlhttp2.status==200) 
       { 
        document.getElementById("matched_pub").innerHTML=xmlhttp2.responseText; 
       } 
      } 
      xmlhttp2.open("GET","/assets/keywordsearch.php?b="+str+"&user_id="+document.getElementById('someid'), true); 
      // xmlhttp2.open("GET","/assets/keywordsearch.php?b="+str,true); 
      xmlhttp2.send(); 

     } 

    } 
</script> 

searchwords.php(請求/外部文件)

$usr = $_GET["user_id"]; 

$query = "SELECT DISTINCT(pub_title) 
      FROM teacher_publications 
      JOIN users ON teacher_publications.user_id = users.id 
      WHERE keywords IS NOT NULL 
      AND pub_title IS NOT NULL 
      AND teacher_publications.user_id = ".$usr." 

「;

+0

php在服務器上運行,javascript運行在客戶端上。 PHP不能發出ajax請求,它只能響應它們。你可以在頁面創建時嵌入任何你想要的東西,或者讓php響應ajax調用,但是你不能使用php來做一個xmlhttprequest(aka ajax)。 –

回答

0

你可以把$user_id一個隱藏的輸入字段內,並且使用Javascript,讀它的價值在你的Ajax請求使用

你可以這樣說:

<input type="hidden" id="someid" value="<?= $user_id ?>

然後在完成之後,您可以通過執行此操作來獲得價值:

document.getElementById('someid');使用純Javascript或$('#someid').value();如果您使用jquery

這將得到您可以在請求中使用的用戶標識值。

像這樣:

xmlhttp2.open("GET","/assets/keywordsearch.php?b="+str+"&user_id="+document.getElementById('someid').value, true); 與一個以上 現在您可以訪問用戶ID的值$_GET['user_id']所請求的文件替換當前的xmlhttp2.open。

+0

你有這樣的代碼示例嗎? JS是我的弱點謝謝! – Danny

+0

@Danny用代碼示例編輯我的答案 – FMashiro

+0

但是,如何在請求/外部文件的SQL語句中使用該var('someid')? $查詢=「SELECT DISTINCT(pub_title) \t \t FROM teacher_publications \t \t用戶加入ON teacher_publications.user_id = users.id \t \t WHERE關鍵字IS NOT NULL \t \t和pub_title IS NOT NULL \t \t和teacher_publications .user_id =「。$ someid。「 」; – Danny