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