dongmu4591 2019-07-01 15:12
浏览 106
已采纳

通过post方法将日期值发送到ajax的php进程

I'm using php and ajax to upload multiples files with progress bar animation Everything work well, except I can't get a date value with Post method this code return empty variable

$expire = $_POST['expire'];

I tried to change the order of reading the value of $expire from $_POST['expire'] But I always get an empty value !

Here is my import.php (contains form to upload multple files) :

<input type="file" name="file[]" id="file" required multiple accept="application/pdf" />
<input type="date" name="expire" class="form-control" min="<?php echo date('Y-m-d') ?>" />
<button type="submit" id="submit" name="submit" class="btn btn-success" style="width:100%">
   Commencer <i class="fa fa-upload"></i>
</button>

And it contains te ajax code to call upload.php process


document.getElementById('submit').addEventListener('click', function(e){
    e.preventDefault();
    var f = document.getElementById('file'),
        pb = document.getElementById('progressbar'),
        pt = document.getElementById('progressbar-text'),
        btn = document.getElementById('submit');

    btn.innerHTML = "Chargement en cours …";
    btn.disabled = true;    

    app.uploader({
        files: f,
        progressBar: pb,
        progressText: pt,
        processor: 'upload.php',

        finished: function(data){
            //

        },

        error: function() {
            alert('Erreur ! veuillez réessayer SVP');
        }
    });


});

this is my upload.php

phpheader('Content-Type: application/json');


    $allowed = ['pdf'];

    $succeeded = [];
    $failed_move = [];
    $failed_ext = [];
    $failed_bdd = [];
    $failed_name = [];

    if(!empty($_FILES['file'])) {
        $expire = $_POST['expire'];

        include '../config/config.php';
        foreach($_FILES['file']['name'] as $key => $name){

            if(file_exists('../pdf/'.$name)) {
                $failed_name[] = array(
                    'name' => $name
                );
            } else {
                $tmp = $_FILES['file']['tmp_name'][$key];
                $code = explode('.', $name);
                $ext = strtolower(end($code));

                if (in_array($ext, $allowed) === true) {
                    if (move_uploaded_file($tmp, '../pdf/'.$name)) {

                        if (addPdf($code['0'], $expire)) {
                            $succeeded[] = array(
                                'name' => $name
                            );
                        } else {
                            $failed_bdd[] = array(
                                'name' => $name
                            );
                            unlink('../pdf/'.$name);
                        }
                    } else {
                        $failed_move[] = array(
                            'name' => $name
                        );
                    }
                } else {
                    $failed_ext[] = array(
                        'name' => $name
                    );
                }
            }
        }
    }

    if (!empty($_POST['ajax'])){
        echo json_encode(array(
            'succeeded' => $succeeded,
            'failed_move' => $failed_move,
            'failed_ext' => $failed_ext,
            'failed_bdd' => $failed_bdd,
            'failed_name' => $failed_name
        ));
    }
  • 写回答

1条回答 默认 最新

  • duanpo8329 2019-07-01 16:00
    关注

    Here you go. I have modified your code so it includes the value of the input with ID 'expire'. You'll have to modify the HTML code as well as your expire input has no ID attached to it.

    <input type="file" name="file[]" id="file" required multiple accept="application/pdf" />
    <input type="date" name="expire" id="expire" class="form-control" min="<?php echo date('Y-m-d') ?>" />
    <button type="submit" id="submit" name="submit" class="btn btn-success" style="width:100%">Commencer <i class="fa fa-upload"></i>
    

    upload.js

    var app = app || {};
    
    (function(o) {
        "use strict";
    
        // private methods 
        var ajax, getFormData, setProgress;
    
        ajax = function(data) {
            var xmlhttp = new XMLHttpRequest(), uploaded;
    
            xmlhttp.addEventListener('readystatechange', function(){
                if(this.readyState === 4){
                    if(this.status === 200){
                        uploaded = JSON.parse(this.response);
                        if(typeof o.options.finished === 'function') {
                            o.options.finished(uploaded);
                        }
                    } else {
                        if(typeof o.options.error === 'function') {
                            o.options.error();
                        }
                    }
    
    
    
                } 
            });
    
            xmlhttp.addEventListener('progress', function(event) {
                var percent;
                if(event.lengthComputable === true){
                    percent = Math.round((event.loaded / event.total) * 100);
                    setProgress(percent);
                }
            });
    
            xmlhttp.open('post', o.options.processor);
            xmlhttp.send(data);
        }
    
        getFormData = function(source, expire) {
            var data = new FormData(), i;
    
            for (i = 0; i < source.length; i++) {
               data.append('file[]', source[i]);
            }
    
            data.append('ajax', true);
            data.append('expire', expire);
    
    
    
            return data;
        };
    
        setProgress = function(value){
            if(o.options.progressBar !== undefined){
                o.options.progressBar.style.width = value ? value + '%' : 0;
            }
    
            if(o.options.progressText !== undefined){
                o.options.progressText.innerText = value ? value + '%' : '';
            }
        };
    
        o.uploader = function(options) {
            o.options = options;
    
            if(o.options.files !== undefined){
                ajax(getFormData(o.options.files.files, o.options.expire.value));
            }
        };
    }(app));
    

    Ajax code:

    document.getElementById('submit').addEventListener('click', function(e){
        e.preventDefault();
        var f = document.getElementById('file'),
            pb = document.getElementById('progressbar'),
            pt = document.getElementById('progressbar-text'),
            btn = document.getElementById('submit'),
            expire = document.getElementById('expire');
    
    
        btn.innerHTML = "Chargement en cours …";
        btn.disabled = true;    
    
        app.uploader({
            files: f,
            progressBar: pb,
            progressText: pt,
            processor: 'upload.php',
            expire: expire,
    
    
            finished: function(data){
                //
    
            },
    
            error: function() {
                alert('Erreur ! veuillez réessayer SVP');
            }
        });
    
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型