cw22 Posted July 22, 2010 Share Posted July 22, 2010 Hello - I was hoping I could receive some help today with something that's just been eating at me. I want to have a php page with a form - and the user enters something like "Hey There" into a textarea, and when they submit the form as POST on the same php page, the page will check the user input against a pre-defined variable, and if $_POST['code'] == $predefined var { //echo 'Correct'; } I however want the POST and predefined var to be multiline capable, without me having to enter <br /> etc. So, I used the nl2br() php function with $_POST['code'], and stripslashes() with $_POST['code'] (just in case I want to make a php page where the correct answer is.. code perhaps? like <img src="blah.extension" />) I declared a heredoc and assigned it to a variable, then used nl2br on that variable to make THAT variable multiline. End result is something along the lines of ... $hdoc = <<<EOD Some Text EOD; $hdoc = nl2br($hdoc); if(isset($_POST['submit'])) { $code = stripslashes(nl2br($_POST['code'])); if($code == $hdoc) { echo 'You win'; } else { echo 'You lose'; } } Then of course we just have a generic form declared after the block of PHP code... <form method="POST"> <textarea name="code" row=10 col=30></textarea> <br /> <input type="submit" name="submit" value="Submit" /> </form> Any ideas on why I am always getting the echo of "Lose"? Any replies are strongly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/ Share on other sites More sharing options...
radar Posted July 22, 2010 Share Posted July 22, 2010 First thing you could want to do right before your if($code == $hdoc) { is put: echo $code.":".$hdoc; and make sure you have what you want there in both of them... then you can adjust your coding from there to get the proper result. Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089802 Share on other sites More sharing options...
cw22 Posted July 22, 2010 Author Share Posted July 22, 2010 @radar: thanks for the reply. I have done this though and lets say my hdoc is: hey there And I post: hey there When I echo the results, it shows up as: hey there hey there But, I still am getting "lose". I thought maybe it had something to do with $_POST changing one tad little thing, but I still can't find out what. Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089806 Share on other sites More sharing options...
DavidAM Posted July 22, 2010 Share Posted July 22, 2010 Use your browser's "View Source" feature to look at the output. Or, even better, use var_dump() on each variable, instead of echo. The browser will hide spaces, newlines, etc. from view. A newline will still be difficult to see in View Source. var_dump() will give you the variable type, length and content. You should be able to see the true difference. Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089816 Share on other sites More sharing options...
radar Posted July 22, 2010 Share Posted July 22, 2010 check the html output of it, not just the display results. Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089825 Share on other sites More sharing options...
cw22 Posted July 22, 2010 Author Share Posted July 22, 2010 New code: <?php $hdoc = <<<EOD Some Text EOD; if(isset($_POST['submit'])) { $hdoc = nl2br($hdoc); $chk = stripslashes(nl2br($_POST['code'])); echo var_dump($hdoc); echo '<br />'; echo var_dump($chk); } ?> <form method="POST"> <textarea name="code" row=10 col=30></textarea> <br /> <input type="submit" name="submit" value="Submit" /> </form> I get outputted: string(15) "Some Text" string(16) "Some Text" Any idea why string(16) for the input? I'm not inputting any extra space. Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089832 Share on other sites More sharing options...
cmor Posted July 22, 2010 Share Posted July 22, 2010 Maybe a difference between \r\n and \n? Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089838 Share on other sites More sharing options...
cw22 Posted July 22, 2010 Author Share Posted July 22, 2010 Wouldn't there be no difference since I am using nl2br() for both vars? Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089846 Share on other sites More sharing options...
DavidAM Posted July 23, 2010 Share Posted July 23, 2010 nl2br() might leave the CR ( "\r" ) in there, I'm not sure. You might want to do a str_replace() on it just to be safe: $chk = str_replace("\r", "", stripslashes(nl2br($_POST['code']))); Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089879 Share on other sites More sharing options...
cw22 Posted July 23, 2010 Author Share Posted July 23, 2010 David, you're right. Just for future reference - why did it leave \r in there for $chk and not $answer? Marked: Solved. Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089914 Share on other sites More sharing options...
DavidAM Posted July 23, 2010 Share Posted July 23, 2010 If your source code file is saved in unix format it does not have CR's (\r) in it. Windows systems end lines with CR-LF while unix based systems use only NL. So, your HEREDOC does not have CR's in it. I'm guessing your browser was running on a Windows system so it probably put the CR's in the response. Hmmm, maybe I should add that to my POST parameter cleanup. Always remove "\r" from any TEXTAREA's? I'll have to look at that. Quote Link to comment https://forums.phpfreaks.com/topic/208586-checking-multiline-_post-against-predefined-var/#findComment-1089969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.