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

时间:2017-05-09 06:55:05

标签: php forms

我似乎无法弄清楚为什么我的order.txt文件是空白的,尽管看似我的逻辑是正确的。我是PHP的新手,所以如果我有明显的语法错误,我会道歉。

单击提交按钮后,我尝试将表单的用户输入值记录到.txt文件中。这是我的代码:注意:我的表单和php脚本都在同一个文件中,因此操作指的是自己。

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 个答案:

答案 0 :(得分:2)

     <?php
 if(isset($_POST) && !empty($_POST)){
    $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;
    $fp = fopen("order.txt", 'w');
    fwrite($fp, $fcontent);
    fclose($fp);
 }
?>

答案 1 :(得分:2)

嘿,你做的是轻微的错误...... 如果你在同一个文件中使用PHP .. 因此,无需提供文件名,请将其留空....

并且在php中连接我们不使用

+ sign we use the DOT (.) sign...

这是代码..试试吧....

    <form action="" 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" name="submit">
</form>


<?php
extract($_POST);
if(isset($submit))
{
$newfile = fopen("order.txt", "w+");
$fproduct = "Product: Mount Diablo, California Print";
$fqty = "Quantity Ordered: ".$orderquantity;
$fname = "Person Billed: ".$firstname;
$faddress = "Address: ".$streetaddress . "," . $city . "," . $state . "," . $zipcode;
$fcontent = $fproduct . $fqty . $fname . $faddress;
fwrite($newfile, $fcontent);
fclose($newfile);
}     
?>

答案 2 :(得分:2)

<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" name="Checkout">
</form>

PHP:

        <?php
          if(isset($_POST['Checkout']))
        {

        $newfile = fopen("order.txt", "w+");
        $fproduct = "Product: Mount Diablo, California Print ";
        $fqty = "Quantity Ordered: ".$_POST['orderquantity'];
        $fname = "Person Billed: ".$_POST['firstname'];
        $faddress = "Address: ".$_POST['streetaddress'] . "," .   $_POST['city'] . "," . $_POST['state'] . "," . $_POST['zipcode'];

        $fcontent = $fproduct . $fqty . $fname . $faddress;
        fwrite($newfile, $fcontent);
        fclose($newfile);
        }


  ?>    

答案 3 :(得分:1)

您尝试使用arithmetic operators

您需要连接变量,而不是添加变量。

这个:

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

应该是:

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

然后将内容写入文件:

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

确保所有$ _POST值都可以,但

编辑:使确定 $ _POST值拼写正确:您在表单和结果中使用不同的名称 - &gt;

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

并摆脱所有算术运算符!

$faddress = "Address: ".$_POST['streetaddress'].", ".$_POST['city'].",". $_POST['state'] ."," .$_POST['zipcode'];