Page 1 of 1

problem with forms adding to txt file

Posted: Mon Nov 11, 2002 9:57 pm
by royce
I'm using php to write to a text file which has one variable per line. My problem is that when I use text boxes it adds a space after my input so it throws everything off. Example:

mytext.txt looks like

4
3
2
9
33
44
bla bla bla bla bla bla doo dee doo dee doo
3

I call the info like this <?php echo $myinfo[7] ?> which would bring up 3

When I submit my form with the bla bla bla stuff it adds a space after it which creates...

4
3
2
9
33
44
bla bla bla bla bla bla doo dee doo dee doo

3

So now my <?php echo $myinfo[7] ?> just brings up nothing and shifts everything below it out of place. Anybody know what the problem is?

Here's an example of my text area, pretty basic right?
<textarea name="7" cols="25" rows="5"><?php echo $snow[6]; ?></textarea>


Also, how do I make it so I can add quotes in the text area without the script wigging out? i.e. I enter in 5" and it writes 5\" or something like that. Thanks!!!!

Posted: Mon Nov 11, 2002 10:40 pm
by hob_goblin

Posted: Mon Nov 11, 2002 10:44 pm
by royce
Thanks, I'm not sure how to use them in my form though? :?: :?:

Posted: Mon Nov 11, 2002 11:34 pm
by hatman
there's a &nbsp; function or something like that. You can set it so it replaces the &nbsp; with <br>
Let me see if I can find it.

Posted: Mon Nov 11, 2002 11:37 pm
by hob_goblin
http://www.php.net/nl2br

is probably what you meant.


But, you would be using these functions to process the form, before it adds to the file

http://phpcomplete.com/content.php?id=65

have you tried some tutorials on form processing?

Posted: Tue Nov 12, 2002 1:14 am
by hatman
Yeah I overlooked something.
but this is what I was thinking of

Code: Select all

function nbsp($string)
{
  $new_string = ereg_replace(" ", "&amp;nbsp;", $string);
  return $new_string;
}
this one does something similiar, however

Code: Select all

str_replace("", "")
just thowing out some ideas here... :roll:

Posted: Tue Nov 12, 2002 2:43 am
by twigletmac
To get rid of excess whitespace before you enter something into a text file you'd want to use trim() as hobgoblin advised - this function will remove excess newlines (especially useful when using textboxes) as well as spaces around input.

Mac

Posted: Tue Nov 12, 2002 9:36 am
by royce
Here the code that writes the form info to the text file


<?
// writes the stuff into the file //
if($_POST):
$thefile="../dynamic/snowreport.txt";
$handler=fopen($thefile,"w");

foreach($_POST as $k=>$v)
fwrite($handler,"$v\n");

fclose($handler);
endif;
?>

Where would I put this trim() thing?

Posted: Tue Nov 12, 2002 1:19 pm
by twigletmac
In the foreach loop you are setting up $v as the data to be written to the file so it is this which you want to trim:

Code: Select all

if (isset($_POST&#1111;'some_particular_variable'])) {
    $thefile = '../dynamic/snowreport.txt';
    $handler = fopen($thefile, 'w');
    
    foreach ($_POST as $key =&gt; $value) {
        $data = trim($value)."\n";
        fwrite($handler, $data);
    }

    fclose($handler);
}
Mac

Posted: Tue Nov 12, 2002 1:36 pm
by royce
This part here if (isset($_POST['some_particular_variable'])) {
What do I put in place of 'some_particular_varible'? What if I want all variables trimmed of white space? Can you explain a little. Thanks for being so helpful! :D

Posted: Wed Nov 13, 2002 2:38 am
by twigletmac
I put that there because $_POST is defined even when you haven't posted a form, just put

Code: Select all

&lt;?php

echo '&lt;pre&gt;';
print_r($_POST);
echo '&lt;/pre&gt;';

?&gt;
into a file and view it and you'll see that the array exists even without any form being submitted. So just testing to see if $_POST is set means that your if conditional will return true whether or not any information has been posted. In order to check to make sure that the form has in fact been submitted before you put data into the flat file you need some specific part of $_POST that you check to make sure exists. There are different ways of doing this but I tend to use a hidden field in my form -

Code: Select all

&lt;input type="hidden" name="action" value="submit" /&gt;
Then I would do:

Code: Select all

if (isset&#1111;$_POST&#1111;'action'])) {
to check that the form has been submitted.

Now in your case you are going to be putting all the other information from the $_POST array into your flat file and you won't want this $_POST['action'] variable there too so I'd adjust the code snippet to use the unset() function to remove this value from the array before the file is written:

Code: Select all

if (isset($_POST&#1111;'action'])) {
    /* Unset $_POST&#1111;'action'] 'cause we don't want its value written
        to the file */
    unset($_POST&#1111;'action']);

    /* Get the file ready to be written to */
    $thefile = '../dynamic/snowreport.txt'; 
    $handler = fopen($thefile, 'w'); 
    
    /* Here you take all the values from the $_POST array (excluding
        action which has been unset) and trim them and put them into
        the file. */
    foreach ($_POST as $key =&gt; $value) { 
        $data = trim($value)."\n"; 
        fwrite($handler, $data); 
    } 
    
    /* Finished putting in the data so close the file */
    fclose($handler); 
}
Mac