Javascript function does not resume $_POST from PHP

Asked

Viewed 122 times

0

I’m implementing a progress bar in a simple file upload script. I have the three files that are giving me a headache, Upload.php where the upload script is located, script.js where is situated the script that updates the Progress bar and finally the index.php where is the upload form.

The upload panel is a form that sends with the $_POST Trigger for the PHP file, and it always worked right after I implemented this script (script.php) began to give trouble, the progress bar works, but the PHP upload script is triggered.

index php.

<form action="" method="post" enctype="multipart/form-data">
     <input type="file" name="pic" />
     <!-- ........ -->
<button type="submit" name="btn-upload">Upload file</button> <!-- botão de upload -->

<div class="progress">
    <div class="bar"></div >
    <div class="percent">0%</div >
</div>

<!--scripts include-->

<!-- jQuery Library-->
<script type="text/javascript" src="js/jquery.min.js"></script>

<!-- jQuery Form Plug in -->
<script type="text/javascript" src="js/jquery.form.min.js"></script>

<!-- our main javascript file -->
<script type="text/javascript" src="js/script.js"></script>

<script type="text/javascript" src="js/demo.js"></script>

script js.

$(document).ready(function() {
    /* variables */
    var percent = $('.percent');
    var bar = $('.bar');

    /* submit form with ajax request */
    $('form').ajaxForm({

        /* set data type json */
        dataType:  'json',

        /* reset before submitting */
        beforeSend: function() {
            bar.width('0%');
            percent.html('0%');
        },

        /* progress bar call back*/
        uploadProgress: function(event, position, total, percentComplete) {
            var pVel = percentComplete + '%';
            bar.width(pVel);
            percent.html(pVel);
        },

    });
});

upload.php

(View full file)

// muitas funções que uso na função abaixo
//
if(isset($_POST['btn-upload']))
{
    $original = $_FILES['pic']['name'];
    $array = explode('.', $original);

So, in summary, the problem is caused when I click the upload button, the progress bar walks, but the file is not sent, because the PHP upload function is not called.

I’ve tried putting a echo 'debug'; in the archive upload.php to see if at least the function was called, but nothing appeared on the page.

  • You can pass the complete upload.php file?

  • Updated question.

  • 1

    But the form ta no action, and from what I saw, no AJAX request is made pro upload.php. How will HTML know where to send this form?

  • the button btn-upload in the form will generate an event in the Post, i am a beginner in PHP/HTML, for all I know, this is what happens in the code, always worked.

  • Do the following in the form action puts action="upload file path.php"

  • I just did that, it redirects the page and shows only what the file sent but the home page index.php some...

Show 1 more comment

1 answer

0

I could not find in your jquery code the string with the URL pointing to the file uploads.php. this is necessary if not by default it sends to same form page.

inserir a descrição da imagem aqui

Make this change to see the result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.