Dynadot โ€” .com Transfer

I am also in need of Php help

Spacemail by SpaceshipSpacemail by Spaceship
Watch

Infernal

Established Member
Impact
0
imjust bstarting to learn php, and i would like some help on appending a file using a form.

e.g
i have index.php in my browser and it has a text field on it i want whatever is in the text field to write to the file test.txt

this is the current code i have (dont laugh plz, this is the second day ive tryed and learnt php (about the 3rd hour))

PHP:
<?php
$myfile = "test.txt";
$f = fopen($myfile, 'a') or die("can't open file");
$ind = "index.php";

echo "
<table border=0 cellspacing=0 cellpadding=1>
  <tr>
    <td>
<form action=\"$ind\" method=\"POST\">
	<p>Input:
      	<input style=\"width:400\" type=\"text\" name=\"input\" value=\"$txt\" />
  </p>
	<p>
   	  <input name=\"submit\" type=\"submit\" value=\"submit\" />
	</p>
</form>
</td>
  </tr>
</table>";

?>

thanks for any help you might have

Inf
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Take a look at the example in the manual: http://ph.php.net/manual/en/function.fopen.php

Also try not to mix your PHP and Html. It's better to get into good habits earlier rather than later :)

Lux

PS: you've got the variable $txt but you're never writing anything to it...
 
0
•••
okay i know all of that on the link you gave me, its kinda not what i asked. can anyone else help me? theres a script called cutenews that uses this method. ive been looking at its php and i cant figure out how it works :( if nobody can help then nevermind thanks anyway

Inf
 
0
•••
PHP:
<?php
$myfile = "test.txt";
$f = fopen($myfile, 'a') or die("can't open file");

//Let's not mix your PHP... also - use single quotes, like this: echo('text'); - it parses faster.
//And, instead of making a variable called $ind, let's just write index.php ;)
?>
<table border=0 cellspacing=0 cellpadding=1>
  <tr>
    <td>
<form action="index.php" method="POST">
    <p>Input:
          <input size="50" type="text" name="input" value="<?php echo($txt) ?>" />
  </p>
    <p>
         <input name="submit" type="submit" value="submit" />
    </p>
</form>
</td>
  </tr>
</table>

I revised it a little bit - I don't have much time right now. I also fixed up that HTML form, style='width:400' won't work for a textbox. Instead, I used size="50".
 
0
•••
Next code you will need is the writing code. There are many different ways of doing this mind you. As well if this isn't private information txt file is fine if it is try finding another format such as .db or try adding none viewing into htaccess

Anyways the code for the fwrite you can learn at
http://www.php.net/fwrite
This fwrite function is the function which writes to an opened file.
Also make sure you chmod the file text.txt to 777.

Best of luck. Please post when you finsh your project!
 
0
•••
PHP:
<?php

/*Something like this MAY work :) */

$myfile = "test.txt";
$fp = fopen($myfile, 'w+') or die("can't open file");

?>
<table border=0 cellspacing=0 cellpadding=1>
  <tr>
    <td>
<form action="index.php" method="POST">
    <p>Input:
          <input size="50" type="text" name="input" value="<?php echo($txt) ?>" />
  </p>
    <p>
         <input name="submit" type="submit" value="submit" />
    </p>
</form>
</td>
  </tr>
</table>
<?php

fwrite($fp, $txt);
fclose($fp);

?>
 
0
•••
SecondVersion said:
PHP:
<?php

/*Something like this MAY work :) */

$myfile = "test.txt";
$fp = fopen($myfile, 'w+') or die("can't open file");

?>
<table border=0 cellspacing=0 cellpadding=1>
  <tr>
    <td>
<form action="index.php" method="POST">
    <p>Input:
          <input size="50" type="text" name="input" value="<?php echo($txt) ?>" />
  </p>
    <p>
         <input name="submit" type="submit" value="submit" />
    </p>
</form>
</td>
  </tr>
</table>
<?php

fwrite($fp, $txt);
fclose($fp);

?>


That may work but you should verify that the fields are actually there.. Here is a fixed version of your code.
PHP:
<?php
/*Make sure to check that the fields where inputed.. You can produce an error if you wish but for now I will just leave it at this */

if(isset($_POST['input'], $_POST['submit'])) {
/*Something like this MAY work :) */

$myfile = "test.txt";
$fp = fopen($myfile, 'w+') or die("can't open file");

fwrite($fp, $txt);
fclose($fp);
}else{
?>
<table border=0 cellspacing=0 cellpadding=1>
  <tr>
    <td>
<form action="index.php" method="POST">
    <p>Input:
          <input size="50" type="text" name="input" value="<?php echo($txt) ?>" />
  </p>
    <p>
         <input name="submit" type="submit" value="submit" />
    </p>
</form>
</td>
  </tr>
</table>
<?php } ?>

This parses faster if you don't output the html within the php.

Wish you the best of luck.
 
0
•••
okay if my SEMI WORKING database does not work i will try using all of the versions you guys have posted, but i just coded this and it partially works. feel free to try it out, it appends files quite well i guess, needs tweaking though, thanks for the help guys

PHP:
<?php 
$myFile = "test.txt";
$f=fopen("test.txt","a") or die("can't open file");
$txt=$_POST["text"];

$stringData = $_POST["text"];
fwrite($f, $stringData);

echo "
<table border=0 cellspacing=0 cellpadding=1>
  <tr>
    <td>
<form action=\"form.php\" method=\"POST\">
    <p>Input:
          <input size=\"50\" type=\"text\" name=\"text\" />
  </p>
    <p>
         <input name=\"submit\" type=\"submit\" value=\"submit\" />
    </p>
</form>
</td>
  </tr>
</table>";

fclose($f);

echo "You just wrote ";
echo "$txt";
echo " to the database!"

?>

Inf

EDIT:----->

okay i have done it, and it is currently under further development :) eventually ill have a full script for use with the templates i will be selling :)

a sample of the script is......
http://www.infernalgfx.co.uk/tests/phplearn/ffdb2/form.php
just write something in the box and submit it, it then takes you to a screen confirming what you just wrote (this is the form that actually adds to the database). once you have wrote to that click this link:
http://www.infernalgfx.co.uk/tests/phplearn/ffdb2/test.txt
and it shows what you just wrote (at the bottom and encoded in html(if you ctrl+a to select it all and paste it into dreamweaver or front page you will see it in a graphical view))

thanks :)

Inf
 
Last edited:
0
•••
Nice Job!

Good luck with the full script if you need any help just post :)
 
0
•••
try using the the mode a+ when you assigning the file handler.
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back