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 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密