0

I have a html form which has 2 textbox and a text area. I am fetching the data from the form using a php code to display it on another page.

html form code:

    <html>

<form name="addsg" method="POST" action="validate.php">

<div class="label">name</div>
<div class="response"><span><textarea class="textarea" name="name"></textarea></span></div>


<div class="label">age</div>
<div class="response"><span><input class="textbox" name="age" type="text" size="5" maxlength="5" value="" /></span></div>


<div class="label">place</div>
<div class="response"><span><input class="textbox" name="place" type="text" value="" /></span></div>

<div class="submit_section button">
<input id="generate" type="submit"  name="script" value="generate" />
</div>

</form>
</html>

PHP code:

<?php

    if (!empty($_POST['name']) 
    && !empty($_POST['age'])
    && !empty($_POST['place']))

   {
 echo '<textarea name="textarea" id="textarea" cols="100" rows="5" readonly>';

      echo "{$_POST['name']},{$_POST['age']} years old, from {$_POST['place']}";
     echo '</textarea>';
   }
 ?>

If I put only one name in name text area column, I will get output like this

anoop,26 years old, from IN

But if I put more than one name in the name column(with same age and place), output showing like this

anoop
Tom,26 years old, from IN

Age and place are showing only for one name, not for all. I would like to get output like this

 anoop,26 years old, from IN
 Tom,26 years old, from IN

any suggestions ?

4

4 回答 4

0

试试这个,应该可以的。

<?
$lines = array();
$names = explode("\n", $_POST['name']);

foreach($names as $name)
  $lines[] = $name.', '.$_POST['age'].' years old, from '.$_POST['age'];
?>

<html>
<body>
<textarea name="textarea" id="textarea" cols="100" rows="5" readonly>
<?php implode("\n", $lines); ?>
</textarea>
</body>
</html>
于 2013-07-25T11:14:09.773 回答
0

您可以为 name 放置多个文本框并将 name 声明为 array 。

例如:

<input type="text" name="ex[]" id="ex"/>
<input type="text" name="ex[]" id="ex1"/>

PHP:

for($1=0;$i<count($_POST['ex']);$i++){

echo $_POST['ex'][$i];

}
于 2013-07-25T10:58:51.390 回答
0
<?
$name_array = explode( "\n", $_POST['name'] ); //use the new line char as the separator
?>

<textarea name="textarea" id="textarea" cols="100" rows="5" readonly>
<?
foreach( $name as $name_array )
  echo $name.', '.$_POST['age'].' years old, from '.$_POST['age']."<br/>\n";
?>
</textarea>
于 2013-07-25T11:23:56.863 回答
0

尝试按顺序使用这两个函数: 1- nl2br($_POST['name']) 2- strip_tags()

也许是一些隐藏的 html 标签导致

于 2013-07-25T11:24:49.247 回答