budthapa
budthapa

Reputation: 1032

print the value to textarea,

I am a beginner and self learner. This might be easy question but it is creating some problem to me. I think I missed something somewhere.

When I write some text in textarea and hit Find and replace button(leaving other two fields empty), the value captured from textarea should appear in the textarea itself and error message should appear outside the textarea. textarea should not be blank. I think message are working fine.

I am not sure if the problem is with the button or action='' in form.

<?php
//find and replace string
//using str_replace(), takes three parameters, $findword, $wordtoreplace, $userinput

if(isset($_POST['text']) && isset($_POST['find']) && isset($_POST['replace'])){
    $paragraph=nl2br(htmlentities($_POST['text']));
    $find_string=$_POST['find'];  //assign the value to be found to the variable
    $replace_string=$_POST['replace']; //assign the value to be replaced

    if(empty($paragraph)){
      echo 'No text to search for.';
    }
    elseif(empty($find_string)){
      echo 'Enter some text to find.';
    }
    elseif(empty($replace_string)){
      echo 'Enter some text to replace with.';
    }
    else{
      echo str_replace($find_string, $replace_string, $paragraph);
   }
}
?>
<form action='' method='POST'>
   <textarea name='text' rows=20 cols=100 value='<?php echo $paragraph; ?>'></textarea
   <br />
   <label>Search For</label>
   <input name='find' value='<?php echo $find_string; ?>'></input>
   <br />
   <label>Replace with</label>
   <input name='replace' value='<?php echo $replace_string; ?>'></input>
   <br />
   <button>Find and Replace</button>
</form>

Upvotes: 0

Views: 5071

Answers (2)

Deep Dizzy
Deep Dizzy

Reputation: 303

You have to put the output in the midle of textarea open and close tag:

<textarea name='text' rows=20 cols=100><?php echo $paragraph; ?></textarea>

Upvotes: 1

Reeno
Reeno

Reputation: 5705

<textarea> doesn't have the value attribute. Provide the content like this:

<textarea name='text' rows='20' cols='100'><?php echo $paragraph; ?></textarea>

Btw, the closing bracket of </textarea was missing

Upvotes: 3

Related Questions