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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog