Jump to content

New to php, Don't understand why this wont work


MrDoug

Recommended Posts

I just began learning php a few days ago and here's what I'm attempting to accomplish...

 

html file takes input from a form

sends it to a php file

php file writes it to a text file

 

It just wont work though, heres my code can anyone help me please...

 

HTML FILE - (text.html)

------------------------------------------------------------------------

<html>
<body bgcolor="#000000">
<center>
<form action="text.php" method="post">
<font size="5" color="#FFFFFF">Enter Text</font><br>
<br>
<font color="#FFFFFF">Input1:</font><br><input type="text" name="input1" size="60"><br>
<br>
<font color="#FFFFFF">Input2:</font><br><input type="text" name="input2" size="60"><br>
<br>
<font color="#FFFFFF">Input3:</font><br><input type="text" name="input3" size="60"><br>
<br>
<font color="#FFFFFF">Input4:</font><br><textarea name="input4" cols="60" rows="4"></textarea><br>
<br>
<input type="submit" value="Save" name="submit">
</form>
</center>
</body>
</html>

 

PHP FILE - (text.php)

---------------------------------------------------------------------

<?php

$fp = fopen(info.txt, "a");

fwrite($fp, '$_POST['input1']');
fwrite($fp, '$_POST['input2']');
fwrite($fp, '$_POST['input3']');
fwrite($fp, '$_POST['input4']');

?>

 

Thanks for your help,

MrDoug

Link to comment
Share on other sites

Does it create the file, does it not create the file, does it create it but it's empty... what doesn't work?

 

question on this topic

 

in this code

<?php

$fp = fopen(info.txt, "a");

fwrite($fp, '$_POST['input1']');
fwrite($fp, '$_POST['input2']');
fwrite($fp, '$_POST['input3']');
fwrite($fp, '$_POST['input4]');

?>

 

can you subsitute info.txt for $user_name.txt so that it pulls a user name value and creates a text file based off of that?

Link to comment
Share on other sites

@revraz, it wont even create the textfile, and if i make it manualy prior to running the code it remains empty

 

@prcollin I dont have any username variables but i could create them, how would that help though

Link to comment
Share on other sites

@revraz, it wont even create the textfile, and if i make it manualy prior to running the code it remains empty

 

@prcollin I dont have any username variables but i could create them, how would that help though

 

My guess is that this is a permissions problem.  Is this on a windows or Unix server?

Link to comment
Share on other sites


$fp = fopen("info.txt", "a+");

fwrite($fp, '$_POST['input1']');
fwrite($fp, '$_POST['input2']');
fwrite($fp, '$_POST['input3']');
fwrite($fp, '$_POST['input4']');
fclose($fp);
?>

 

His code makes no attempt to read from the filehandle, so there's no reason to open the file with 'a+' mode.  Opening with 'a' is fine here and does work.  As I posted previously I'm fairly confident this is a file permissions problem.  By default this will try to write in the same directory as the executing script, and probably the Apache user doesn't have permissions to write to that directory.

 

 

Link to comment
Share on other sites

@revraz, it wont even create the textfile, and if i make it manualy prior to running the code it remains empty

 

@prcollin I dont have any username variables but i could create them, how would that help though

 

No i wasnt suggesting an answer just another question on how I could use a variable to save the file. 

 

One more thing how do you get it to post to a specific directory again I used to know now i forget

 

i have a directory called philsshit on my server and i want to take this new file and put it in that directory

 

Link to comment
Share on other sites

@revraz, it wont even create the textfile, and if i make it manualy prior to running the code it remains empty

 

@prcollin I dont have any username variables but i could create them, how would that help though

 

No i wasnt suggesting an answer just another question on how I could use a variable to save the file. 

 

In answering you I completely blanked out on the fact you wanted to use a variable -- I amended the code example so it uses a variable.  As I stated, No problem, you can do it.

Link to comment
Share on other sites

He wasn't the OP.

 

In answering you I completely blanked out on the fact you wanted to use a variable -- I amended the code example so it uses a variable.  As I stated, No problem, you can do it.

Link to comment
Share on other sites

He wasn't the OP.

 

In answering you I completely blanked out on the fact you wanted to use a variable -- I amended the code example so it uses a variable.  As I stated, No problem, you can do it.

 

Agreed.  I did try and answer the original poster, but he seems to have gone dark -- even PM'd him.  I'm about 99.9% sure it's a permissiosn problem.

Link to comment
Share on other sites

sry about not responding, I was posting during a free period at school but I had to return to classes.  I got your PM, but it said I'm not allowed to send PM's, dunno why.  I'm not actually sure it was a permissions problem.  I fiddled with it a little more and modified it based on reading other code and somehow it worked. :D

Heres my code, btw I added some things once I got it working and am also now using an html file rather than txt

<?php

$fp = fopen('info' . '.html', 'a');

fwrite($fp, '<br>');
fwrite($fp, '<br>');
fwrite($fp, '<b>input1:</b>');
fwrite($fp, '<br>');
fwrite($fp, $_POST['input1']);
fwrite($fp, '<br>');
fwrite($fp, '<br>');
fwrite($fp, '<b>input2:</b>');
fwrite($fp, '<br>');
fwrite($fp, $_POST['input2']);
fwrite($fp, '<br>');
fwrite($fp, '<br>');
fwrite($fp, '<b>input3:</b>');
fwrite($fp, '<br>');
fwrite($fp, $_POST['input3']);
fwrite($fp, '<br>');
fwrite($fp, '<br>');
fwrite($fp, '<b>input4:</b>');
fwrite($fp, '<br>');
fwrite($fp, $_POST['input4']);
fwrite($fp, '<br>');
fwrite($fp, '<br>');
fwrite($fp, '<hr height="5" width="100%">');

?>

 

Thanks for all your help,

MrDoug

 

Oh, one more thing, how do I make it load a new html page once its written everything, so for instance...

user gives input

input is saved

new page loads saying "thanks for contributing" or whatever

Link to comment
Share on other sites

Oh, one more thing, how do I make it load a new html page once its written everything, so for instance...

user gives input

input is saved

new page loads saying "thanks for contributing" or whatever

 

This is a big reason why people use PHP -- because anything you print or echo is returned to the client as html.  So all you need to do is echo out what you want.

 

echo 'Thanks for your submission. ';

Link to comment
Share on other sites

how do you specify the directory it drops into

 

Add the path to the fopen() function when you specify the file, rather than just the filename.

 

fopen('/some/path/yourfile.txt', ....)

 

so could i do

$fp = fopen(userpages/$user_name . '.txt', 'a');

so that it takes the user name input from the form to create the file?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.