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

尝试通过表单输入在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);
        ?>

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

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

HTML: p >

 &lt; form action =“p1_checkout.php”method =“POST”&gt; 
名字:&lt; br&gt; 
&lt; input type =“text”name =“  firstname“&gt;&lt; br&gt; 
姓氏:&lt; br&gt; 
&lt; input type =”text“name =”lastname“&gt;&lt; br&gt; 
街道地址:&lt; br&gt; 
&lt;  ; input type =“text”name =“streetaddress”&gt;&lt; br&gt; 
 City:&lt; br&gt; 
&lt; input type =“text”name =“city”&gt;&lt; br&gt; 
 State  :&lt; br&gt; 
&lt; input type =“text”name =“state”&gt;&lt; br&gt; 
邮政编码:&lt; br&gt; 
&lt; input type =“text”name =“zipcode”  &gt;&lt; br&gt; 
电话号码:&lt; br&gt; 
&lt;输入类型=“文字”名称=“phonenumber”&gt;&lt; br&gt; 
订单数量:&lt; br&gt; 
&lt;输入 type =“text”name =“orderquantity”&gt;&lt; br&gt; 
&lt; input type =“submit”value =“Checkout”&gt; 
&lt; / form&gt; 
  code>  p 重新> 
 
 

PHP: p>

 &lt;?php 
 $ newfile = fopen(“order.txt”,“w +”); 
 $  fproduct =“Product:Mount Diablo,California Print”; 
 $ fqty =“Quantity Ordered:”。$ _ POST ['quantityordered']; 
 $ fname =“Person Billed:”。$ _ POST ['firstname'];  
 $ faddress =“地址:”。$ _ POST ['streetaddress'] +“,”+ $ _POST ['city'] +“,”+ $ _POST ['state'] +“,”+ $ _POST ['  zipcode']; 
 $ fcontent = $ fproduct + $ fqty + $ fname + $ faddress; 
 fwrite($ newfile,$ content); 
 fclose($ newfile); 
?&gt; 
  code  >  pre> 
  div>

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'];

     <?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);
 }
?>

hey you are doing the slight mistake... if you are using php in same file.. So, no need to give the file name in action leave it blank....

and for concatenation in php we don't use the

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

here is the code.. try it....

    <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);
}     
?>

<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);
        }


  ?>