JS コード ( http://jsfiddle.net/3mcm2/ ) のフィドルをアップロードしました。その一番下に、PHP ドキュメントで JS を呼び出す方法があります。スクリプトを実行するには、一番下の PHP コード コメントを削除するだけです。PHPでどのように出力しているかを確認するために、それを追加したかっただけです。また、これらの最後のコメントの上には、.js ファイル内の 3 行のコメントがあります。これらは、すべてがどのように見えるかをよりよく理解するために、PHP が何をエコーしているかを確認するためにあります。
/* The following is what is in my .js file: (see the bottom of this script for part of
what is in my PHP file) */
var f = document.createElement("form");
f.setAttribute('method', "get");
f.setAttribute('action', "index.php");
var Category = (function () {
var categoryCount = 0;
function elem(tag) { // shortcut
return document.createElement(tag);
}
function text(str) { // shortcut
return document.createTextNode(str);
}
function Category(node) {
var self = this;
this.categoryId = ++categoryCount;
// make add button
this.addButton = elem('button');
this.addButton.appendChild(text('Add Textbox'));
this.addButton.addEventListener('click', function () {
self.addTextbox();
});
// make wrapper
this.wrapper = elem('section');
this.wrapper.setAttribute('id', 'cat'+this.categoryId);
this.wrapper.appendChild(this.addButton);
// make textboxes
this.textboxes = [];
this.addTextbox();
// append to document
if (node) {
this.append(node);
}
}
Category.prototype.addTextbox = function () {
var e = document.createElement("input");
e.setAttribute('name', 'cat-'+this.categoryId+'-textbox[]');
f.appendChild(e); // this is where each textbox is supposed to be added to the form...
this.textboxes.push(e);
this.wrapper.insertBefore(e, this.addButton);
};
Category.prototype.append = function (node) {
return node.appendChild(this.wrapper);
};
return Category;
}());
var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"submit");
s.setAttribute('value',"Submit");
f.appendChild(s);
//var cat1 = new Category(document.body);
//var cat2 = new Category(document.body);
//document.getElementsByTagName('body')[0].appendChild(f);
上記のコメントは、このスクリプトが何を行っているかを確認するためのものであり、これらの 3 行は実際には私の .js ファイルにはありません。次のコメントは私の PHP ファイルの一部であり、上記のコメントを出力するだけです。
$counter = 0;
echo '<script type="text/javascript" src="js/categories.js"></script>';
foreach ($catArr as $category) {
$counter++;
echo 'do<script>var cat'.$counter.' = new Category(document.body);</script>';
}
echo "<script>document.getElementsByTagName('body')[0].appendChild(f);</script>";
私の問題は、JS で作成したフォームでは、GET がデータを配信していないことです。私のphpページは単にindex.phpからindex.phpに移行していますか? 疑問符に続くテキストボックス変数ではなく、疑問符を使用します。何らかの理由で、フォームは作成されたテキストボックスまたはその名前を見つけられません。私を助けてください。