私のフォームには、名前、年齢、場所を収集し、フォームを送信した後に出力をエコーするための 3 つのテキスト領域があります。
私が直面している問題:
たとえば、次のようなデータを入力している場合
名前 :
George
Tom
年:
30
40
場所:
SA
IN
私は次のような出力を期待しています:
My name is George, age 30, from SA
My name is Tom, age 40, from IN
しかし、それは私に出力を与えます:
My name is George, age 30, from SA
My name is George, age 30, from IN
My name is George, age 40, from SA
My name is George, age 40, from IN
My name is Tom, age 30, from SA
My name is Tom, age 30, from IN
My name is Tom, age 40, from SA
My name is Tom, age 40, from IN
各テキストエリアの各行データを処理して出力するにはどうすればよいですか?
私のコード: PHP FIDDLE
<form id="main" name="main" action="#text" method="post" >
<div class="input">Name<span><textarea class="textarea" id="main-name" name="main-name" type="text"></textarea></span> </div>
<div class="input">Age<span><textarea class="textarea" id="main-age" name="main-age" type="text"></textarea></span> </div>
<div class="input">Place<span><textarea class="textarea" id="main-place" name="main-place" type="text"></textarea></span> </div>
<div class="submit-clear">
<input id="generate" type="submit" name="script" value="create my symcli script" />
<input onClick="clear_all();" id="clear" type="submit" name="clear" value="clear" />
</div>
</form>
<div id="output-main">
<form id="main2" name="main2" action="#text" method="post">
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value=" ">
<?php
$names1 = $_POST['main-name'];
$ages1 = $_POST['main-age'];
$places1 = $_POST['main-place'];
$names = explode("\n", $names1);
$ages = explode("\n", $ages1);
$places = explode("\n", $places1);
foreach ($names as $key => $name) {
foreach ($ages as $key => $age) {
foreach ($places as $key => $place) {
$name = trim($name);
$age = trim($age);
$place = trim($place);
echo "My name is $name, age $age, from $place".PHP_EOL;
}}}
?>
</textarea>
</form>
</div>