0

Thanks for the help. Object of the program.
file.php has a form and the following code. names.txt is the file that the will populate with the names entered in the textfield and when a user clicks on submitted.

A person enters their name into a form field and clicks submit. The name is entered into a file called names.txt

Issue. The file.php program works.
The form displays in the browser and the test 'echoes' show in the browser. BUT the names.txt field does not have any data inside of it even when I enter a name into the textfield and click submit.

file.php
if (isset($_POST['name'])) {

    $name = $_POST['name'];
    if (!empty($name)) {
        //echo 'okay';

        $handle = fopen('names.txt', 'w');
        fwrite($handle, $'alex');
        fwrite($handle, $'kyle');
        fclose($handle); 

    }else{
        echo 'please type a name';

?>

<form input type ="text" name ="file.php" method ="POST" >
    Name: <br><input type ="text"  name ="name"><br><br>
    <input type="submit" name ="submit" value ="Submit">   
</form>

3 Answers 3

1

this

$'alex'

does not mean "a variable with the name alex in it". If you want to write the string 'alex', you need.

fwrite($handle, 'alex');
Sign up to request clarification or add additional context in comments.

1 Comment

sorry - i have been at this all day. typo. the three of you responded immediately and thanks. can you read that issue with the question mark icon in my file structure. i think that has something to do with it. thanks
1

The variables $'alex' and $'kyle' do not exist. Try this instead:

$handle = fopen('names.txt', 'w');
fwrite($handle, $name . "\n");
fclose($handle); 

1 Comment

thanks sam. I modified by code to the above. Still no output when I type localhost/phpWithAlex/names.txt. Did you see my comment regarding weird stuff going on with my file structure. Many hours trying to figure what is probably an easy fix but I am new to php although not programming etc.
1

You are have several issues here, your opening the file using fopen in mode w, this basically opens the file in write mode, places a the pointer at the start of the file and then truncates the ifile.

You should use be using a+, that will open the file, create it if required and place the pointer at the end of the file.

Next big issue is your using the following lines

fwrite($handle, $'alex');
fwrite($handle, $'kyle');

By your description you should be using

fwrite($handle, $name);

another small issue is your using both isset and empty, you can just use empty as that does an internal isset check as well

try the following:

if (!empty($_POST['name']))
{
    $handle = fopen('names.txt', 'a+');
    fwrite($handle, $_POST['name']);
    fclose($handle);
}
else
{
    echo 'please type a name';
    ?>
    <form input type ="text" name ="file.php" method ="POST" >
        Name: <br><input type ="text"  name ="name"><br><br>
       <input type="submit" name ="submit" value ="Submit">   
    </form>

4 Comments

Thank you Robert. I am going to modify the code now. You make sense. Is there a way to include a screenshot ? Somehow while I was working on this code, what I believe is a rather serious problem came about. at the url: localhost/ i now have a directory tree of 4 folders. One of these is called phpWithAlex where I am saving all my practice programs code. When I click on phpWithAlex, which is where file.php and names.txt reside, all of the folders have a QUESTION MARK ICONS to the left of them. :(
are you running linux ?
I wish I were. Using terminal(unix commands) on a Macbookpro. Installed apache phpMyAdmin and mySQL. Looked in http.conf files etc. I am a programmer but I am just now learning php. I restarted apache to but that did not resolve the problem.
Hi again. Can you suggest a really basic way using PHP to create a .txt file that I can populate with data. Once I have this set,I can continue with the tutorials that will later have me using mySql. Please read my original post which is descriptive. It uses a from field where a user enters their information. Thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.