繁体   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