gpt4 book ai didi

javascript - 将数组从 HTML 表单传递到 PHP,然后传递到 WordPress

转载 作者:行者123 更新时间:2023-12-03 05:23:50 24 4
gpt4 key购买 nike

我试图将数组从表单传递到 php,一切正常,直到在 php 文件中到达 Foreach 循环,在其中仅运行一次(在所有其他循环周期 var_dump 给出 NULL)。

HTML:

 <div id="dynamicInput">
<div class="project">
<span>Project images</span> <input id="pr_img" multiple type='file' name='my_file_upload0[]'></br>
<input id="pr_name" type='text' placeholder='Project name' name='pname[]'>
<textarea id="pr_desc" cols="40" rows="8" placeholder='Project description' name='pdescr[]'></textarea>
</div>


<script>
var counter = 1;
var limit = 99;
function addInput(divName){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.className = "project";
newdiv.innerHTML = " <span>Project images</span> <input multiple id='pr_img' type='file' name='my_file_upload" + counter + "[]'></br><input id='pr_name' type='text' placeholder='Project name' name='pname[]'> <textarea cols='40' rows='8' type='text' id='pr_desc' placeholder='Project description' name='pdescr[]'></textarea>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
</script>

</div>

PHP:

 require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );


$combo = array_combine($_POST[pname], $_POST[pdescr]);

$countt=0;

foreach ($combo as $nam => $descri) {

$projectid =wp_insert_post( array(
'post_title' => $nam,
'post_type' => 'Projects',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => $descri,
'post_status' => 'publish',
'post_author' => $userrid,
'menu_order' => 0,

) );
/*---------------------This is the place where it doesnt work------------*/

$files = $_FILES["my_file_upload".$countt.""];
++$countt;

/*-----------------------------------------------------------------------*/
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_image_upload" => $file);
//var_dump($_FILES);
foreach ($_FILES as $file => $array) {
// $newupload = my_handle_attachment($file,$post_id);
$attach_id = media_handle_upload( $file, 0);


}

}
unset($files);
}

add_post_meta( $projectid, 'project_pics', $attach_id, true );
if ( is_wp_error( $attachment_id ) ) {
echo "Error while uploading images";
} else {


}

}

编辑:$_FILES数组内容:

 ["my_file_upload0"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(6) "bg.bmp"
[1]=>
string(6) "bg.png"
}
["type"]=>
array(2) {
[0]=>
string(9) "image/bmp"
[1]=>
string(9) "image/png"
}
["tmp_name"]=>
array(2) {
[0]=>
string(38) "/customers/c/e/b/********/phpO2lni7"
[1]=>
string(38) "/customers/c/e/b/********/phpcIObqF"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(64)
[1]=>
int(10946)
}
}
["my_file_upload1"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(12) "job-icon.png"
[1]=>
string(8) "nova.png"
}
["type"]=>
array(2) {
[0]=>
string(9) "image/png"
[1]=>
string(9) "image/png"
}
["tmp_name"]=>
array(2) {
[0]=>
string(38) "/customers/c/e/b/********/phpZKskyd"
[1]=>
string(38) "/customers/c/e/b/********/phpbMlgIL"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(15213)
[1]=>
int(10906)
}

最佳答案

查看$_FILES的内容。我的猜测是您只会看到 my_file_upload0,就像在循环中您尝试循环遍历 my_file_upload0my_file_upload1 my_file_upload2

您需要循环遍历 $_FILES['my_file_upload0'] 本身,因为这应该是一个数组,其中包含表单上文件输入中选择的所有文件。

   foreach($_FILES['my_file_upload0'] as $key => $files) {
// Code
}

编辑 - 为了清楚起见,我实际上建议将 my_file_upload0 的名称更改为 my_file_upload

第二次编辑 - 我已经复制了您对此处提供的数组所做的操作 - http://phpfiddle.org/main/code/j88g-i2ci这会毫无问题地执行 for 循环两次。

关于javascript - 将数组从 HTML 表单传递到 PHP,然后传递到 WordPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41247270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com