debite
debite

Reputation: 444

Compare submitted textarea with initial value

I want to output an array (from a text-file) into a textarea, let users change it and afterwards save that array back to the text-file.

<textarea name="text"><? echo(html_entity_decode(implode("\n", $array))) ?></textarea>

Before the array gets stored back to the text-file, I want to make sure, that actually something was changed.

if ($_POST['text'] != html_entity_decode(implode("\n", $array))) {
    // SAVE THE TEXT BACK TO THE FILE
}

But the above condition always returns true, even when no change was submitted.

What am I doing wrong? Is there anything else done with the input while submitting?

Upvotes: 0

Views: 445

Answers (1)

Michael Podrybau
Michael Podrybau

Reputation: 165

Maybe try

if(strcmp(preg_replace( "/\r|\n/", "", $_POST['text']), html_entity_decode(implode("\n", $array))) != 0){
    // SAVE THE TEXT
}

That's getting a bit hard to read, but essentially preg_replace should strip the newline characters out of your POST data for purposes of comparing it to your original array. Hope this works for you!

Upvotes: 1

Related Questions