2012-12-26 61 views
0

我有一個表單提交,它將表單數據張貼到一個名爲invoice-detail.php的文件。在同一個實例中,我想調用一個函數來處理少量值。我曾嘗試過,但onClick不起作用。提交表單與onclick事件不起作用

請檢查下面的代碼,建議我如何去執行在同一個實例的兩個動作。

<?php include 'db.php'; ?> 
<html> 
<head> 
<title>INVOICE Header</title> 


<SCRIPT language=JavaScript> 

function showda() 
{ 
alert ("Show function !"); 
} 

function showtin(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","invtin.php?q8=" + str8,true); 
xmlhttp.send(); 
} 

function showcst(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","invcst.php?q10=" + str10,true); 
xmlhttp.send(); 
} 

</script> 


</head> 
<body> 

    <?php 

echo "<form name='form1' method='post' target='mainFrame' action='invoice-detail.php'>"; 
echo "<table width='829' height='100' border='0' align='center' cellpadding='0' cellspacing='2'>"; 

echo "<tr>"; 
echo "<td height='39' width='99' class='sty1'>Invoice No</td>"; 

$res=mysql_query("select * from INVHDR_draft WHERE usr='$user'"); 
$row = mysql_fetch_assoc($res); 
$tincst=mysql_query("SELECT TIN_No, CST_No, ECC_No FROM ACMAST where Ac_code='$cat'"); 
$tincst2=mysql_fetch_assoc($tincst); 

echo "<td width='140'><input name='Inv_no' type='text' size='6' maxlength='6' value='$row[Invno]' onBlur=\"if (this.value == '' || this.value == 0) {alert('Invoice No. is required'); f1.Inv_no.focus();}\" onchange=\"showinvno(this.value);\"></td>"; 
echo "</tr>"; 

echo "<tr>"; 
echo "<td height='39' width='99' class='sty1' valign='bottom'>TIN</td>"; 
echo "<td width='140' valign='bottom'><input name='tin' type='text' size='15' maxlength='15' value='$tincst2[TIN_No]' onchange=\"showtin(this.value);\"></td>"; 

echo "<td width='203' class='sty1' valign='bottom'>CST "; 
echo " <input name='cst' type='text' size='15' maxlength='15' value='$tincst2[CST_No]' onchange=\"showcst(this.value);\"></td>"; 
echo "<td width='150' class='sty1' valign='bottom'>ECC No. "; 
echo " <input name='ecc' type='text' size='15' maxlength='15' value='$tincst2[ECC_No]' onchange=\"showcst(this.value);\"></td>"; 
echo "</tr>"; 
echo "</table>"; 

echo " <input type='submit' name='Submit' value='Process!' onClick=\"'showda();\">"; 

echo "</form>"; 

?> 
</body> 
</html> 
+0

什麼錯誤,現在顯示。如果您不確定錯誤,請在控制檯 – polin

+0

@Pankit Kapadia中的Firebug/developers工具中查看它現在沒有顯示任何錯誤,它只是將值傳遞給invoice-detail.php。 onclick不工作,它不是調用函數 – user1114409

+0

@ user1114409 - 嗨,我剛編輯你的問題,因爲它沒有格式化。我沒有回答。 –

回答

0
<?php include 'db.php'; ?> 
    <html> 
    <head> 
    <title>INVOICE Header</title> 


    <SCRIPT language=JavaScript> 

    function showda() 
    { 
    alert ("Show function !"); 
    ////////do your processing here before submit////// 

    document.form1.action="invoice-detail.php"; 
    document.form1.submit(); 
    } 

    function showtin(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","invtin.php?q8=" + str8,true); 
    xmlhttp.send(); 
    } 

    function showcst(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","invcst.php?q10=" + str10,true); 
    xmlhttp.send(); 
    } 

    </script> 


    </head> 
    <body> 

     <?php 

    echo "<form name='form1' method='post' target='mainFrame' action=''>"; 
    echo "<table width='829' height='100' border='0' align='center' cellpadding='0' cellspacing='2'>"; 

    echo "<tr>"; 
    echo "<td height='39' width='99' class='sty1'>Invoice No</td>"; 

    $res=mysql_query("select * from INVHDR_draft WHERE usr='$user'"); 
    $row = mysql_fetch_assoc($res); 
    $tincst=mysql_query("SELECT TIN_No, CST_No, ECC_No FROM ACMAST where Ac_code='$cat'"); 
    $tincst2=mysql_fetch_assoc($tincst); 

    echo "<td width='140'><input name='Inv_no' type='text' size='6' maxlength='6' value='$row[Invno]' onBlur=\"if (this.value == '' || this.value == 0) {alert('Invoice No. is required'); f1.Inv_no.focus();}\" onchange=\"showinvno(this.value);\"></td>"; 
    echo "</tr>"; 

    echo "<tr>"; 
    echo "<td height='39' width='99' class='sty1' valign='bottom'>TIN</td>"; 
    echo "<td width='140' valign='bottom'><input name='tin' type='text' size='15' maxlength='15' value='$tincst2[TIN_No]' onchange=\"showtin(this.value);\"></td>"; 

    echo "<td width='203' class='sty1' valign='bottom'>CST "; 
    echo " <input name='cst' type='text' size='15' maxlength='15' value='$tincst2[CST_No]' onchange=\"showcst(this.value);\"></td>"; 
    echo "<td width='150' class='sty1' valign='bottom'>ECC No. "; 
    echo " <input name='ecc' type='text' size='15' maxlength='15' value='$tincst2[ECC_No]' onchange=\"showcst(this.value);\"></td>"; 
    echo "</tr>"; 
    echo "</table>"; 

    echo " <input type='submit' name='Submit' value='Process!' onClick='showda(); >"; 

    echo "</form>"; 

    ?> 
    </body> 
    </html> 

使用此代碼

+0

非常感謝您的幫助,這工作得很好,但我有另一個問題,Onclick我需要從窗體「Inv_no」傳遞一個值到javascript,我可以使用另一個函數做到這一點? – user1114409

+0

沒關係,你可以做到這一點告訴我你想傳遞哪個值,以及在哪個函數的JavaScript –

1

你可以叫onclick這個代碼後做一些代碼,您可以撥打formObject.submit()

function showda() 
{ 
    alert ("Show function !"); 
    formObject.submit(); //get your form object by id or tagname 
} 
1

,而不是添加onclick到輸入的添加onsubmit到表單元素

echo "<form name='form1' method='post' target='mainFrame' action='invoice-detail.php'> onClick=\"'return showda();\">";

然後你可以更新你的showda()方法:

function showda() 
{ 
    alert ("Show function !"); 
    // do your actions here 
    // if they fail you can return false 
    return true; 
}