2012-02-28 91 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> 
相关问题