2012-02-28 87 views
1

我已經拍攝了2個文本框和1個文本區域 用戶將通過搜索框 搜索內容並提供應該用哪個詞替換它。如何用下面提到的代碼中的PHP腳本替換文本區域的內容

之情況1:

我要替換「好」字與「壞」
但這代碼不會替換文本區域的內容。 它而與新的替換字符串

有什麼解決辦法?追加

<body> 

<form id="form1" name="form1" method="post" action=""> 

<p> 
<label for="search">Search :</label> 
<input type="text" name="search" id="search" /> 
</p> 

<p> 
<label for="replace">Replace</label> 
<input type="text" name="replace" id="replace" /> 
</p> 

<p><Br /> 
<input type="submit" name="submit" id="submit" value="Submit" /> 
<label for="textarea"></label> 
</p> 

<p><br /> 
<textarea name="textarea" id="textarea" cols="45" rows="5" > 
"Good morning how are you today are you feeling Good."; 
<?php 

if(isset($_POST["submit"])) 
{ 
    $search = $_POST["search"]; 
$replace = $_POST["replace"]; 
$textarea = $_POST["textarea"]; 

$newtext = str_replace($search,$replace,$textarea); 
    echo $newtext; 

} 

?> 
    </textarea> 
    </p> 
</form> 

</body> 
</html> 
+1

請出示的是如何出錯的例子(真實數據) – 2012-02-28 10:38:53

+0

你怎麼實際上將文本推入textarea? – shanethehat 2012-02-28 10:39:43

+0

這應該很好。也許顯示更多代碼 – Vytautas 2012-02-28 10:39:47

回答

0

您需要在PHP中使用條件語句來控制是否顯示默認文本。目前你只是在它後面添加動態文本。

<textarea name="textarea" id="textarea" cols="45" rows="5" > 
    <?php 
     if(isset($_POST["submit"])) { 
      $search = $_POST["search"]; 
      $replace = $_POST["replace"]; 
      $textarea = $_POST["textarea"]; 

      $newtext = str_replace($search,$replace,$textarea); 
      echo $newtext; 
     } else { 
      echo "Good morning how are you today are you feeling Good."; 
     } 
    ?> 
</textarea> 
0

HTML腳本

<html> 
<body> 
<form action="srch.php" method="post"> 
Find: <input type="text" name="find" value=><br><br> 
Replace: <input type="text" name="replace" ><br><br> 
<input type="submit" value="Replace"/><br><br> 

<textarea name="maintext" rows="8" cols="80"></textarea> 
</form> 
</body> 
</html> 

php腳本.......

<html> 
<body> 

<?php 
$find= $_POST['find']; 
$replace= $_POST['replace']; 
$text= $_POST['maintext']; 
if (isset($find) && isset($replace)) 
{ 
$newtext= str_replace($find, $replace, $text); 
} 
?> 
<form action="" method="post"> 
Find: <input type="text" name="find" value='<?php echo $find; ?>'/><br><br> 
Replace: <input type="text" name="replace" value='<?php echo $replace; ?>'/><br><br> 
<input type="submit" value="Replace"/><br><br> 

<textarea name="maintext" rows="8" cols="80"><?php echo $newtext; ?></textarea> 
</form> 

</body> 
</html> 
相關問題