douhui9380 2017-05-09 06:55
浏览 91
已采纳

尝试通过表单输入在PHP中写入.txt时出现空白文件

I cannot seem to figure out why my order.txt file is blank despite seemingly having my logic correct. I'm new to PHP, so I apologize if I have obvious syntax errors.

I'm attempting to log the user input values of a form to a .txt file once the submit button is clicked. Here is my code: NOTE: both my form and php script are in the same file, so the action is referring to itself.

HTML:

<form action="p1_checkout.php" method = "POST">
      First name:<br>
      <input type="text" name="firstname"><br>
      Last name:<br>
      <input type="text" name="lastname"><br>
      Street Address:<br>
      <input type="text" name="streetaddress"><br>
      City:<br>
      <input type="text" name="city"><br>
      State:<br>
      <input type="text" name="state"><br>
      Zip Code:<br>
      <input type="text" name="zipcode"><br>
      Phone Number:<br>
      <input type="text" name="phonenumber"><br>
      Order Quantity:<br>
      <input type="text" name="orderquantity"><br>
      <input type="submit" value="Checkout">
</form>

PHP:

       <?php
            $newfile = fopen("order.txt", "w+");
            $fproduct = "Product: Mount Diablo, California Print";
            $fqty = "Quantity Ordered: ".$_POST['quantityordered'];
            $fname = "Person Billed: ".$_POST['firstname'];
            $faddress = "Address: ".$_POST['streetaddress'] + "," + $_POST['city'] + "," + $_POST['state'] + "," + $_POST['zipcode'];
            $fcontent = $fproduct + $fqty + $fname + $faddress;
            fwrite($newfile, $content);
            fclose($newfile);
        ?>
  • 写回答

4条回答 默认 最新

  • drbae3964 2017-05-09 07:05
    关注

    You try to use arithmetic operators

    You need to concatenate vars, not add them.

    this :

     $fcontent = $fproduct + $fqty + $fname + $faddress;
    

    should be:

     $fcontent = "$fproduct $fqty $fname $faddress";
    

    then write content to file:

    fwrite($newfile, $fcontent); // you have typo, missing `f`
    

    Make sure all $_POST values are ok though

    EDIT: make sure $_POST values are correctly spelled : you use different name in form and in post result ->

    $_POST['quantityordered'] != name="orderquantity" (in form)
    

    and get rid of all arithmetic operators !

    $faddress = "Address: ".$_POST['streetaddress'].", ".$_POST['city'].",". $_POST['state'] ."," .$_POST['zipcode'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决