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条)

报告相同问题?

悬赏问题

  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿