1

I have records which column name is counter. And the value is like 000003, 000012, and so on. But my problem is, when I try to post it the value that receive in the form action url change, I receive only 3 which is the real value is 000003. Why is that? I echo POST value in my second page.

<?php
$mysqli = new mysqli("localhost", "root", "", "app");
$result1 = $mysqli->query("
    SELECT *
    FROM app 
");
echo'<table border=1>
        <thead>
    <tr>
        <th>cc</th>
    </tr>
        </thead>';
        echo'<tbody>';
while($row = $result1->fetch_assoc()){
echo'   <tr>';
            echo "<td><a href='#' onclick='javascript:postIt(".$row['counter'].");'>".$row['counter']."</a></td> ";
        echo'</tr>';
    }
    echo "</tbody></table>";

?>
<script>
 function postIt(value){
   document.forms[0].id.value = value;
   document.forms[0].submit();
 }
</script>
<form name="blah" action="check.php" method="post">
<input type="hidden" name="id">
</form>

Why 000003 become only 3, or 000172 become 172. Help?

surname
  • 163
  • 1
  • 1
  • 17

3 Answers3

0

Because you are working with integers, not string. If you want to keep them as strings, meaning, with the leading zeros, convert the values to string type.

Peon
  • 7,902
  • 7
  • 59
  • 100
0

Why is this a problem? At the receiving end you can simply pad the number back out how you want it to.

It seems like a counter should be an integer. You can then pad it out or not if and when you want to.

If you really want to you can pad it out before you send it.

See here: How can I pad a value with leading zeros?

Community
  • 1
  • 1
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
0

this may work

echo "<td><a href='#' onclick='javascript:postIt(\"".$row['counter']."\");'>".$row['counter']."</a></td> ";
SHIN
  • 386
  • 2
  • 13
  • it works, but would explain the function of \ that u add in the script? – surname Nov 26 '13 at 15:34
  • in javascript function(0003) means function(3) but you need function("0003"). so i added ". but you already has " to handle string i need to escape it so put \" – SHIN Nov 26 '13 at 15:36