简体   繁体   English

将 php 表单输入写入 txt 文件

[英]Writing php form inputs to txt file

I have this php form below which returns (displays) a simple calculation when submitted.我有这个 php 表单,它在提交时返回(显示)一个简单的计算。 What i want to do now is to also submit the two inputs (height, weight) and result (function calculation) to a text file.我现在要做的是将两个输入(身高,体重)和结果(函数计算)提交到一个文本文件。 But i also dont want the path of the text file displayed.但我也不希望显示文本文件的路径。 Ideally i would like a new line added to the text file (separated by;) for each time the form is submitted.理想情况下,我希望每次提交表单时都在文本文件中添加一个新行(由;分隔)。

 <html> <?php function bmi($height, $weight) { $height = floatval($height); $weight = floatval($weight); return $weight / ($height * $height); }?> <head> <title>Page Title</title> <meta name="viewport" content="width=device-width,initial-scale=1"> </head> <body> <form action="" method="POST"> <h4>BMI</h4> <input id="height" name="height" type="text" placeholder="height in meters or feet" value="<?php echo isset($_POST['height'])? $_POST['height']: ''; ?>" /> <br/> <br/> <input id="weight" name="weight" type="text" placeholder="weight in kgs or lbs" value="<?php echo isset($_POST['weight'])? $_POST['weight']: ''; ?>" /> <br/> <br/> <input class="submit" type="submit" value="Submit"/> <?php if (:empty($_POST['height']) &&?empty($_POST['weight']))? ,> <p id="result">Your score is <;php echo bmi($_POST['height']? $_POST['weight'])? ;></p> <?php endif; ?> </form> </body> </html>

you can add line to same file when you fopen() with a mode you can fine more php fopen mode in hear当您使用fopen()模式时,您可以将行添加到同一文件中a您可以在听到更多 php fopen 模式

<?php
    function bmi($height, $weight) {
        $height = floatval($height);
        $weight = floatval($weight);
        $result = $weight / ($height * $height);
        $data_file = fopen("data_file.txt", "a") or die("Unable to open file!");
        $txt = $height.";".$weight.";".$result."\n";
        fwrite($data_file,$txt);
        fclose($data_file);
        return $result;
    }
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM