Jump to content

writing to file


jeanay

Recommended Posts

I am new to PHP and I cannot figure out what I am doing wrong...  ???

 

I need to write data to a file from a form...

 

The code for the form is:

 

<form action="writedirectory.php" method="get" enctype="application/x-www-form-urlencoded" name="Telephone Directory Form" id="telephonedir." dir="" lang="en" onSubmit="Add Entry">

  <p>

    <label>Last Name

      <input type="text" name="lastname" id="lastname" />

    </label>

  <label>First Name

      <input type="text" name="firstname" id="firstname" />

    </label>

  </p>

  <p>

    <label>Street Address     

      <input name="streetadddress" type="text" id="streetadddress" size="57" />

    </label>

  </p>

  <p>

    <label>City

      <input type="text" name="city" id="city" />

    </label>

    <label>State

      <input name="state" type="text" id="state" size="5" />

    </label>

    <label>Zip Code

      <input name="zipcode" type="text" id="zipcode" size="15" />

    </label>

  </p>

  <p>

    <label>Area Code

      <input name="area" type="text" id="area" size="7" />

    </label>

    <label>Phone Number

      <input type="text" name="phonenumber" id="phonenumber" />

    </label>

  </p>

  <p>

    <input type="submit" name="submit" id="submit" value="Add Entry" />

  </p>

</form>

<hr />

 

 

The code to write is:

 

<?php

 

$lastName=$_GET['lastname'];

$firstName=$_GET['firstname'];

$streetAddress=$_GET['streetaddress'];

$city=$_GET['city'];

$state=$_GET['state'];

$zipCode=$_GET['zipcode'];

$areaCode=$_GET['area'];

$phoneNumber=$_GET['phonenumber'];

 

 

 

 

if (empty ($_GET['lastname']) && empty

($_GET['firstname']) && empty

($_GET['streetaddress']) && empty

($_GET['city']) && empty

($_GET['state']) && empty

($_GET['zipcode']) && empty

($_GET['area']) && empty

($_GET['phonenumber']))

echo "<p><hr /><p>You must complete entire form to submit entry</p><hr /></p>";

 

else {

 

$PhoneDirectory = "TelephoneDirectory.txt";

$AddEntry = fopen("$PhoneDirectory", "a");

 

if (fwrite ($AddEntry, $lastName.=","$firstName.=","$streetAddress.=","$city.=","

$state.=","$zipCode.=$areaCode.=","$phoneNumber);

 

echo "Entry submitted successfully";

fclose($AddEntry);

 

} else {

echo "Unable to add entry";

}

 

?>

 

 

I know that some of the syntax is wrong... I just need to be pointed in the right direction.... I appreciate any help or assistance.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/163709-writing-to-file/
Share on other sites

1) remove the enctype from your form tag. You do not have any reason for it to be there.

2) change the form method to post.  There are very few reasons on this planet why you would need to make a form's method GET.

3) since you changed your form's method to post, change all the $_GET to $_POST in your php code

4) I can't even begin to comprehend what it is you're attempting to do with that fwrite <-- link to manual. Use it.  Or at  least post what and how you want stuff put into your text file.

Link to comment
https://forums.phpfreaks.com/topic/163709-writing-to-file/#findComment-863866
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.