donglu9872 2016-01-07 03:37
浏览 114

将php中的输入数据保存到notepad.txt

<form id="form" name="form" method="post" action="">
    <div class="jquery-script-clear"></div>
    <h1>BARCODE GENERATOR</h1>
    <div id="generator"> Please fill in the code :
        <input  type="text" name="barcodeValue" id="barcodeValue" value="1234"><br>    <br>
    </div>

    <div id="submit">
        <input type="button" onclick="generateBarcode();"     value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Generate &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;">         <input type="button" onclick="printDiv('print')" value="Print" />
    </div>
</form>

<?php
$barcodeValue = $_POST["barcodeValue"];
$save = file_get_contents("save.txt");
$save = "$barcodeValue" . $save;
file_put_contents("save.txt", $save);
echo $save;
?>

sample picture

How to save the input data in save.txt file. When i clicked generate button the text file not showing in same folder.

  • 写回答

1条回答 默认 最新

  • duaj39673 2016-01-07 04:03
    关注

    The problem with your code is you have no submit button so your form was not actually posting when you pressed the button. if you look at my edits you can see I changed the button from input type="button" to type="submit". That allows for the form to submit back to the same php script.

    Your script also was causing errors because you accessed $_POST["barcodeValue"] without checking if it existed. You also have to check if the save.txt exists before reading from it. If analyze my edits you can see how checking if the variables are available will help quite a bit.

    <form id="form" name="form" method="post" action="">
        <div class="jquery-script-clear"></div>
        <h1>BARCODE GENERATOR</h1>
        <div id="generator"> Please fill in the code :
            <input  type="text" name="barcodeValue" id="barcodeValue" value="1234"><br>    <br>
        </div>
    
        <div id="submit">
            <input type="submit"   value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Generate &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;">
        </div>
    </form>
    
    <?php
    
    
        if(isset($_POST["barcodeValue"]))
        {
            $barcodeValue = $_POST["barcodeValue"];
            if(file_exists("save.txt"))
                $save = file_get_contents("save.txt");
            else
                $save = "";
            $save = $barcodeValue . $save;
            file_put_contents("save.txt", $save);
            echo $save;
        }
    
    
    ?>
    

    Let me know if you need more help

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数