dポイントプレゼントキャンペーン実施中!

初心者です。
現在、PHPでforを利用し、繰り返しフォームを表示
させた後、その表示されたフォームへ入力をし、更に
違うPHPで入力された値を出力させようとしているのですが、
最終の出力が出来ずに悩んでおります。
繰り返しでフォームを作成するまでは出来たのですが、
どなたか御助力頂けたら助かります。
----------------------------------------------------
<form.html>
<html>
<head>
<title>it1</title>
</head>
<body>
<form name="form" action="./it.php" method="POST">
<b>ラジオ選択<br>
<input name="machine" type="radio" value="24">24<br>
<input name="machine" type="radio" value="48">48<br>
<input name="machine" type="radio" value="24">50</b><br><br>
</body>
<input type="submit" name="Submit" value="設定">
</form>
</html>
------------------------------------------------------
<it.php>
<?php
$a = $_REQUEST["machine"];

?>

<html>
<body>
<p>入力テキストボックス表示</p>

<?php
for ($c=0; $c<$a; $c++){
$var[$c] += $var[$c];
$dest[$c] += $dest[$c];
$no[$c] += $c+1;
}
?>
<table>
<tr>
<td><center>
<form name="form" action="./it2.php" method="POST">
<td>
<center>
<b>vlan</b><br>
<?php
for ($i=0; $i<count($var); $i++) {
"<form name="form" action="./it2.php" method="POST">"
print("fa0/${no[$i]}<input type=\"text\" name=\"${var[$i]}\" size=\"3\" maxlength=\"3\"><br>\n");
}
?>
</td>
<td>
<b>description</b><br>
<?php
for ($i=0; $i<count($dest); $i++) {
print("<input type=\"text\" name=\"${dest[$i]}\" size=\"15\" maxlength=\"15\"><br>\n");
}
?>
</td>
</tr>
</table>
<input type="submit" name="Submit" value="設定">
</form>
</body>
</html>
--------------------------------------------------------
以降の出力ソースに関して、難儀中。

A 回答 (4件)

it.phpのinput nameを変更しないといくらit2.phpを修正してもダメです。



ソースを書きましたので参考にしてください。

<form.html>
<html>
<head>
<title>it1</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
</head>
<body>
<form name="form" action="./it.php" method="POST">
<b>ラジオ選択</b>
<br>
<input name="machine" type="radio" value="24">24<br>
<input name="machine" type="radio" value="48">48<br>
<input name="machine" type="radio" value="50">50<br>
<input type="submit" value="設定">
</form>
</body>
</html>

<it.php>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<body>
<p>入力テキストボックス</p>
<table>
<?php
$a = $_POST["machine"];
print "<form action=\"it2.php\" method=\"POST\">";
print "<tr><td><b>vlan</b></td><td><b>description</b></td></tr>";
print "<form name=\"form\" action=\"./it2.php\" method=\"POST\">";
for ($i=1; $i<=$a; $i++) {
print "<td>fa0/$i"."<input type=\"text\" name=\"var".$i."\" size=\"3\" maxlength=\"3\"></td>";
print "<td><input type=\"text\" name=\"dest".$i."\" size=\"15\" maxlength=\"15\"></td></tr>\n";
}
print "</table>";
print "<input type=\"submit\" value=\"送信\">";
print "</form>";
?>
</body>
</html>

input nameは「ver1,ver2....」「dest1,dest2...」で出力されます。
    • good
    • 0
この回答へのお礼

ありがとうございます。非常に参考になりました。
配列ではなく、単一の変数として扱えばよかったのですね。
配列に拘る余り手詰まりになってました。

お礼日時:2007/03/29 09:41

it.phpで表示した内容をPOSTで送る場合はname値にユニークな値を入れないと


送信した時に値を受け取ることができません。

<input type=\"text\" name=\"${ver[$i]}\" size=\"3\" maxlength=\"3\">
${var[$i]}はいくら$iをカウントしてもずっと値は0のままです。
${dest[$i]}も同様にずっと0です。

it.phpで渡したname値をit2.phpで受け取る場合も同様にname値をカウントしながらPOSTで受け取ればいいです。

it2.php
<?php
$a = $_POST["var"];
$b = $_POST["dest"];
$c = $_POST["no"];
?>
↑これでは絶対無理です。

この回答への補足

確かに普通に受け取るような記述では表示できませんでした。
受け取る際もカウントしながらと言うことなので、
受け取りのforを書いてみたのですが、
0の羅列になってしまいます。
------------------------
<it2.php抜粋>

<?php
for ($c=0; $c<48; $c++){
$a[$c] += $_POST["{var[$c]}"];
echo $a[$c];
}
?>

補足日時:2007/03/28 09:35
    • good
    • 0
この回答へのお礼

再度試してみますありがとうございます。

お礼日時:2007/03/29 09:42

細かいソースは置いておいて。

。。。
中程のforの中にある"<form name="form" action="./it2.php" method="POST">"
は何でしょう?
必要ありませんよね。

基本的に、POST/GETで送信される値は、<form ....></form>の中にある値です。ページ上に<form></form>はいくつでも書く事が出来ますが、実際に送信されるのはsubmitが押された<form></form>の中の変数だけです。
すなわち、#1さんのソースでは最後の一つしか送信されない筈です。

どんな変数が送信されてきたか、については
print_r($_POST)
等で見れば分かります。

以下蛇足です。
あまり関係無い事ですが、
for ($i=0; $i<count($var); $i++)
とすると、ループ1回につき毎回count関数が呼び出されてしまいますので効率が悪いです。出来れば
$cnt = count($var);
for ($i=0; $i<$cnt; $i++)
とした方が良いと思います。まぁ、100回やそこらでは影響無いでしょうが(笑。
    • good
    • 0
この回答へのお礼

若干読み込みが遅かったので、
その点も考慮してみます。ありがとうございます。

お礼日時:2007/03/29 09:43

ソースが間違いだらけなので修正しておきました。


ここまでPHPが書けるのでしたら以降のソースも大丈夫でしょう。

添削はいたします。
----------------------------------------------------
<form.html>
<html>
<head>
<title>it1</title>
<meta http-equiv="Content-Type" content="text/html; charset=SHIFT-JIS">
</head>
<body>
<form name="form" action="./it.php" method="POST">
<b>ラジオ選択</b>
<br>
<input name="machine" type="radio" value="24">24<br>
<input name="machine" type="radio" value="48">48<br>
<input name="machine" type="radio" value="24">50<br>
<input type="submit" name="Submit" value="設定">
</form>
</body>
</html>

------------------------------------------------------
<it.php>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=SHIFT-JIS">
<body>
<p>入力テキストボックス表示</p>
<?php
$a = $_POST["machine"];
for ($c=0; $c<$a; $c++){
$var[$c] += $var[$c];
$dest[$c] += $dest[$c];
$no[$c] += $c+1;
}
?>
<table>
<tr>
<td><center>
<form name="form" action="./it2.php" method="POST">
<td>
<center>
<b>vlan</b><br>
<?php
for ($i=0; $i<count($var); $i++) {
print "<form name=\"form\" action=\"./it2.php\" method=\"POST\">";
print("fa0/${no[$i]}<input type=\"text\" name=\"${var[$i]}\" size=\"3\" maxlength=\"3\"><br>\n");
}
?>
</td>
<td>
<b>description</b><br>
<?php
for ($i=0; $i<count($dest); $i++) {
print("<input type=\"text\" name=\"${dest[$i]}\" size=\"15\" maxlength=\"15\"><br>\n");
}
?>
</td>
</tr>
</table>
<input type="submit" name="Submit" value="設定">
</form>
</body>
</html>

この回答への補足

修正ありがとうございます。
it.phpで表示させたフォームに適当な文字を
入力して次のit2.phpへ出力しようとしたのですが
やはりうまくいきません。
forで繰り返した表示させたフォームの値を
受け取る事はできないのでしょうか。
-------------------------------------------------
<it2.php>(結果表示試し用)
<html>
<meta http-equiv="Content-Type" content="text/html; charset=SHIFT-JIS">
<body>
<p>出力結果</p>
<?php
$a = $_POST["var"];
$b = $_POST["dest"];
$c = $_POST["no"];
?>

<?php

echo $a;
echo $b;
echo $c;

?>
</body>
</html>

補足日時:2007/03/27 09:38
    • good
    • 0
この回答へのお礼

参考にさせて頂きました。
ありがとうございます。

お礼日時:2007/03/29 09:44

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!