lundi 22 juin 2020

php if statements causing file to write twice

I have a simple form which takes input and performs a maths equation and displays the results. It also takes these input and result, and writes the values to a saved text file. The problem im having is that ive introduced some if statements which is causing the math functions to be run twice per form submission and so the data is being saved to the text file twice per one form submission. Ill post the code and hopefully someone can point out a better way to do things.

<html>

<?php

//variables and result
function bio($height, $weight, $chest, $waist, $hip) {
    $height = floatval($height);
    $weight = floatval($weight);
    $chest = floatval($chest);
$waist = floatval($waist);
$hip = floatval($hip);
$app = ($height+$weight+$chest+$waist+$hip)   ;


$datawrite1 = fopen("data_file_1.txt", "a") or die("Unable to open file!");
        $txt = 
$height.";".   
$weight.";".  
$chest.";".    
$waist.";". 
$hip.";".    
$app."\n";
        fwrite($datawrite1,$txt);
        fclose($datawrite1);

return $app; 

}

?>

<head>
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>

<form action="#result" method="POST" name="form1";">
 <br/>
<label id="label1">height</label>
 <br/>
    <input id="height" name="height" type="number" 
step=".1" max="300"
 onblur="this.style.backgroundColor=/^\d+(?:\.\d{0,1})?$/.test(this.value)?'inherit': '#fec7ce'"
placeholder="height in cm" value="<?php echo isset($_POST['height']) ? $_POST['height'] : '';
?>" />
    <br/>
<label id="label1">weight</label>
    <br/>
    <input id="weight" name="weight" type="number" step=".1"  onblur="this.style.backgroundColor=/^\d+(?:\.\d{0,1})?$/.test(this.value)?'inherit': '#fec7ce'"
placeholder="weight in kgs" value="<?php echo isset($_POST['weight']) ? $_POST['weight'] : '';
 ?>" />
<br/>
<label id="label1">bust / chest</label>
    <br/>
    <input id="chest" name="chest" type="number" step=".1"
 onblur="this.style.backgroundColor=/^\d+(?:\.\d{0,1})?$/.test(this.value)?'inherit': '#fec7ce'" placeholder="chest in cm" value="<?php echo isset($_POST['chest']) ? $_POST['chest'] : ''; ?>" />
<br/>
<label id="label1">waist</label>
    <br/>
    <input id="waist" name="waist" type="number" step=".1"
 onblur="this.style.backgroundColor=/^\d+(?:\.\d{0,1})?$/.test(this.value)?'inherit': '#fec7ce'" placeholder="waist in cm" value="<?php echo isset($_POST['waist']) ? $_POST['waist'] : ''; ?>" />
    <br/>
<label id="label1">hip</label>
    <br/>
    <input id="hip" name="hip" type="number" step=".1"
 onblur="this.style.backgroundColor=/^\d+(?:\.\d{0,1})?$/.test(this.value)?'inherit': '#fec7ce'" placeholder="hip in  cm" value="<?php echo isset($_POST['hip']) ? $_POST['hip'] : ''; ?>" />
    <br/> 
   <br/> 
    <br/>
    <input class="submit" type="submit" value="Calculate" onclick="showDiv()"  id="Login" 
    />

<p id="result">
<?php //handles if empty or 0 input
if 
(!empty($_POST['height']) && !empty($_POST['weight']) && !
 empty($_POST['chest']) && !empty($_POST['waist']) && !empty($_POST['hip'])): 
?>
 <?php 
 if (bio($_POST['height'], $_POST['weight'], $_POST['chest'],  $_POST['waist'], $_POST['hip']) < "16.") 
{
  echo "BMI below 16 (" .bio($_POST['height'], $_POST['weight'], $_POST['chest'],  $_POST['waist'], $_POST['hip'])  .   "), won't proceed"; sleep(3.14);
} 
else 

if (bio($_POST['height'], $_POST['weight'], $_POST['chest'],  $_POST['waist'], $_POST['hip'])  < "2") 
{
  echo "input error detected, please check" ; sleep(3.14);
} 
else 
{
  echo "your score is " . bio($_POST['height'], $_POST['weight'], $_POST['chest'],  $_POST['waist'], $_POST['hip'])  . " % "  ; sleep(3.14);
}
endif;
?>
</p>  

</form>

</body>
</html>

Aucun commentaire:

Enregistrer un commentaire