gpt4 book ai didi

javascript - 如何通过表单数据和 Ajax PHP 保存数组

转载 作者:行者123 更新时间:2023-12-03 03:36:59 26 4
gpt4 key购买 nike

如何通过formdata将多选的所有值与文件一起保存。它总是显示文件“foreach() 的参数无效”。即使文件已保存到各自的文件夹中,但多选选项并未进入 foreach 并保存到数据库中。

警告:foreach() 中提供的参数无效

我正在使用以下代码。

HTML

<!--JQUERY-->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<!--Bootstrap 3.3.7-->
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!--Font-awesome 4.7.0-->
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<button class="btn btn-success addnewjob" data-toggle="modal" data-target="#add_new_record_modal">Add New Job</button>


<!-- Modal - Add New Record/User -->
<div class="modal fade bs-example-modal-lg" id="add_new_record_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Add New Record</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<select name="test[]" id="sbTwo" multiple="multiple" class="form-control">
<option value="PAPER CUP">PAPER CUP</option>
<option value="PAPER BOWL">PAPER BOWL</option>
<option value="PAPER PLATE">PAPER PLATE</option>
<option value="PAPER BAG">PAPER BAG</option>
</select>
</div>

<div class="col-xs-12 col-sm-12 col-md-3 col-lg-3">
<label for="jspec_filedoc">DOC Files</label>
<div class="input-group">
<label class="input-group-btn">
<span class="btn btn-info">
<span class="fa fa-file-image-o"></span>
<input id="jspec_filedoc" name="jspec_filedoc" type="file" accept=".doc, .docx,.xlsx" style="display: none;" multiple>
</span>
</label>
<input type="text" class="form-control" id="existingaifile" placeholder="Document File" readonly>
</div>
</div>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary" onclick="addRecord()">Add Record</button>
</div>
</div>
</div>
</div>
<!-- // Modal -->

脚本

function addRecord() {

var conf = confirm("Are you sure, do you really want to APPROVE this file?");
if (conf == true) {
// get values

$('#sbTwo option').prop('selected', true); //select all data in multiselect
var test = $("#sbTwo").val();
var pj_schednum = $('#pj_schednum').val();
var jspec_filedoc = $('#jspec_filedoc').prop('files')[0];

var form_data = new FormData();
form_data.append('jspec_filedoc', jspec_filedoc);
form_data.append('pj_schednum', pj_schednum);
alert(form_data.append('test', test));

$.ajax({
url: 'ajax/createjob.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'POST',
success: function(data) {
alert(data);
}
});
}
}

$(document).ready(function() {
$(function() {

$(document).on('change', ':file', function() {
var input = $(this),
numFiles = input.get(0).files ? input.get(0).files.length : 1,
label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
input.trigger('fileselect', [numFiles, label]);
});

$(document).ready(function() {
$(':file').on('fileselect', function(event, numFiles, label) {
var input = $(this).parents('.input-group').find(':text'),
log = numFiles > 1 ? numFiles + ' files selected' : label;

if (input.length) {
input.val(log);
} else {
if (log) alert(log);
}
});
});
});
});

PHP

<?php
if(isset($_POST['pj_schednum']) && isset($_POST['test']) && isset($_FILES['jspec_filedoc']))
{
// include Database connection file
include("db_connection.php");

//get current date with 24 hours time format
date_default_timezone_set('Asia/Manila');
$a = date('m/d/Y H:i:s');
$b = date('m/d/Y h:i A', strtotime($a));
$z = date('mdYHis');

// get values
$file_name3 = explode(".", $_FILES['jspec_filedoc']['name']);
$new_name3 = rand() . '.'. $file_name3[1];
$sourcePath3 = $_FILES["jspec_filedoc"]["tmp_name"];
$savethisname3='JT'.$z.$new_name3;
$targetPath3 = "../bin/jobs_attachments/docfile/".$savethisname3;
move_uploaded_file($sourcePath3, $targetPath3);


$i = 0;
foreach ($_POST['test'] as $operation_processname){
echo $operation_processname."\n";

$operation_step=$i++;//The steps

$query = "INSERT INTO operation (o_name, o_step, o_doc, o_added) VALUES('$operation_processname', '$operation_step','$savethisname','$b')";
if (!$result = mysqli_query($db, $query)) {
exit(mysqli_error());
}
}

echo "1 Record Added!";
}
?>

结果

+-----------------------------------+
+o_name| o_step| o_doc| o_added |
+-----------------------------------+
|PAPER CUP |0 |doc1.docx | 01/11/2017 05:16:45 PM|
|PAPER BOWL|0 |doc2.docx | 01/12/2017 05:16:45 PM|
|PAPER PLATE|0 |doc3.docx | 01/13/2017 05:16:45 PM|
|PAPER BAG|0 |doc4.docx | 01/14/2017 05:16:45 PM|
+------------------------------------------------+

最佳答案

您可以通过 var test = $("#sbTwo").val(); 发送数据。因此,当您从 php 访问时,它将作为字符串访问,例如 item1,item2 .所以尝试explode首先在循环之前。

$test = explode(",",$_POST['test']);

关于javascript - 如何通过表单数据和 Ajax PHP 保存数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811471/

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