I have a small project I have been working on. I had to create a area conversion calculator using one file or PHP_SELF. The problem I am having is the form is working and outputs the number, but it will only ouput the last if statement no matter what conversion method I pick. It will always output Sq.Miles.
if (!empty($_POST['con'])) {
$con = $_POST['con'];
} else {
$error .= "You need to select a conversion Method. <br>";
}
if (!empty($_POST['number'])) {
$num = $_POST['number'];
} else {
$error .= "You need to type in a number. <br>";
}
if (empty($error)) {
if ($con = "SFSM") {
$result = $num * 0.093 . " Sq. Meters";
}
if ($con = "SYSM") {
$result = $num * 0.84 . " Sq. Meters";
}
if ($con = "SMSK") {
$result = $num * 2.6 . " Sq. Kiometers";
}
if ($con = "SMSF") {
$result = $num * 10.76 . " Sq. Feet";
}
if ($con = "SMSY") {
$result = $num * 1.2 . " Sq. Yards";
}
if ($con = "SKSM") {
$result = $num * 0.38 . " Sq. Miles";
}
}
}
?>
<form method="post" action="area2.php">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">Area Conversions</td>
</tr>
<tr>
<td>Conversion Method</td>
<td>Number</td>
</tr>
<tr>
<td>
<select name="con">
<option selected="selected" style="color:#CCC"></option>
<option value="SFSM">Square Feet to Square Meters</option>
<option value="SYSM">Square Yards to Square Meters</option>
<option value="SMSK">Square Miles to Square Kilometers</option>
<option value="SMSF">Square Meters to Square Feet</option>
<option value="SMSY">Square Meters to Square Yards</option>
<option value="SKSM">Square Kilometers to Square Miles</option>
</select>
</td>
<td>
<input type="text" name="number" size="10" value = "<?php if ($num){ echo $num; } else { echo "Insert Number"; } ?>" onfocus="this.value==this.defaultValue?this.value='':null"/><br />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Calculate" name="submit" />
<input type="reset" value="reset" name="reset" />
</td>
</tr>
<?php
if (!empty($error)) {
echo $error;
}elseif (!empty($result)) {
echo "<tr><td colspan=\"2\">Result: " . $result . " </td></tr>";
}
?>
</table>
</form>
Try:
The
=operator is assignment;==is comparison.Essentially, your if statements are all evaluating to true; and since they all set the variable, it’s only the last call that’s actually appearing