2012-11-27 239 views
0

我在下面的代碼中有一個XMLHttpRequest問題,它不能按需要工作。有人可以幫忙!XMLHttpRequest不能正常工作

簡而言之:該程序正在接受來自用戶的價值,它應該更新RATEMASTER_draft表。如果用戶按F5進行刷新,它仍然保存這些值,爲此我再次讀取該表並再次將值插入到輸入框中。

但由於某些原因,onchange數據沒有更新表。

<?php include 'accesscontrol.php'; ?> 

<?php 
$dbservertype='mysql'; 
$servername='localhost'; 
    $dbusername='svga'; 
$dbpassword='[email protected]#'; 
$dbname='svga3'; 


connecttodb($servername,$dbname,$dbusername,$dbpassword); 
function connecttodb($servername,$dbname,$dbuser,$dbpassword) 
{ 
global $link; 
$link=mysql_connect ("$servername","$dbuser","$dbpassword"); 
if(!$link){die("Could not connect to MySQL");} 
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); 
} 
//////// End of connecting to database //////// 
?> 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head id="Head1" runat="server"> 
<title>RATEMASTER</title> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
<script type="text/javascript"> 
    $(function() { 
    $("form:not(.filter) :input:visible:enabled:first").focus(); 
    }); 
</script> 


<script type="text/javascript"> 

function showUser(str) 
{ 

if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET","rateaccode.php?q1=" + str,false); 
xmlhttp.send(); 
} 


function showprodes(str2) 
{ 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET","rateprod.php?q2=" + str2,true); 
xmlhttp.send(); 
} 


function showperltr(str3) 
{ 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET","rateperltr.php?q3=" + str3,true); 
xmlhttp.send(); 
} 


function showperno(str4) 
{ 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET","rateperno.php?q4=" + str4,true); 
xmlhttp.send(); 
} 


function showpercu(str5) 
{ 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET","ratepercu.php?q5=" + str5,true); 
xmlhttp.send(); 
} 

function showperkg(str6) 
{ 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else  
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET","rateperkg.php?q6=" + str6,true); 
xmlhttp.send(); 
} 


function showexcise(str7) 
{ 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET","rateexcise.php?q7=" + str7,true); 
xmlhttp.send(); 
} 


function showcess(str8) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratecess.php?q8=" + str8,true); 
xmlhttp.send(); 
} 


function showedu(str9) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","rateedu.php?q9=" + str9,true); 
xmlhttp.send(); 
} 


function showfreight(str10) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratefreight.php?q10=" + str10,true); 
xmlhttp.send(); 
} 


function showcoll(str11) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratecoll.php?q11=" + str11,true); 
xmlhttp.send(); 
} 


function showloading(str12) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","rateloading.php?q12=" + str12,true); 
xmlhttp.send(); 
} 


function showunloading(str13) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","rateunloading.php?q13=" + str13,true); 
xmlhttp.send(); 
} 


function showtransport(str14) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratetransport.php?q14=" + str14,true); 
xmlhttp.send(); 
} 


function showvat(str15) 
{ 
alert ("vat"); 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratevat.php?q15=" + str15,false); 
xmlhttp.send(); 
} 



function showcst(str16) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratecst.php?q16=" + str16,true); 
xmlhttp.send(); 
} 

function showcstwithout(str17) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratecstwithout.php?q17=" + str17,true); 
xmlhttp.send(); 
} 

function showtariff(str18) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratetariff.php?q18=" + str18,true); 
xmlhttp.send(); 
} 

function showbal(str19) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratebal.php?q19=" + str19,true); 
xmlhttp.send(); 
} 

function showdesc(str20) 
{ 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.open("GET","ratedesc.php?q20=" + str20,true); 
xmlhttp.send(); 
} 

//---------------------------------------------------- 

</script> 


<style type="text/css"> 
<!-- 
.sty1 { 
    font-family: Verdana, Arial, Helvetica, sans-serif; 
    font-size: 12px; 
    font-weight: bold; 
} 
--> 
</style> 

</head> 


<body onload="document.f1.vat.focus();"> 
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#026465"> 
    <tr> 
    <td><div align="center"><span class="style1"> Amogh Gases Private Ltd <br></span> 
<center class="style5">RATEMASTER - SUPPLEMENTARY INV</center> 
<br> 

<table width="603" border="0" cellpadding="0" cellspacing="0" class="wrapper"> 
    <tr> 
    <td height="30"><img src="images/bar1.jpg" width="39" height="30" hspace="0" vspace="0" border="0" align="top" /></td> 
<td height="30"><a class="MenuBarItemSubmenu" href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image30','','images/bar2mas_top.jpg',1)"><img src="images/bar2mas.jpg" alt="Master" name="Image30" width="71" height="30" hspace="0" vspace="0" border="0" align="top" id="Image30"/></a></td> 
<td height="30"><img src="images/bar3.jpg" width="33" height="30" hspace="0" vspace="0" border="0" align="top" /></td> 
<td height="30"><a class="MenuBarItemSubmenu" href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image31','','images/bar4sale_top.jpg',1)"><img src="images/bar4sale.jpg" alt="Sales" name="Image31" width="60" height="30" hspace="0" vspace="0" border="0" align="top" id="Image31"/></a></td> 
<td height="30">  <a href="#"><img src="images/bar5.jpg" width="34" height="30" hspace="0" vspace="0" border="0" align="top" /></a></td> 
<td height="30">  <a class="MenuBarItemSubmenu" href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image32','','images/bar6purch_top.jpg',1)"><img src="images/bar6purch.jpg" alt="Sales" name="Image32" width="101" height="30" hspace="0" vspace="0" border="0" align="top" id="Image32"/></a> </td> 
<td height="30">  <img src="images/bar7.jpg" width="33" height="30" hspace="0" vspace="0" border="0" align="top" /></td> 
<td height="30">  <img src="images/bar8repo.jpg" width="81" height="30" hspace="0" vspace="0" border="0" align="top" /></td> 
<td height="30">  <img src="images/bar9.jpg" width="33" height="30" hspace="0" vspace="0" border="0" align="top" /></td> 
<td height="30">  <img src="images/bar10util.jpg" width="80" height="30" hspace="0" vspace="0" border="0" align="top" /></td> 
<td height="30">  <img src="images/bar11.jpg" width="38" height="30" hspace="0" vspace="0" border="0" align="top" /></td> 
    </td> 
    </tr> 
</table> 
<span class="style7">.</span></div> 
</td> 
    </tr> 
</table> 

<? 
    $result = mysql_query("SELECT Ac_code, Ac_desc FROM ACMAST ORDER BY Ac_desc"); 
    $result2 = mysql_query("SELECT Prod_desc FROM PRODMAST ORDER BY Prod_desc"); 


//--Table RATEMASTER_draft values------ 
$res=mysql_query("select * from RATEMASTER_draft WHERE usr='$user'"); 
$row = mysql_fetch_assoc($res); 

$acode=$row['Ac_code']; 
$prodesc=$row['Prod_desc']; 
$rateno=$row['Rate_perno']; 
$rateltr=$row['Rate_perltr']; 
$ratecumtr=$row['Rate_percumtr']; 
$ratekg=$row['Rate_perkg']; 
$exc=$row['Excise_rate']; 
$ces=$row['Cess_Excise']; 
$edu=$row['Edu_cess']; 
$fre=$row['Freight']; 
$load=$row['Loading_chrgs']; 
$unload=$row['Unloading_chrgs'];  

$cname=mysql_query("select Ac_desc from ACMAST WHERE Ac_code='$acode'"); 
$cname2=mysql_fetch_assoc($cname); 
$cname3=$cname2['Ac_desc']; 




echo "<form name='f1'>"; 
echo "<table width='730' border='0' align='center' cellpadding='0' cellspacing='1'>"; 
echo " <tr>"; 
echo " <td width='109' height='40'><span class='style3'>Customer </span></td>"; 

echo " <td colspan='4'>"; 
echo "<select name='Accode' onchange=\"showUser(this.value);\"><option value='$acode'>$cname3</option>"; 
    while($nt=mysql_fetch_assoc($result)) 
    { //Array or records stored in $nt 
    echo "<option value=$nt[Ac_code]>$nt[Ac_desc]</option>"; 
    } 
    echo "</select>";// Closing of list box 
echo "</td>"; 

echo " <td colspan='3'>"; 
    echo " <span class='style3'>Gas Type &nbsp;</span> <select name='Proddesc' onchange=\"showprodes(this.value);\"><option value='$prodesc'>$prodesc</option>"; 
    while($nt2=mysql_fetch_assoc($result2)) 
    {//Array or records stored in $nt 
    echo "<option value='$nt2[Prod_desc]'>$nt2[Prod_desc]</option>"; 
    /* Option values are added by looping through the array */ 
    } 
    echo "</select>";// Closing of list box 

echo " </td>"; 
echo " </tr>"; 
echo " <tr>"; 
echo " <td><span class='style3'>Rate per no</span></td>"; 
echo " <td width='76' height='40'><input name='perno' type='text' size='6' maxlength='6' value='$rateno' onchange=\"showperno(this.value);\"></td>"; 
echo " <td width='96'><span class='style3'>Rate per ltr</span></td>"; 
echo " <td width='81'><input name='perltr' type='text' size='6' maxlength='6' value='$rateltr' onchange=\"showperltr(this.value);\"></td>"; 
echo " <td width='111'><span class='style3'>Rate per cu mtr</span></td>"; 
echo " <td width='77'><input name='percu' type='text' size='6' maxlength='6' value='$ratecumtr' onchange=\"showpercu(this.value);\"></td>"; 
echo " <td width='117'><span class='style3'>Rate per kg</span></td>"; 
echo " <td width='54'><input name='perkg' type='text' size='6' maxlength='6' value='$ratekg' onchange=\"showperkg(this.value);\"></td>"; 
echo " </tr>"; 
echo " <tr>"; 
echo " <td height='40'><span class='style3'>Excise rate</span></td>"; 
echo " <td><input name='excise' type='text' size='6' maxlength='6' value='$exc' onchange=\"showexcise(this.value);\"></td>"; 
echo " <td><span class='style3'>Cess Excise</span></td>"; 
echo " <td><input name='cess' type='text' size='6' maxlength='6' value='$ces' onchange=\"showcess(this.value);\"></td>"; 
echo " <td><span class='style3'>Edu cess</span></td>"; 
echo " <td><input name='edu' type='text' size='6' maxlength='6' value='$edu' onchange=\"showedu(this.value);\"></td>"; 
echo " <td><span class='style3'>Freight</span></td>"; 
echo " <td><span class='style3'><input name='freight' type='text' size='6' maxlength='6' value='$fre' onchange=\"showfreight(this.value);\"></span></td>"; 
echo " </tr>"; 
echo " <tr>"; 
//echo " <td height='40'><span class='style3'>Collection chrgs</span></td>"; 
//echo " <td><input name='coll' type='text' size='6' maxlength='6' onchange=\"showcoll(this.value);\"></td>"; 
echo " <td><span class='style3'>Loading chrgs</span></td>"; 
echo " <td><input name='loading' type='text' size='6' maxlength='6' value='$load' onchange=\"showloading(this.value);\"></td>"; 
echo " <td><span class='style3'>Unloading chrgs</span></td>"; 
echo " <td><input name='unloading' type='text' size='6' maxlength='6' value='$unload' onchange=\"showunloading(this.value);\"></td>"; 
// echo " <td><span class='style3'>Transport chrgs</span></td>"; 
// echo " <td><input name='transport' type='text' size='6' maxlength='6' onchange=\"showtransport(this.value);\"></td>"; 
echo " </tr>"; 

echo " <tr>"; 
echo " <td><span class='style4'>Suplimentry Invoice</span></td>"; 

echo " <td>"; 
echo "<select name='supli'><option value='1'>Yes</option>"; 
     echo "</select>";// Closing of list box 
echo "</td>"; 

echo " <td height='40'><span class='style3'>VAT</span></td>"; 
echo " <td><input name='vat' type='text' size='6' maxlength='6' onchange=\"showvat(this.value);\"></td>"; 
echo " <td><span class='style3'>CST with CForm</span></td>"; 
echo " <td><input name='cst' type='text' size='6' maxlength='6' onchange=\"showcst(this.value);\"></td>"; 
echo " <td><span class='style3'>CST without CForm</span></td>"; 
echo " <td><input name='cstc' type='text' size='6' maxlength='6' onchange=\"showcstwithout(this.value);\"></td>"; 
echo " </tr>"; 

echo " <tr>"; 
echo " <td height='40'><span class='style4'>Balance Rate</span></td>"; 
echo " <td><input name='balrate' type='text' size='6' maxlength='6' onchange=\"showbal(this.value);\"></td>"; 
echo " <td><span class='style4'>Description</span></td>"; 
echo " <td colspan='5'><input name='desc' type='text' size='70' maxlength='200' value='Transportation and Other Misc Expenses incurred against original Invoice No: ' onchange=\"showdesc(this.value);\"></td>"; 
echo " </tr>"; 


echo "</table>"; 

echo "</form>"; 
?> 


</body> 
</html> 
+1

echo「..」;回聲「..」;回聲「..」;傷害我的感受。 :( – Popnoodles

+0

當你按CTRL + f5時,會發生什麼?瀏覽器緩存東西 – Popnoodles

+0

現在,如果我按CTRL + F5或F5,它不會在輸入框中保留任何值,因爲它不會在表中寫入任何內容,通過XMLHttprequest。 – user1114409

回答

0

交換手段;在元素失去焦點後以另一種方式「改變完成後」。

只有在完成選擇onchange後纔會觸發。

如果你想要做的,從鍵盤只是選擇項有向上和向下選擇(ENTER),然後使該元素按TAB

失去焦點並且,爲了防止AJAX調用到緩存添加一個唯一的時間戳數據到網址 您可以通過更改這樣的行來完成;

xmlhttp.open("GET","ratecst.php?q16=" + str16 +"&ts="+new Date().getTime(),true); 
+1

謝謝你現在在工作,只是增加這條線。 – user1114409

0

你可能誤解了onChange-Function的含義。只要你點擊字段外部,而不是當按下鍵時,它就會被調用。

這將寧可是函數onKeyUp我會說。

+0

我的onChange的目的是,使用這個程序的用戶在輸入文本框中的值後用於按TAB鍵,因此我必須使用這個。 – user1114409