jquery スクリプトからファイル名を取得する際に問題が発生しています。
フォームのファイル入力からのファイル名を含む複数の非表示フィールドがあり、これを使用してファイル名を取得します。
var fn = $('input[name="filename[]"]').serializeArray();
var post_var = {'filename':fn};
その後:
return JSON.stringify({
"filename": post_var
});
それは私に次のようなものを与えます:
[Object { name="filename[]", value="703640495-qr-flo.png"}, Object { name="filename[]", value="703640495-qr-pgl.png"}]
しかし、次のような現在のphpスクリプトで「値」のコンテンツを取得する方法がわかりません。
foreach($filename as $key => $value) {
$imgrow = $this->db->dbh->prepare('INSERT INTO '. $this->config->db_prefix .'_images (aid, image) VALUES (:aid, :image)');
$imgrow->bindValue(':aid', $id);
$imgrow->bindParam(':image', strtolower($value));
$imgrow->execute();
}
var_dump($filename) の場合、次のようになります。
array(1) {
[0]=>
object(stdClass)#104 (1) {
["filename"]=>
array(2) {
[0]=>
object(stdClass)#105 (2) {
["name"]=>
string(10) "filename[]"
["value"]=>
string(20) "703640495-qr-flo.png"
}
[1]=>
object(stdClass)#106 (2) {
["name"]=>
string(10) "filename[]"
["value"]=>
string(20) "703640495-qr-pgl.png"
}
}
}
}
解決:
foreach(array_shift($filename) as $file ) {
foreach ($file as $key => $value) {
$imgrow = $this->db->dbh->prepare('INSERT INTO '. $this->config->db_prefix .'_images (aid, image) VALUES (:aid, :image)');
$imgrow->bindValue(':aid', $id);
$imgrow->bindParam(':image', strtolower($value->value));
$imgrow->execute();
}
}