/ * 在下面的代码中有一个下拉列表,必须在每次发生更改时调用该函数。 此函数名称为TotalCountsOfPL()。 下拉列表是名称,Id是“sltLeaveType”;
在调用文本框(AjxSelectedLeaveType)之后,应该使用下拉列表的选定值填充。
应该填充文本框(AjxTakenLeave),并填充数据库提取的计数。
* /
我在项目中使用了Ajax概念。
这是Dropdownlist的变化我正在调用函数(方法)。但这不起作用。
表示当我选择dropdownList的内容时,所选数据(选项)应显示在名为
的文本字段中我的html文件是 我在项目中使用了Ajax概念。
这是Dropdownlist的变化我正在调用函数(方法)。但这不起作用。
我的html文件是
<html>
<head>
<script>
function TotalCountsOfPL(str)
{
if(iset(session_unregister("mySessionVariable")))
{
session_destroy();
};
session_start();
session_register("mySessionVariable");
if(str!="")
{
$_SESSION["mySessionVariable"] = str;
document.getElementById("AjxSelectedLeaveType").innerHTML=str;
}
if (str=="")
{
document.getElementById("AjxTakenLeave").innerHTML="";
//document.getElementById("AjxLeaveType").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("AjxTakenLeave").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","getPLCount.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
}
$prevEmployeeId= (isset($_POST['cmbEmployeeId'])) ? $_POST['cmbEmployeeId'] : "";
$prevLeaveFromDate = (isset($_POST['txtLeaveFromDate'])) ? $_POST['txtLeaveFromDate'] : "";
$prevLeaveToDate = (isset($_POST['txtLeaveToDate'])) ? $_POST['txtLeaveToDate'] : "";
$prevLeaveType = (isset($_POST['sltLeaveType'])) ? $_POST['sltLeaveType'] : "";
$prevToTime = (isset($_POST['sltLeaveToTime'])) ? $_POST['sltLeaveToTime'] : "";+
// Gnanendra .. Here I have consider the variable $prevFromTime as "Total days" selected by the dropdownlist
$prevFromTime = (isset($_POST['sltLeaveFromTime'])) ? $_POST['sltLeaveFromTime'] : "";
//G Comment below
$prevTotalTime = (isset($_POST['txtLeaveTotalTime'])) ? $_POST['txtLeaveTotalTime'] : "";
$prevComments = (isset($_POST['txtComments'])) ? $_POST['txtComments'] : "";
$timeElementClass = (!empty($prevLeaveFromDate) && ($prevLeaveFromDate == $prevLeaveToDate)) ?
"show" : "hide";
$AjxSelectedLeaveType=(isset($_POST['AjxSelectedLeaveType'])) ? $_POST['AjxSelectedLeaveType'] : "";
$AjxTakenLeave=(isset($_POST['AjxTakenLeave'])) ? $_POST['AjxTakenLeave'] : "";
?>
<form id="frmLeaveApp" name="frmLeaveApp" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?leavecode=Leave&action=<?php
echo $modifier; ?>">
<input type="hidden" value="<?php echo $token;?>" name="token" />
<?php if (isset($confirmDate)) { ?>
<input type="hidden" name="confirmDate" value="<?php echo $prevLeaveFromDate; ?>"/>
<?php } ?>
<?php if (isset($role)) { ?>
<?php if(($role == authorize::AUTHORIZE_ROLE_ADMIN) || ($role == authorize::AUTHORIZE_ROLE_SUPERVISOR)){ ?>
<label for="cmbEmployeeId"><?php echo $lang_Leave_Common_EmployeeName; ?><span class="required">*</span></label>
<div>
<input type="hidden" name="cmbEmployeeId" id="cmbEmployeeId" value="<?php echo isset($prevEmployeeId) ? $prevEmployeeId : ""; ?>" />
<div class="yui-ac" id="employeeSearchAC" style="float: left">
<input name="txtEmployeeId" autocomplete="off" class="yui-ac-input" id="txtEmployeeId" type="text" value="<?php echo isset($empName) ?
CommonFunctions::escapeHtml($empName) : ""; ?>" tabindex="2" onfocus="showAutoSuggestTip(this)" style="color: #999999" />
<div class="yui-ac-container" id="employeeSearchACContainer" style="top: 28px; left: 10px;">
<div style="display: none; width: 159px; height: 0px; left: 100em" class="yui-ac-content">
<div style="display: none;" class="yui-ac-hd"></div>
<div class="yui-ac-bd">
<ul>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
</ul>
</div>
<div style="display: none;" class="yui-ac-ft"></div>
</div>
<div style="width: 0pt; height: 0pt;" class="yui-ac-shadow"></div>
</div>
</div>
</div>
<br class="clear"/>
<?php } ?>
<?php } ?>
<label for="sltLeaveType"><?php echo $lang_Leave_Common_LeaveType; ?></label>
<select name="sltLeaveType" id="sltLeaveType" class="formSelect" onchange="TotalCountsOfPL(this.value)">
<?php
$skippedLeaveTypesCount = 0;
if (is_array($records[1])) {
foreach ($records[1] as $record) {
$className = get_class($record);
if ($className == 'LeaveQuota') {
if ($record->isLeaveQuotaDeleted()) {
$skippedLeaveTypesCount++;
continue;
}
}
$selected = ($record->getLeaveTypeID() == $prevLeaveType) ? 'selected="selected"' : "";
?>
<option <?php echo $selected;?> value="<?php echo $record->getLeaveTypeID();?>"><?php echo $record->getLeaveTypeName();
?></option>
<?php }
if ($skippedLeaveTypesCount == count($records[1])) { ?>
<option value="-1">-- <?php echo $lang_Error_NoLeaveTypes; ?> --</option>
<?php }
} else { ?>
<option value="-1">-- <?php echo $lang_Error_NoLeaveTypes; ?> --</option>
<?php } ?>
</select>
<br class="clear"/>
<!--Gnanendra Here the Leave date range will get-->
<?php
if (!(is_array($records[1])) && ($modifier == 'Leave_Apply')) { ?>
<div class="notice"><?php echo $lang_Leave_Common_LeaveQuotaNotAllocated; ?></div>
<br class="clear"/>
<?php } ?>
<label for="txtLeaveFromDate"><?php echo $lang_Leave_Common_FromDate; ?><span class="required">*</span></label>
<input name="txtLeaveFromDate" type="text" id="txtLeaveFromDate" size="10"
value="<?php echo $prevLeaveFromDate; ?>" class="formDateInput"/>
<input type="button" name="Submit" value=" " class="calendarBtn" id="btFromDate"/>
<br class="clear"/>
<label for="txtLeaveToDate"><?php echo $lang_Leave_Common_ToDate; ?><span class="required">*</span></label>
<input name="txtLeaveToDate" type="text" id="txtLeaveToDate" size="10"
value="<?php echo $prevLeaveToDate; ?>" class="formDateInput"/>
<input type="button" name="Submit" value=" " class="calendarBtn" id="btToDate"/>
<br class="clear"/>
<div id="trTime1" class="<?php echo $timeElementClass;?>">
<label for="sltLeaveFromTime"><?php echo $lang_Leave_Common_FromTime; ?></label>
<?php $lang_Leave_Common_FromTime="Total Days"; ?>
<label for="sltLeaveFromTime"><?php echo $lang_Leave_Common_FromTime; ?></label>
<select name="sltLeaveFromTime" id="sltLeaveFromTime" onchange="fillTimes();"
class="formTimeSelect">
<select name="sltLeaveFromTime" id="sltLeaveFromTime" onchange="FillTheDayLength();"
class="formTimeSelect">
<option value=""></option>
<!-- It will the time to the drop downlist (00:00 to 23:59) -->
<?php
for ($i=$startTime; $i<=$endTime; $i+=$interval) {
$timeVal = date('H:i', $i);
$selected = ($timeVal == $prevFromTime) ? 'selected="selected"' : "";
?>
<option <?php echo $selected; ?> value="<?php echo $timeVal; ?>" ><?php echo LocaleUtil::getInstance()->formatTime($timeVal);
?></option>
<?php } ?>
</select>
<label for="sltLeaveToTime"><?php echo $lang_Leave_Common_ToTime; ?></label>
<select name="sltLeaveToTime" id="sltLeaveToTime" onchange="fillTimes();"
class="formTimeSelect">
<option value=""></option>
<?php
for ($i=$startTime; $i<=$endTime; $i+=$interval) {
$timeVal = date('H:i', $i);
$selected = ($timeVal == $prevToTime) ? 'selected="selected"' : "";
?>
<option <?php echo $selected; ?> value="<?php echo $timeVal; ?>" ><?php echo LocaleUtil::getInstance()->formatTime($timeVal);
?></option>
<?php } ?>
</select>
<br class="clear"/>
<label for="txtLeaveTotalTime"><?php echo $lang_Leave_Common_TotalHours; ?></label>
<input name="txtLeaveTotalTime" id="txtLeaveTotalTime" size="4" onchange="fillTimes();"
value="<?php echo $prevTotalTime; ?>" class="formInputText" style="width:3em;"/>
<br class="clear"/>
</div>
<div id="trTime2" class="<?php echo $timeElementClass;?>">
</div>
<div id="trTime3" class="<?php echo $timeElementClass;?>">
</div>
<div id="trTime4" class="<?php echo $timeElementClass;?>">
</div>
<br class="clear"/>
<label for="txtComments"><?php echo $lang_Leave_Common_Comment; ?></label>
<textarea name="txtComments" id="txtComments" class="formTextArea" rows="3" cols="20"
><?php echo $prevComments;?></textarea>
<!-- -->
<input name="AjxSelectedLeaveType" type="text" id="AjxSelectedLeaveType" size="30"
value="<?php (isset($_POST['AjxSelectedLeaveType'])) ? $_POST['AjxSelectedLeaveType'] : "Leave Type";?>" class="formDateInput"/>
<input name="AjxTakenLeave" type="text" id="AjxTakenLeave" size="30"
value="<?php (isset($_POST['AjxTakenLeave'])) ? $_POST['AjxTakenLeave'] : "Taken Leave";?>" class="formDateInput"/>
<!-- -->
<br class="clear"/>
<div class="formbuttons">
<input type="button" class="<?php echo $btnClass;?>" id="saveBtn"
onclick="addSave();" onmouseover="moverButton(this);" onmouseout="moutButton(this);"
value="<?php echo $btnTitle;?>" title="<?php echo $btnTitle;?>"/>
</div>
</form>
</div>
</body>
And the Following is getPLCount.php
<?php
$q=$_POST["AjxSelectedLeaveType"];
$Eid=$_SESSION["mySessionVariable"];
$con = mysql_connect('localhost', 'gnuser', 'anu123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("My_Database", $con);
$EmployeeId= $q;
if (isset($q))
{
$query = "select count(leave_request_id) from leave_requests
where leave_type_id='LTY002' AND employee_id=".$q.";";
}
if(isset($Eid))
{
$query = "select count(leave_request_id) from leave_requests
where leave_type_id='LTY002' AND employee_id=".$Eid.";";
}
$result = mysql_query($query);
echo "document.getElementById("AjxTakenLeave").innerHTML={$result}";
mysql_close($con);
?>
请任何人告诉我它为什么不起作用。
答案 0 :(得分:2)
您正在将PHP与JavaScript混合使用。例如:
if(str!="")
{
$_SESSION["mySessionVariable"] = str;
document.getElementById("AjxSelectedLeaveType").innerHTML=str;
}
$_SESSION
是PHP,document.getElementById
是JavaScript。
Javascript在客户端执行(即:broweser),PHP在您的服务器上执行。你不能这样混合它们。
答案 1 :(得分:2)
你可能会收到一个错误TotalCountsOfPL函数,我认为(纠正我,如果有误)你试图在Javascript函数中使用PHP函数。即会话功能。这不合法。我还注意到你的代码中有一个语法错误,在body标签之后有一个php标签,在它之后有一个}但是它没有在php上下文中打开。我建议你将显示错误设置为true,并使用javascript调试器,如firebug或dragonfly。
答案 2 :(得分:0)
试试这个:)
<html>
<head>
<script>
function TotalCountsOfPL(str)
{
if(iset(session_unregister("mySessionVariable")))
{
session_destroy();
};
session_start();
session_register("mySessionVariable");
if(str!="")
{
$_SESSION["mySessionVariable"] = str;
document.getElementById("AjxSelectedLeaveType").innerHTML=str;
}
if (str=="")
{
document.getElementById("AjxTakenLeave").innerHTML="";
//document.getElementById("AjxLeaveType").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("AjxTakenLeave").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","getPLCount.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$prevEmployeeId= (isset($_POST['cmbEmployeeId'])) ? $_POST['cmbEmployeeId'] : "";
$prevLeaveFromDate = (isset($_POST['txtLeaveFromDate'])) ? $_POST['txtLeaveFromDate'] : "";
$prevLeaveToDate = (isset($_POST['txtLeaveToDate'])) ? $_POST['txtLeaveToDate'] : "";
$prevLeaveType = (isset($_POST['sltLeaveType'])) ? $_POST['sltLeaveType'] : "";
$prevToTime = (isset($_POST['sltLeaveToTime'])) ? $_POST['sltLeaveToTime'] : "";+
// Gnanendra .. Here I have consider the variable $prevFromTime as "Total days" selected by the dropdownlist
$prevFromTime = (isset($_POST['sltLeaveFromTime'])) ? $_POST['sltLeaveFromTime'] : "";
//G Comment below
$prevTotalTime = (isset($_POST['txtLeaveTotalTime'])) ? $_POST['txtLeaveTotalTime'] : "";
$prevComments = (isset($_POST['txtComments'])) ? $_POST['txtComments'] : "";
$timeElementClass = (!empty($prevLeaveFromDate) && ($prevLeaveFromDate == $prevLeaveToDate)) ?
"show" : "hide";
$AjxSelectedLeaveType=(isset($_POST['AjxSelectedLeaveType'])) ? $_POST['AjxSelectedLeaveType'] : "";
$AjxTakenLeave=(isset($_POST['AjxTakenLeave'])) ? $_POST['AjxTakenLeave'] : "";
?>
<form id="frmLeaveApp" name="frmLeaveApp" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?leavecode=Leave&action=<?php
echo $modifier; ?>">
<input type="hidden" value="<?php echo $token;?>" name="token" />
<?php if (isset($confirmDate)) { ?>
<input type="hidden" name="confirmDate" value="<?php echo $prevLeaveFromDate; ?>"/>
<?php } ?>
<?php if (isset($role)) { ?>
<?php if(($role == authorize::AUTHORIZE_ROLE_ADMIN) || ($role == authorize::AUTHORIZE_ROLE_SUPERVISOR)){ ?>
<label for="cmbEmployeeId"><?php echo $lang_Leave_Common_EmployeeName; ?><span class="required">*</span></label>
<div>
<input type="hidden" name="cmbEmployeeId" id="cmbEmployeeId" value="<?php echo isset($prevEmployeeId) ? $prevEmployeeId : ""; ?>" />
<div class="yui-ac" id="employeeSearchAC" style="float: left">
<input name="txtEmployeeId" autocomplete="off" class="yui-ac-input" id="txtEmployeeId" type="text" value="<?php echo isset($empName) ?
CommonFunctions::escapeHtml($empName) : ""; ?>" tabindex="2" onfocus="showAutoSuggestTip(this)" style="color: #999999" />
<div class="yui-ac-container" id="employeeSearchACContainer" style="top: 28px; left: 10px;">
<div style="display: none; width: 159px; height: 0px; left: 100em" class="yui-ac-content">
<div style="display: none;" class="yui-ac-hd"></div>
<div class="yui-ac-bd">
<ul>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
<li style="display: none;"></li>
</ul>
</div>
<div style="display: none;" class="yui-ac-ft"></div>
</div>
<div style="width: 0pt; height: 0pt;" class="yui-ac-shadow"></div>
</div>
</div>
</div>
<br class="clear"/>
<?php } ?>
<?php } ?>
<label for="sltLeaveType"><?php echo $lang_Leave_Common_LeaveType; ?></label>
<select name="sltLeaveType" id="sltLeaveType" class="formSelect" onchange="TotalCountsOfPL(this.value)">
<?php
$skippedLeaveTypesCount = 0;
if (is_array($records[1])) {
foreach ($records[1] as $record) {
$className = get_class($record);
if ($className == 'LeaveQuota') {
if ($record->isLeaveQuotaDeleted()) {
$skippedLeaveTypesCount++;
continue;
}
}
$selected = ($record->getLeaveTypeID() == $prevLeaveType) ? 'selected="selected"' : "";
?>
<option <?php echo $selected;?> value="<?php echo $record->getLeaveTypeID();?>"><?php echo $record->getLeaveTypeName();
?></option>
<?php }
if ($skippedLeaveTypesCount == count($records[1])) { ?>
<option value="-1">-- <?php echo $lang_Error_NoLeaveTypes; ?> --</option>
<?php }
} else { ?>
<option value="-1">-- <?php echo $lang_Error_NoLeaveTypes; ?> --</option>
<?php } ?>
</select>
<br class="clear"/>
<!--Gnanendra Here the Leave date range will get-->
<?php
if (!(is_array($records[1])) && ($modifier == 'Leave_Apply')) { ?>
<div class="notice"><?php echo $lang_Leave_Common_LeaveQuotaNotAllocated; ?></div>
<br class="clear"/>
<?php } ?>
<label for="txtLeaveFromDate"><?php echo $lang_Leave_Common_FromDate; ?><span class="required">*</span></label>
<input name="txtLeaveFromDate" type="text" id="txtLeaveFromDate" size="10"
value="<?php echo $prevLeaveFromDate; ?>" class="formDateInput"/>
<input type="button" name="Submit" value=" " class="calendarBtn" id="btFromDate"/>
<br class="clear"/>
<label for="txtLeaveToDate"><?php echo $lang_Leave_Common_ToDate; ?><span class="required">*</span></label>
<input name="txtLeaveToDate" type="text" id="txtLeaveToDate" size="10"
value="<?php echo $prevLeaveToDate; ?>" class="formDateInput"/>
<input type="button" name="Submit" value=" " class="calendarBtn" id="btToDate"/>
<br class="clear"/>
<div id="trTime1" class="<?php echo $timeElementClass;?>">
<label for="sltLeaveFromTime"><?php echo $lang_Leave_Common_FromTime; ?></label>
<?php $lang_Leave_Common_FromTime="Total Days"; ?>
<label for="sltLeaveFromTime"><?php echo $lang_Leave_Common_FromTime; ?></label>
<select name="sltLeaveFromTime" id="sltLeaveFromTime" onchange="fillTimes();"
class="formTimeSelect">
<option value=""></option>
<!-- It will the time to the drop downlist (00:00 to 23:59) -->
<?php
for ($i=$startTime; $i<=$endTime; $i+=$interval) {
$timeVal = date('H:i', $i);
$selected = ($timeVal == $prevFromTime) ? 'selected="selected"' : "";
?>
<option <?php echo $selected; ?> value="<?php echo $timeVal; ?>" ><?php echo LocaleUtil::getInstance()->formatTime($timeVal);
?></option>
<?php } ?>
</select>
<label for="sltLeaveToTime"><?php echo $lang_Leave_Common_ToTime; ?></label>
<select name="sltLeaveToTime" id="sltLeaveToTime" onchange="fillTimes();"
class="formTimeSelect">
<option value=""></option>
<?php
for ($i=$startTime; $i<=$endTime; $i+=$interval) {
$timeVal = date('H:i', $i);
$selected = ($timeVal == $prevToTime) ? 'selected="selected"' : "";
?>
<option <?php echo $selected; ?> value="<?php echo $timeVal; ?>" ><?php echo LocaleUtil::getInstance()->formatTime($timeVal);
?></option>
<?php } ?>
</select>
<br class="clear"/>
<label for="txtLeaveTotalTime"><?php echo $lang_Leave_Common_TotalHours; ?></label>
<input name="txtLeaveTotalTime" id="txtLeaveTotalTime" size="4" onchange="fillTimes();"
value="<?php echo $prevTotalTime; ?>" class="formInputText" style="width:3em;"/>
<br class="clear"/>
</div>
<div id="trTime2" class="<?php echo $timeElementClass;?>">
</div>
<div id="trTime3" class="<?php echo $timeElementClass;?>">
</div>
<div id="trTime4" class="<?php echo $timeElementClass;?>">
</div>
<br class="clear"/>
<label for="txtComments"><?php echo $lang_Leave_Common_Comment; ?></label>
<textarea name="txtComments" id="txtComments" class="formTextArea" rows="3" cols="20"
><?php echo $prevComments;?></textarea>
<!-- -->
<input name="AjxSelectedLeaveType" type="text" id="AjxSelectedLeaveType" size="30"
value="<?php (isset($_POST['AjxSelectedLeaveType'])) ? $_POST['AjxSelectedLeaveType'] : "Leave Type";?>" class="formDateInput"/>
<input name="AjxTakenLeave" type="text" id="AjxTakenLeave" size="30"
value="<?php (isset($_POST['AjxTakenLeave'])) ? $_POST['AjxTakenLeave'] : "Taken Leave";?>" class="formDateInput"/>
<!-- -->
<br class="clear"/>
<div class="formbuttons">
<input type="button" class="<?php echo $btnClass;?>" id="saveBtn"
onclick="addSave();" onmouseover="moverButton(this);" onmouseout="moutButton(this);"
value="<?php echo $btnTitle;?>" title="<?php echo $btnTitle;?>"/>
</div>
</form>
</div>
</body>
And the Following is getPLCount.php
<?php
$q=$_POST["AjxSelectedLeaveType"];
$Eid=$_SESSION["mySessionVariable"];
$con = mysql_connect('localhost', 'gnuser', 'anu123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("My_Database", $con);
$EmployeeId= $q;
if (isset($q))
{
$query = "select count(leave_request_id) from leave_requests
where leave_type_id='LTY002' AND employee_id=".$q.";";
}
if(isset($Eid))
{
$query = "select count(leave_request_id) from leave_requests
where leave_type_id='LTY002' AND employee_id=".$Eid.";";
}
$result = mysql_query($query);
echo "document.getElementById('AjxTakenLeave').innerHTML={$result}";
mysql_close($con);
?>