954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

form input to text file - cant figure this one out

Hello all
i have (after much googleing) written a php script which when activated appends to a main text file with the text seen below in this exact format

type=
name=----------------------------------------------------------------------------------------------
#
type=
name=(USER INPUT HERE)
#
type=
name=----------------------------------------------------------------------------------------------
#

what i need is a simple webform with one input field where the user can enter thier name.
When they hit submit the main text file would be written (appended) to with the text shown above yet with the users name in place of (USER INPUT HERE).From what i understand this is a 3 part job and i am missing the first two parts the input and processing of the data
i have found many simple input forms but have no clue how these should be made to work with my present script which works the way it should.

Can some one point me in the right direction?
im going through all the tutorials i can at the moment and might find a solution before anyone here can help.
But there is no harm in asking .

ciao folk

thanks for any feedback

solocky

solocky
Newbie Poster
8 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

So you want a simple form which when submited writes exactly:
type=
name=----------------------------------------------------------------------------------------------
#
type=
name=(THE USERS INPUT)
#
type=
name=----------------------------------------------------------------------------------------------
#

To the textfile ?

Is that correct and all you need help with ?

Designer_101
Posting Whiz
314 posts since Jul 2007
Reputation Points: 12
Solved Threads: 16
 

as i said before i have a script that does this already but what i need help with is making the entry of the input input field this form
http://solocky.hosting4you.net/
be written in the text file where it says ( the users input) as shown above
ive made the simple form but i cant get the two to interact right now im reading up on the (isset($_POST['submit'])) function

this is my first suchattemt at any sort of programing so any help would be appreciated

solocky
pardon my english

solocky
Newbie Poster
8 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

heres the code im using sraight out the text book

<?php
if (isset($_POST['submit'])) {
$filename = “far.*plx”; // Where the “name” is name of file it will create if file doesnt exist, and will write data on it.
$open = fopen($filename, “a”);
$name = $_POST['say'];
$hidden = $_POST['hidden'];
fwrite($open, “SAY WHAT U WILL:\t$say\r”);
fclose($open);
}
?>
<html>
<head>
<title>X-BLOG</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body><!-- Printing on page starts from here -->
<!-- This is a comment and will not be printed on page -->
<form action="" method="POST">
SAY WHAT U WILL:<input type="text" name="say" /> 
<input type="hidden" name="hidden" value="type=" />
<input type="hidden" name="hidden" 

value="name=----------------------------------------------------------------------------------------------" />
<input type="hidden" name="hidden" value="#" />
<input type="hidden" name="hidden" value="type=" />
<input type="hidden" name="hidden" value="name=" /
<input type="hidden" name="hidden" value="#" />
<input type="hidden" name="hidden" value="type=" />
<input type="hidden" name="hidden" 

value="name=----------------------------------------------------------------------------------------------" />
<input type="hidden" name="hidden" value="#" />
<input type="submit" name="submit" value="Write it !" />
</form> <!-- Ending the form -->
</body>
</html>
solocky
Newbie Poster
8 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Solocky, as I understand it, you don't need to pass all those hidden strings in HTML to the client; the user never sees them; they just get written to your text file with the users "say" input embedded, so all those strings only need exist in PHP, not HTML.

Try something like this (off the cuff, not tested):

<?php
// String params (do it like this, or similar for security reasons - ie avoid code injection)
$params = array('say' => 'say'); //add further string params here as necessary.
while( list($var, $param) = @each($params) )
{
  if ( isset($HTTP_POST_VARS[$param]) || isset($HTTP_GET_VARS[$param]) ) 
  {
    $$var = ( isset($HTTP_POST_VARS[$param]) ) ? htmlspecialchars($HTTP_POST_VARS[$param]) : htmlspecialchars($HTTP_GET_VARS[$param]);
  }
  else
  {
    $$var = '';
  }
}
// Boolean params (do it like this, or similar for security reasons - ie avoid code injection)
$params = array('submit' => 'submit'); //add further boolean params here as necessary.
while( list($var, $param) = @each($params) )
{
  if ( isset($HTTP_POST_VARS[$param]) || isset($HTTP_GET_VARS[$param]) )
  {
    $$var = true;
  }
  else
  {
    $$var = false;
  }
}

if ($submit && $say != '') {
  $filename = “far.*plx”;
  $fn = fopen($filename, “a”);
  $myStringArray = array(
    '----------------------------------------------------------------------------------------------',
    'type=',
    'name=',
    $say,
    '#',
    'type=',
    '----------------------------------------------------------------------------------------------',
    '#',
    ''
  );
  fwrite($fn, implode("\n", $myStringArray));
  fclose($fn);
}
?>
<html>
<head>
<title>X-BLOG</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<form action="myScriptFile.php" method="POST">
SAY WHAT U WILL: <input type="text" name="say" /> 
<input type="submit" name="submit" value="Write it !" />
</form>
</body>
</html>

As you will see, I've implode()d an array of strings to compose the string that gets written to file. Others may choose to use the str_replace() method. Whatever floats your boat.

ReplacemyScriptFile.php with your actual php filename.

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

thank you for your help
unfortunetly the input is not getting in to the text file but every thing else works charmingly im open to more ideas and im still googleing

thank you
ciao
solocky

solocky
Newbie Poster
8 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

OK, I'll turn on my development machine and debug the code .....

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

OK, try this :-

<?php
// String params (do it like this, or similar for security reasons - ie avoid code injection)
$params = array('say' => 'say'); //add further string params here as necessary.
while( list($var, $param) = @each($params) )
{
  if ( isset($HTTP_POST_VARS[$param]) || isset($HTTP_GET_VARS[$param]) ) 
  {
    $$var = ( isset($HTTP_POST_VARS[$param]) ) ? htmlspecialchars($HTTP_POST_VARS[$param]) : htmlspecialchars($HTTP_GET_VARS[$param]);
  }
  else
  {
    $$var = '';
  }
}
// Boolean params (do it like this, or similar for security reasons - ie avoid code injection)
$params = array('submit' => 'submit'); //add further boolean params here as necessary.
while( list($var, $param) = @each($params) )
{
  if ( isset($HTTP_POST_VARS[$param]) || isset($HTTP_GET_VARS[$param]) )
  {
    $$var = true;
  }
  else
  {
    $$var = false;
  }
}

if ($submit && $say != '') {
  $filename = 'far.txt';
  $fn = fopen($filename, 'a');
  $cr = "\r\n";
  $myStringArray = array(
    'type = ' . $cr,
    'name = ' . $say . $cr,
    '----------------------------------------------------------------------------------------------' . $cr,
    '#' . $cr,
  );
  fwrite($fn, implode('', $myStringArray));
  fclose($fn);
}
?>
<html>
<head>
<title>X-BLOG</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body>
<form action="test.php" method="POST">
SAY WHAT U WILL: <input type="text" name="say" />
<input type="submit" name="submit" value="Write it !" />
</form>
</body>
</html>


Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

Solocky,

Even better, change the line :-
if ($submit && $say != '') {
to if ($say != '') {

Otherwise the write block will only execute if the user clicks theSubmit button, not if he hits the return key on the keyboard.

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

Hello all i have (after much googleing) written a php script which when activated appends to a main text file with the text seen below in this exact format

type= name=---------------------------------------------------------------------------------------------- # type= name=(USER INPUT HERE) # type= name=---------------------------------------------------------------------------------------------- #

what i need is a simple webform with one input field where the user can enter thier name. When they hit submit the main text file would be written (appended) to with the text shown above yet with the users name in place of (USER INPUT HERE).From what i understand this is a 3 part job and i am missing the first two parts the input and processing of the data i have found many simple input forms but have no clue how these should be made to work with my present script which works the way it should.

Can some one point me in the right direction? im going through all the tutorials i can at the moment and might find a solution before anyone here can help. But there is no harm in asking .

ciao folk

thanks for any feedback

solocky

You need to first open the file: $handle = fopen( "FILEPATH" , "a" ); // a meaning amend

Then you need to fwrite the file with the users input:

fwrite( "#\ntype=\nname=".$_POST['input'] , $handle );


Then close the file: fclose( $handle );

This should work.

Josh Connerty
Posting Whiz
342 posts since Apr 2009
Reputation Points: 31
Solved Threads: 27
 

Solocky,

Another thing ........ your code has smart-quotes in it, which will really screw things up. PHP only understands straight-quotes, "..." and '...' (which behave differently in some circumstances). See the Strings section of the PHP manual.

If you have to use MS Word to edit your code then turn smart-quotes off (somewhere in the preferences). Notepad is better but personally I use FirstPage 2000 by EvrSoft (though the 2006 version has been available for some 3 years now). Once you've used an editor that colour-codes your script, there's no going back.

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

MUCH THANX FOR YOU TWO WHO HELPED ME WITH THIS PROBLE
IVE GOT IT UP AND RUNNING AS IT NEEDS TO
YOUCAN SEE IT IN ACTION AT
http://solocky.hosting4you.net/
UNFORTUNETLY IF YOU DONT HAVE AN XBOX WHICH RUNS THE NAVI-X SCRIPT THEN YOU CANT READ THE RESULTS STILL YOU CAN STOP BY AND LEAVE A MESSAGE ( YOU WONT GET A CONFORMATION THAT YOUR MESAGE WAS SENT -IM STILL WORKING ON THAT JUSTSUBMIT AND FINISH)

THANKS
AGAIN
SOLOCKY

solocky
Newbie Poster
8 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You