[英]Javascript won't work with php in joomla
這是我的問題。 我現在正嘗試從joomla數據庫獲取所有結果,並顯示為復選框。 然后,如果我的PHP代碼沒有從數據庫中得到結果,它應該提示錯誤消息並在joomla中隱藏特定的DIV。 但是我的代碼無法正常工作,它不會隱藏我想要的DIV元素。
這是我的代碼:
<html>
<head>
</head>
<?php
$q = $_GET['q'];
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
$db = JFactory::getDBO();
$sql="SELECT courseid FROM course WHERE campusid = '$q'";
// Set the query for the DB oject to execute
$db->setQuery($sql);// 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'>";
echo "<input type='checkbox' id ='courseID' name='courseID' value='$result->courseid' onChange='showBox2(this.value)'><span class='checkbox'></span>";
echo $result->courseid;
echo '</label>';
echo '<br/>';
}
}
else{ echo 'Error. No data found in '.$q;
echo '<script type="text/javascript">';
echo 'function myFunction(){ document.getElementById('hideBox2').style.display =none; };';
echo '</script>';}
?>
<body>
</body>
</html>
我想隱藏的DiV元素:
<div id="hideBox2" style="display:none;">
<div class="spacer-t40 spacer-b30">
<div class="tagline"><span> Date Open For Registration & Course Start Date </span></div><!-- .tagline -->
</div>
<div class="frm-row">
<div class="section colm colm6">
<label class="field prepend-icon">
<input type="text" id="from" name="from" class="gui-input">
<label class="link-icon jsn-icon-calendar"></i></label>
</label>
</div><!-- end section -->
<div class="section colm colm6">
<label class="field prepend-icon">
<input type="text" id="to" name="to" class="gui-input">
<label class="field-icon"><i class="fa fa-calendar"></i></label>
</label>
</div><!-- end section -->
</div><!-- end .frm-row section -->
</div>
另外,這是一個php文件。
您應該在JavaScript部分中轉義引號字符:
echo 'function myFunction(){ document.getElementById(\'hideBox2\').style.display = \'none\'; };';
或者,甚至可能更好,直接輸出javascript:
?> function myFunction(){ document.getElementById('hideBox2').style.display = 'none'; }; <?php
不要忘記也將您的“ none”值放在引號中。
但是,您的代碼看起來很凌亂。 我找不到要隱藏的div的輸出位置。 您在打開主體之前就輸出列表。 另外,您可以在輸出之前檢查數據庫結果計數,如果還需要保留div,則可以“確定”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.