Jump to content

Question about PHP newlines


Whitelighter

Recommended Posts

Hello, everyone. This is my first post here.So, I have a question. I've had a problem but I solved it, I don't need any help making codes run smoothly on the browser. So, it's more of a philosophical question.Now, here's a part of the code in a php page I'm making.

$fp=fopen("pg.txt","r");$x=fgets($fp);if ($x == "03032010tru"){echo '<td><form action="get03032010.php" method="get">Enter your name here: <input type="text" name="claimername" /> <br /><input type="submit" name="submit" value="Claim!" /></form></td>';}

So, I have a pg.txt file, in which these are written:

03032010tru10032010tru17032010tru

So, in my php code above, I just tell the program to read the first line of the .txt with the fgets() function and put it in $x. Ok, it does it. So, now $x contains the text "03032010tru" with a newline character at the end. Then, I want to check a condition with the if statement and if $x is actually "03032010tru" with a newline character at the end, the program should do an echo.Now, the question is how do I add the newline character in the string of the condition? You see in the code I haven't added it.Notice this is a string just to be used for comparing with a variable, not for printing, so I think adding <br /> wouldn't do the trick. I tried it and it really didn't.So, I'm thinking "I should put an \n, after all it's the 'real' newline character (cos <br /> is actually just a trick with html)". I add an \n making it

if ($x == "03032010tru\n")

and it still doesn't work.Then I'm thinking "maybe I should actually press the <Enter> button, although it most probably won't work". That means the code should be like this:

$fp=fopen("pg.txt","r");$x=fgets($fp);if ($x == "03032010tru"){echo '<td><form action="get03032010.php" method="get">Enter your name here: <input type="text" name="claimername" /> <br /><input type="submit" name="submit" value="Claim!" /></form></td>';}

Just the same, only with a new line after the 3rd line. It seems ridiculous but that's how it eventually worked!And I want to ask: Why? Why didn't the "\n" work? And why did the Enter button work? Does this mean that I can never get rid of this ugly code? I'm calling it ugly 'cos there's a line with only quotation marks and a parenthesis in it and I can't merge it with another line or something similar. That is, I can't change the appearance of the code.Isn't it weird? Or am I a noob at programming? :)

Link to comment
Share on other sites

Line endings are OS-dependent. Windows uses \r\n, Unix uses \n, and Macs use \r. PHP also defines the constant PHP_EOL to hold the line ending for the OS that PHP is running on. You may want to use a function like trim to remove any whitespace including line endings and just compare the values.

Link to comment
Share on other sites

Line endings are OS-dependent. Windows uses \r\n, Unix uses \n, and Macs use \r. PHP also defines the constant PHP_EOL to hold the line ending for the OS that PHP is running on. You may want to use a function like trim to remove any whitespace including line endings and just compare the values.
Thanks a lot, justsomeguy, that's what I needed pointed out.Would I be off-topic now if I asked another question?I want to edit a file with php. I open it with "w" and it deletes it and creates a new one. I open it with "a" and the file pointer begins at the end of the file. So, I fseek() but it doesn't work on files opened for appending (as stated on php.net). So how do I edit something that is already written in the file but (of course) without truncating the file? I won't mind if you don't want to answer, as it's offtopic actually.
Link to comment
Share on other sites

Open the file with 'r+'. Read the file in, edit the text, then write it back out:$fh = fopen("/path/to/file.txt", "r+");

Link to comment
Share on other sites

More explicitly:Open the file and read it in:

$filename = "/path/to/file.txt";$handle = fopen($filename, "r+");$contents = fread($handle, filesize($filename));fclose($handle);

...edit the file...Write the file back out:

$string = 'my new data';$filename = "/path/to/file.txt";$handle = fopen($filename, "r+");  // could also be "$handle = fopen($filename, "w");"fwrite($handle, $string);fclose($fp);

Link to comment
Share on other sites

Thanks, guys, you really did help me.One last question (off-topic too :)):I made a page with several forms in it. Each form consists of a text field and a submit button. When one writes his name on one of the text fields and presses the respective submit button, the data (the name actually) is transferred to another page. So when this second page is loaded, a file is edited and, long story short, the initial page with the forms is edited so that the form submitted does not appear anymore.So, it's like this: There are 3 forms. The visitor of the page chooses one, writes the name, clicks the button and thus permanently changes this page (the one with the forms) so that it now has 2 forms (the one he wrote his name on is deleted).My problem is, when one writes his name, clicks submit and then loads the initial page again, now one form short, his name appears pre-written in the text field of the next form. It must be the browser's work, maintaining cache information so that it appears again, but in the next form (since the one the visitor submitted no longer exists). It's like when you're on a forum and press "Post Reply", write a message in the text area, then click Back on your browser, then Forward again and the message seems pre-written in the text area.I don't want the name to appear pre-written in the next form. How do I do it? Can I do it with PHP or is it just a browser setting making it impossible for me to handle?Thanks in advance.

Link to comment
Share on other sites

Are you sure the value isn't being put there? If you view the source of the page and find that field element, does it include the name in the value attribute? If you close the browser and open it again does the name show up when you load that page again?

Link to comment
Share on other sites

Instead of using fopen and fwrite, you can also read the entire file with file_get_contents and write to it with file_put_contents.
Too easy, where's the challenge? :)
Link to comment
Share on other sites

Are you sure the value isn't being put there? If you view the source of the page and find that field element, does it include the name in the value attribute? If you close the browser and open it again does the name show up when you load that page again?
It doesn't include the name in the value attribute. If I close the browser or refresh the page, the name stops showing up. I think it shows up only when you click Back in your browser. But even then I don't want it to show up. Though I don't know whether I'm able to control this stuff or it's just browser settings.
Link to comment
Share on other sites

long story short, the initial page with the forms is edited so that the form submitted does not appear anymore.
It sounds like you want the person to complete several forms, in sequence and preserve the data submitted so you have a single set of data from several forms.In that case, I'd recommend offering the pages in sequence and storing the data in session variables to preserve it as the visitor moves through the forms. When the last form is completed, use the data from the session to perform the next action.
Link to comment
Share on other sites

That's not the problem I described. I said I don't want previously written data in a text field to appear in the next text field when I press Back on my browser. It's pretty trivial so I don't know if I can fix it. Maybe there's a function that erases previously submitted information from appearing in a form, so I can apply it when the initial page is loaded.

Link to comment
Share on other sites

what about just writing a simple init() javascript function that sets the value of all input based form elements to empty strings? Or it could be done with PHP if the forms are being generated that way.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...