簡體   English   中英

勾選復選框后,JavaScript將添加MySQL數據

[英]Javascript add MySQL data when checkbox is ticked

我目前正在使用這個javscript和PHP代碼

<script>
function add(total)
{
    form2.thetotal.value = document.forms["form1" + total].total.value;
}
</script>

<form name="form2">
<table width="800" border="0" cellspacing="0" cellpadding="10" style="position:fixed; z-index:-999; background-color:#FFF;">
  <tr bgcolor="#eeeeee">
    <td>&nbsp;</td>
    <td colspan="2" width="50%"><strong>Total: </strong><input type="text" name="thetotal" id="thetotal" size="20" value="0" /></td>
    <td colspan="2" width="50%"><strong>VAT:</strong> </td>
  </tr>
  <tr bgcolor="#eeeeee">
    <td width="5%">&nbsp;</td>
    <td width="20%"><strong>Invoice Number</strong></td>
    <td width="35%"><strong>Company</strong></td>
    <td width="20%"><strong>Date</strong></td>
    <td width="20%"><strong>Total</strong></td>
  </tr>
  </table>
</form>

<form name="form1">
<table width="800" border="0" cellspacing="0" cellpadding="10">
<?php
$sql="SELECT * from billing_pdf_archive order by invoice_number ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
    $counter++;
    $sql2="SELECT * from customer where sequence = '".$result["customer_sequence"]."' ";
    $rs2=mysql_query($sql2,$conn) or die(mysql_error());
    $result2=mysql_fetch_array($rs2);

    $sql3="SELECT * from reseller where sequence = '".$result["reseller_sequence"]."' ";
    $rs3=mysql_query($sql3,$conn) or die(mysql_error());
    $result3=mysql_fetch_array($rs3);

    if($result["customer_sequence"] != '0')
    {
        $company = $result2["company"];
    }
    elseif($result["reseller_sequence"] != '0')
    {
        $company = '<strong>Reseller: </strong>'.$result3["company"];
    }

    $total = $result["total"];

    echo '<tr>
    <td width="5%"><input type="checkbox" name="check" id="check" onclick=\'add('.$total.');\' /></td>
    <td width="20%">'.$result["invoice_number"].'</td>
    <td width="35%">'.$company.'</td>
    <td width="20%">'.$result["datetime"].'</td>
    <td width="20%">&pound;'.$result["total"].'</td>
  </tr>';
}
?>
</table>
</form>

因此,如您所見,它正在從MySQL數據庫中進行選擇,而我正在嘗試這樣做,因此,當選中其中一個復選框時,它會將總計添加到“總”文本字段中(形式2),但它只是保留了該框為零-關於我能做什么的任何想法?

這是您可以執行的操作(假設total是int而不是float):

<script>
function add(total, this_chk_bx)
{
    //(if its float, use `parseFloat` instead of `parseInt`)

    if(this_chk_bx.checked==true){
    //add if its checked
    var tot_1 = parseInt(form2.thetotal.value);
    form2.thetotal.value = tot_1+parseInt(total);
    }
    else{
    //subtract if its unchecked
    }
}
</script>

在您的php中,您必須像這樣發送復選框: onclick=\\'add('.$total.', this);\\'

您沒有為表格1指定總字段,因此它引發了javascript錯誤,無法正常工作。 看看這個

                            <script>
            function add(total)
            {
                form2.thetotal.value =  total + parseFloat(document.form1.formtotal.value);
            }
            </script>


            <form name="form1">

            <!-- replace field name formtotal with anything that you want-->
            <!-- replace field value with anything that you get from your mysql result-->
            <input type="text" name="formtotal" id="formtotal" value="10" />
            </form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM