POST Json到PHP获得VAR

时间:2015-02-27 18:47:33

标签: javascript php mysql ajax json

我有一个这样的表格:

 <?php 
    include '../db/baza.php';
    ?>
    <?php include 'vrh.php' ?> 
        <div id="stranica">
            <div id="stranicaOkvir">
                <form action="dodaj_sliku_obrada.php" method="POST" enctype="multipart/form-data" id="upload" class="upload">

                <fieldset>
                <legend>Dodaj sliku</legend>

                   <?php $upit = "SELECT kategorija_ID, kategorija_naziv FROM kategorije ORDER BY kategorija_ID ASC";
                     $ispis = mysql_query($upit) or die(mysql_error());
                     $blok_ispis = mysql_fetch_assoc($ispis);
                     $ukupno = mysql_num_rows($ispis);   ?> 
                     <p><strong>Obavezno odaberite kategoriju kojoj slika pripada</strong></p>
                            <p> <select name="kategorija" id="kategorija">
                                <?php do { ?>  
                    <option value="<?php echo $blok_ispis['kategorija_ID']; ?>"> <?php echo $blok_ispis['kategorija_naziv']; ?></option>
                    <?php } while ($blok_ispis = mysql_fetch_assoc($ispis)); ?>
                    <?php mysql_free_result($ispis);?>
                </select>   
                </p>

                <input type="file" id="file" name="file[]" required multiple> 
                <input type="submit" id="submit" name="submit" value="Dodaj sliku"> 
                <div class="progresbar">
                    <span class="progresbar-puni" id="pb"><span class="progresbar-puni-tekst" id="pt"></span></span>
                </div>
                <div id="uploads" class="uploads">
                    Uploaded file  links will apper here.
                <script src="js/dodaj_Sliku.js"></script>
                <script>
                document.getElementById('submit').addEventListener('click',function(e){
                    e.preventDefault();
                    var f = document.getElementById('file'),
                        pb = document.getElementById('pb'),
                        pt = document.getElementById('pt');
                    app.uploader({
                        files: f,
                        progressBar: pb,
                        progressText: pt,
                        processor: 'dodaj_sliku_obrada.php',

                        finished: function(data){
                            var uploads = document.getElementById('uploads'),
                            uspjesno_Dodano = document.createElement('div'),
                            neuspjelo_Dodavanje = document.createElement('div'),
                            anchor,
                            span,
                            x;


                            if(data.neuspjelo_Dodavanje.length){
                                neuspjelo_Dodavanje.innerHTML = '<p>Nazalost, sljedece nije dodano: </p>';
                            }
                            uploads.innerText = '';
                            uploads.textContent = '';

                            for( x = 0; x < data.uspjesno_Dodano.length; x = x + 1){
                                anchor = document.createElement('a');
                                anchor.href = '../slike/galerija/' + data.uspjesno_Dodano[x].file;
                                anchor.innerText = data.uspjesno_Dodano[x].name;
                                anchor.textContent = data.uspjesno_Dodano[x].name;
                                anchor.target = '_blank';
                                uspjesno_Dodano.appendChild(anchor);
                            }

                                for( x = 0; x < data.neuspjelo_Dodavanje.length; x = x + 1){
                                span = document.createElement('span');
                                span.innerText = data.neuspjelo_Dodavanje[x].name;
                                span.textContent = data.neuspjelo_Dodavanje[x].name;
                                neuspjelo_Dodavanje.appendChild(span);
                            }

                            uploads.appendChild(uspjesno_Dodano);
                            uploads.appendChild(neuspjelo_Dodavanje);
                        },

                        error: function(){
                            console.log('Ne radi!');
                        }

                    });
                });
                </script>

                <script>

                </script>

                </div>
                </fieldset>
                </form>
            </div>
        </div>
    <?php include 'dno.php' ?> 

.js看起来像这样

    var app = app || {};

(function(o){
    "use strict";
    //Privatne metode
    var ajax, getFormData, setProgress;

    ajax = function(data){
        var xmlhttp = new XMLHttpRequest(), uspjesno_Dodano;

        xmlhttp.addEventListener('readystatechange', function(){
            if(this.readyState === 4){
                if(this.status === 200){
                    uspjesno_Dodano = JSON.parse(this.response);

                    if(typeof o.options.finished === 'function'){
                    o.options.finished(uspjesno_Dodano);
                }
                } else {
                if(typeof o.options.error === 'function'){
                o.options.error();
                    }
                }
            }
        });

        xmlhttp.upload.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){
        var data = new FormData(), i;
        for(i = 0; i < source.length; i = i + 1){
            data.append('file[]', source[i]);
        }
        data.append('ajax', true);
        data.append('kategorija', o.options.kategorija);
        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.options.progressText.textContent = value ? value + '%' : '';
        }
    };

    o.uploader = function(options){
        o.options = options;
        if(o.options.files !== undefined){
            ajax(getFormData(o.options.files.files));
        }
    }

}(app));

在process.php部分,我想从select

中听取选项值
"<select name="kategorija" id="kategorija">"
当我

时,在process.php上

  <?php 
        $kategorija = $_POST['kategorija'];
    echo echo $kategorija;
    ?>

我alwasy获得0值,所以我做错了什么?文件[]处理工作正常,但无法使用附加变量。

1 个答案:

答案 0 :(得分:1)

您不需要echo echo $kategorija;它应该是echo $kategorija;如果这会导致问题,请尝试var_dump($kategorija)查看变量的内容。

此外,您在整个页面中都包含了js,这应该被重构并正确包含在头部中。 php不应该是文档的形式,它应该包含在外面并包含在内,就像你在使用'../db/baza.php';最后,考虑使用PDO连接到您的数据库。