Ajax不能在joomla中使用我的自定义组件

时间:2014-04-22 15:11:58

标签: javascript php ajax joomla

我的joomla文章有问题,我用Sourcerer自定义代码。

以下是我的ajax javascript的一些示例代码:

<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 ajax的新手。

1 个答案:

答案 0 :(得分:0)

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

这是public_html的真实路径?号。