problem with forms adding to txt file
Moderator: General Moderators
problem with forms adding to txt file
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!!!!
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!!!!
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
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?
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?
Yeah I overlooked something.
but this is what I was thinking of
this one does something similiar, however
just thowing out some ideas here... 
but this is what I was thinking of
Code: Select all
function nbsp($string)
{
$new_string = ereg_replace(" ", "&nbsp;", $string);
return $new_string;
}Code: Select all
str_replace("", "")- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
Mac
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
Mac
Code: Select all
if (isset($_POSTї'some_particular_variable'])) {
$thefile = '../dynamic/snowreport.txt';
$handler = fopen($thefile, 'w');
foreach ($_POST as $key => $value) {
$data = trim($value)."\n";
fwrite($handler, $data);
}
fclose($handler);
}- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
I put that there because $_POST is defined even when you haven't posted a form, just put
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 -
Then I would do:
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:
Mac
Code: Select all
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>Code: Select all
<input type="hidden" name="action" value="submit" />Code: Select all
if (issetї$_POSTї'action'])) {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ї'action'])) {
/* Unset $_POSTї'action'] 'cause we don't want its value written
to the file */
unset($_POSTї'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 => $value) {
$data = trim($value)."\n";
fwrite($handler, $data);
}
/* Finished putting in the data so close the file */
fclose($handler);
}