Php 从带有序列化数组的jquery对象返回文件名
从jquery脚本获取文件名时遇到一些问题 我有多个隐藏字段,其中包含表单中fileinput中的文件名,我使用此字段获取文件名:Php 从带有序列化数组的jquery对象返回文件名,php,javascript,jquery,html,arrays,Php,Javascript,Jquery,Html,Arrays,从jquery脚本获取文件名时遇到一些问题 我有多个隐藏字段,其中包含表单中fileinput中的文件名,我使用此字段获取文件名: var fn = $('input[name="filename[]"]').serializeArray(); var post_var = {'filename':fn}; 然后: return JSON.stringify({ "filename": post_var }); 这给了我类似的东西: [Object { name="filename[]"
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"}]
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();
但我不确定如何使用我当前的php脚本获取“value”中的内容,该脚本如下:
[Object { name="filename[]", value="703640495-qr-flo.png"}, Object { name="filename[]", value="703640495-qr-pgl.png"}]
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();
}
}
尝试:
您的文件位于
$filename[0]['filename']
,因此您可以:
$filename
变量,该变量返回位于$filename[0]['filename']
的数组foreach( array_shift($filename) as $file ) {
$file['name']; // the file name (always filename[] so ignore it)
$file['value']; //the file value (the real filename)
}
好极了!与我的问题中编辑的添加内容顺利配合!谢谢!