I tried to make my textbox type in currency format. I did it, but when I insert the value it into my database the value changes. When I type 10000, it changes 10,000. (So far so good.) But when I insert that value into database the value changes to 10. I’m really confused.
Here is my script. I used php, mysql, and the field type is float
<?php include('header.php'); ?>
<?php
if(isset($_POST['simpan']))
{
$nama = htmlentities(addslashes($_POST['nama']));
$sim_wajib = $_POST['sim_wajib'];
$sim_pokok = $_POST['sim_pokok'];
$sim_sukarela = $_POST['sim_sukarela'];
$sim_wpinjam = $_POST['sim_wpinjam'];
$shu = $_POST['shu'];
$total = $sim_wpinjam + $shu;
if($nama != '' && $sim_wajib != '' && $sim_pokok != '' && $sim_sukarela != '' && $sim_wpinjam != '' && $shu != '' )
{
$sql = "INSERT INTO simpanan_tbl";
$sql .= "(nama, sim_wajib, sim_pokok, sim_sukarela, sim_wajibpinjam, shu, jumlah)";
$sql .= "VALUES('$nama','$sim_wajib','$sim_pokok','$sim_sukarela','$sim_wpinjam','$shu','$total')";
$query = mysql_query($sql) or die(mysql_error());
echo '<script type="text/javascript">alert("Data telah masuk!");document.location="manage_simpanan.php";</script>';
}
else
{
echo '<script type="text/javascript">alert("Kolom tidak boleh ada yang kosong!");</script>';
}
}
?>
<div id="rounded_add">
<script type="text/javascript">
function numberFormat(nr)
{
//remove the existing
var regex = /,/g;
nr = nr.replace(regex,'');
//split it into 2 parts
var x = nr.split(',');
var p1 = x[0];
var p2 = x.length > 1 ? ',' + x[1] : '';
//match group of 3 numbers (0-9) and add ',' between them
regex = /(\d+)(\d{3})/;
while(regex.test(p1))
{
p1 = p1.replace(regex, '$1' + ',' + '$2');
}
//join the 2 parts and return the formatted number
return p1 + p2;
}
</script>
<form method="post" name="form1">
<table id="add_simpan" class="add">
<tr>
<td>No Anggota</td>
</tr>
<tr>
<td><input type="text" id="" name="" class="textbox"></td>
</tr>
<tr>
<td>Nama</td>
</tr>
<tr>
<td><input type="text" id="nama" name="nama" class="textbox_left"></td>
</tr>
<tr>
<td>Simpanan Wajib</td>
</tr>
<tr>
<td><input type="text" id="sim_wajib" name="sim_wajib" class="textbox"></td>
</tr>
<tr>
<td>Simpanan Pokok</td>
</tr>
<tr>
<td><input type="text" id="sim_pokok" name="sim_pokok" class="textbox"></td>
</tr>
<tr>
<td>Simpanan Suka Rela</td>
</tr>
<tr>
<td><input type="text" id="sim_sukarela" name="sim_sukarela" class="textbox"></td>
</tr>
<tr>
<td>Simpanan Wajib Pinjam</td>
</tr>
<tr>
<td><input type="text" id="sim_wpinjam" name="sim_wpinjam" class="textbox" onkeyup="this.value = numberFormat(this.value);"></td>
</tr>
<tr>
<td>SHU</td>
</tr>
<tr>
<td><input type="text" id="shu" name="shu" class="textbox" onkeyup="this.value = numberFormat(this.value);"></td>
</tr>
<tr>
<td><input type="submit" value="Simpan" id="simpan" name="simpan" class="button blues"></td>
</tr>
</table>
</form>
</div><!-- END OF ROUNDED_ADD -->
<?php include('footer.php'); ?>
If you have to have accounting style formatting within your text boxes, I would suggest having a hidden field with the raw numeric value alongside each textbox. You could easily add a few lines to the js function right after the “nr = nr.replace(regex,”);” section to copy the now stripped value to the hidden field. To do this, you would have to name each textbox and pass the name to the numberFormat function so that it saves it to the correct hidden input. Then, when the form is submitted, save the values from the hidden fields, not the open textboxes.
Alternately, have your Submit button trigger a function to sweep through all your text fields and remove any formatting, then submit the form as part of the function.
By the way, inserting 10,000 as ten thousand into a float field will trim it to 10. Unless you are using a comma as a decimal point (which I think the French do?) you are saving incorrect data.