I am trying to convert a client’s site from ColdFusion to PHP. I am trying to replicate one of old scripts scripts where the values of a form are inserted into a mysql db. I could not get mine to work so I tried simply echoing a value and found that it was not showing the entire value. For example I typed in 11/13/2011 in one field and Hike in another and the output was 110.
I’m not sure why its storing text as a number. Even if I change it to echo just the Hike it outputs 0. Here is my code:
HTML:
<form action="file.php" method="post">
Event Date: <input type="text" name="eventdate" size="30" />
Theme: <input type="text" name="theme" size="50" />
</form>
PHP:
<?php
$eventdate = $_POST['eventdate'];
$theme = $_POST["theme"];
echo $eventdate+"<br />";
echo $theme+"<br />";
?>
Your problem is that you are adding the fields together, not concatenating with the dot operator:
.PHP will attempt to coerce variables that are not the same type but joined in an operation into types that make sense (for instance, if you’re attempting to add a floating point number to an integer, it will evaluate the integer as a floating point number when performing the operation).
For more information, see the documentation entry for Type Juggling and how PHP Converts Strings to Numbers