Jump to content

Checking multiline $_POST against predefined var


cw22

Recommended Posts

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.

@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.

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.

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.

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.

 

 

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.