0
私は、ユーザーがJavaScriptを使用して特別な行を追加できるようにするHTMLフォームを用意しています。投稿用の動的行を追加するJavaScriptは、データの最初の行のみを返します
フォーム:
<form method="post" action="send.php">
<table id="table">
<tr><th>job</th><th>comment</th></tr>
<tr>
<td><textarea name = "job[]"></textarea></td>
<td><textarea name = "comment[]"></textarea></td>
<tr>
</table>
<input type ="button" value="add entry" onclick="add('table')"/>
<input type ="submit" id="submit" value="submit"/>
はJavaScript:
function add(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
var newentry = document.createElement('textarea');
newentry.type = "text";
newcell.appendChild(newentry);
}
}
PHPコード
$job = $_POST['job'];
$comment = $_POST['comment'];
//code to connect to database
$add_stmt = $mysqli->prepare("INSERT INTO job (job, comment) VALUES (?, ?)");
$foreach($job as $a => $b) {
$add_stmt->bind_param("ss", $job[$a], $comment[$a]);
$add_stmt->execute();
}
私は "エントリを追加" ボタンをクリックすると、行が正常になります作成した。ただし、最初の行のデータのみがデータベースに送信されます。
echo "row count is" . count($_POST['job']);
私はいくつかの行のデータがあっても "行数は1"です。 誰かが自分のコードで何が間違っているのか理解できますか?本当にありがとう!
ありがとうMateusz。しかし、あなたのコードを試してみましたが、行数はまだ1です。$ job [$ a]は、私が入力したデータの代わりに "配列"になりました。 – nubicurio
EDIT:$ job [$ a]は私が入力したデータであり、Arrayではありません(私は何かを誤って入力しました)。 – nubicurio
@nubicurio私は、私のために働くコードを追加しました。動的データを持つ配列を正しく出力します。 –