Jump to content

Some coding help


Bounty

Recommended Posts

Hello...im trying to make a script that will add a input text field to my page and a submit button witch would save all data in the textfield as "somename.txt"...this is all i could get can you help me out here? :/

 

 

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table align="center">
  <tr>
    <td> <textarea name="textfield" cols="45" rows="5" id="textfield">Text</textarea></td>
  </tr>
  <tr>
    <td align="center"> <input name="submit" type="submit" id="button" value="Submit" onClick=""></td>
  </tr>
</table>
</body>
<?php
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "blahhh";
fwrite($fh, $stringData);
fclose($fh);
?>

</html>

 

Link to comment
Share on other sites

Hi, u need to just store data?

try this:

<?php
if(!$_POST)
{
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table align="center">
<form name="text_field_store_data" action="<?=$_SERVER['PHP_SELF']?>" method="POST">
  <tr>
    <td> <textarea name="textfield" cols="45" rows="5" id="textfield">Text</textarea></td>
  </tr>
  <tr>
    <td align="center"> <input name="submit" type="submit" id="button" value="Submit" onClick=""></td>
  </tr>
</form>
</table>
</body>
<?
}
else
{
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_POST['textfield']; // may u protect the string?
if(!fwrite($fh, $stringData))
{
echo "error appear while trying to store data";
}else{
fclose($fh);
echo "Successfuly stored";
echo "<meta http-equiv='REFRESH' content='2;url=$_SERVER[REQUEST_URI]'>";
}
}
?>

</html>

 

why we do a checking if(!$_POST) if not post in other words, if visitor didnt clicked on Submit he will see only 1st part of script, that contains HTML form only, else he will not see a form, but php part will execute and store data, if script cant open file, error appears, if script cant save data for any reason the error will apeare, if all operation success - Successfuly stored, and will redirect visitor to the same form.

 

Hope it was helpful.

Link to comment
Share on other sites

So far xDD

 

Just another question...i created another text field to be input for a name of the file...the string looks like this:

$myFile = $_POST['name'];

And it works but what to use to add .txt to the name of file ? :P

 

Sorry for bothering :/

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.