2014-04-22 24 views
0

我在我的joomla文章中遇到問題,我使用Sourcerer自定義代碼。Ajax不適用於我在joomla中的自定義組件

下面是一些爲AJAX腳本,我的例子代碼:

<script type="text/javascript"> 
    function showBox1(element) { 
     document.getElementById('hideBox1').style.display = ""; 

     if (element == "") 
     { 
      document.getElementById("txtHint").innerHTML = ""; 
      return; 
     } 
     if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp = new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange = function() 
     { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
      { 
       document.getElementById("txtHint").innerHTML = xmlhttp.responseText; 
      } 
     } 
     xmlhttp.open("GET", "home/matedis/public_html/joomla/Add_Edit_Intake/getuser.php?q=" + element, true); 
     xmlhttp.send(); 

    } 

</script> 

的值傳遞給函數的代碼是在這裏:

<?php 
// Get default database object 
$db = JFactory::getDBO(); 

// Get a new JDatabaseQuery object 
$query = $db->getQuery(true); 

// Build the query 
$query->select($db->quoteName('campusid')); 
$query->from($db->quoteName('campus')); 
$query->where($db->quoteName('collegeid') . '=' . $db->quote('1')); 

// Set the query for the DB oject to execute 
$db->setQuery($query); 
// Get the DB object to load the results as a list of objects 
$results = $db->loadObjectList(); 
if ($results) { 
    foreach ($results as $result) { 
     echo "<label class='option block spacer-t10'>"; 
     echo "<input type='radio' id ='campusID' name='campusID' value='$result->campusid' onChange='showBox1(this.value)'><span class='radio'></span>"; 
     echo $result->campusid; 
     echo '</label>'; 
    } 
} else { 
    echo 'Error'; 
} 
?> 

這裏是我的getuser.php代碼:

<?php 

$q = intval($_GET['q']); 
define('JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ]); // define JPATH_BASE on the external file 
require_once(JPATH_BASE . DS . 'libraries' . DS . 'import.php'); // framework 
require_once(JPATH_BASE . DS . 'configuration.php'); // config file 
$db = JFactory::getDBO(); 


$sql="SELECT courseid FROM course WHERE campusid = '".$q."'"; 




// Build the query 
$query->select($db->quoteName('courseid')); 
$query->from($db->quoteName('course')); 
$query->where($db->quoteName('campusid').'='. $db->quote($q)); //This later must change to retrieve id from current user 

// Set the query for the DB oject to execute 
$db->setQuery($query);// Get the DB object to load the results as a list of objects 
$results = $db->loadObjectList(); 
if($results){ 
    foreach($results as $result) 
    { 
     echo $result->courseid; 
} 

} 
else{ echo 'Error';} 
?> 

我有沒有犯過錯誤?因爲它沒有顯示我想要的代碼從這裏的代碼http://www.w3schools.com/php/php_ajax_database.asp。對不起,如果任何不便,因爲我還是新來joomla和php阿賈克斯。

回答

0
xmlhttp.open("GET","home/matedis/public_html/joomla/Add_Edit_Intake/getuser.php?q="+element,true); 

這是public_html的真實路徑嗎?沒有..

+0

這是我的php文件在我的服務器的路徑。這是我的ajax問題的原因? – Marcus

+0

而你的服務器主機配置爲相對路徑「home/matedis/public_html/joomla/Add_Edit_Intake /」?我不這麼認爲,因爲它的失敗 – Deep

+0

那麼我的getuser.php文件應該放在哪個文件夾中? 我目前使用cPanel來配置文件管理器,Add_Edit_Intake是我剛剛在joomla文件夾內創建的文件夾。 – Marcus