How to give include in js page to progress and return to php

Asked

Viewed 93 times

0

Hello, I have a system to upload a file and then put the data in the database.

<form enctype="multipart/form-data" method="post" class="formSendTorrent" id="formSendTorrent" name="formSendTorrent">

        <div class="defaultStyleSend fontDefault">
            <input type="file" multiple name="inputfileSendTorrent[]" id="inputfileSendTorrent"> 
        </div>
        <div id="progressBarCurrent">
            <div id="progbar"></div>
        </div>

        <input type="submit" name="submitSendTorrent" class="submitSendTorrent" value="Enviar">
    </form>

<?php
require DIR_FUNCS.'funcSQL.php';
require_once DIR_FUNCS.'Torrent.php';

if(isset($_POST['submitSendTorrent']))
{   
    echo 'começo';

    $i = 0;

    $uploaddir = DIR_ARQUIVOS.$chave.'/';

    include DIR_FUNCS.'progressBar.js';

    foreach ($_FILES["inputfileSendTorrent"]["error"] as $key => $error) 
    {
        $arqName = $_FILES['inputfileSendTorrent']['name'][$i];
        $arqTemp = $_FILES['inputfileSendTorrent']['tmp_name'][$i];

        if(!@move_uploaded_file($arqTemp, $uploaddir.$arqName))
        {
            $error = error_get_last();
            echo $error['message'];
        }

        $i++;
    }

    echo 'final';
} 
?>

progressBar.js

$(function() 
{
    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#status');

    $('form').ajaxForm(
    {
        beforeSend: function() {
            status.empty();
            var percentVal = '0%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) 
        {
            document.getElementById("progressBarCurrent").style.display = 'block';
            var total_perc = total | 0;
            var current_perc = position | 0;
            document.getElementById("progbar").innerHTML = Math.floor((current_perc / (total_perc / 100)) * 100) / 100 + '%';
            document.getElementById("progbar").style.width = current_perc / (total_perc / 100) + '%';
        },
        complete: function(xhr) {
            status.html(xhr.responseText);
        }
    });
}); 

include does not work, but if I put the progressBar.js in the same page it makes the progress right, however it does not execute php.

I want to run PHP, and just at the moment I upload that it makes progress in the bar, the code above is summarized to better understand, but in the original code I do tests before uploading the file.

1 answer

0

You’re mixing garlic with bugles... by doing the progressiBar.js file include inside php, you’re basically telling the interpreter that what it has to do is read the progressiBar.js file as if it had php code, and that’s not what you want to do

What you have to do is in your html to use the script tag with src pointing to the path of your file progreebar.js

I haven’t looked at the rest of your code, but I assume your script is well done

  • The problem is that I have to do some tests in PHP, and then upload the file (along with the JS progress bar), and then go back to PHP to insert the data taken from this upload into the database.

  • All the codes work (the progress bar, insert in the BD, the tests), what I’m having difficulty even is to make the progress bar rise only at the moment I want, which in this case is after the tests (if the file has name, when it weighs you, etc.) and after it goes up, it has to go back to do the insertion in the comic book.

  • I’m sorry, I don’t understand what you want, it seems to me that you want it and a normal page that asks the server (php->mysql) and that during that time, the user sees a Progress bar, not counting tests

  • I advise you to rephrase your question by adding examples...

  • I will try to explain in a simple way, when the user sends a file, the system will receive it, do some tests in PHP, soon after with the same form upload the file (at the same time it makes the progress bar), and after that uses the file you uploaded to insert some data into the BD. My problem is that if I put the code that is inside the progressiBar.js in the same file that I call the PHP form, it ignores the PHP form and uses only the JS form.

Browser other questions tagged

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