-3

I have a PHP file uploader that needs to trigger a Javascript function to update a jQuery Sortable container after the upload is finished. At the moment, it does not seem to work. What am I doing wrong? Should I be using the VJ8 extension do achieve this?

upload.php:

<script src="gallery.js"></script>

<?php

// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

    $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);

    if(!in_array(strtolower($extension), $allowed)){
        echo '{"status":"error"}';
        exit;
    }

    if(move_uploaded_file($_FILES['upl']['tmp_name'], 'img/main/'.$_FILES['upl']['name'])){
        echo '{"status":"success"}';

        echo "<script>refreshSortable(); alert('Hello from JS from Upload PHP!')</script>";
        exit;
    }
}
exit;

gallery.js:

$(function() {
    $("#sortable-1").sortable({
        update: function(event, ui) {
            //alert("New position: " + ui.item.index());
            alert($('#sortable-1').sortable('toArray'));
        }
    });

    $(".btn").click(function (e) {
        e.preventDefault();
        var $li = $('<div class="gallery-item" id="01.jpg"><a href=""><img src=""></a><h4>Added Item<h4><div class="gallery-item-buttons"><input class="gallery-item-button" type="button"><input class="gallery-item-button" type="button"><input class="gallery-item-button" type="button"></div></div>');
        $("#sortable-1").append($li);
        $("#sortable-1").sortable('refresh');
    });
});

function refreshSortable() {
    $("#sortable-1").sortable('refresh');
}
Cœur
  • 34,719
  • 24
  • 185
  • 251
ArtforLife
  • 323
  • 1
  • 5
  • 15
  • PHP cannot trigger something on the client side. – Jay Blanchard Dec 09 '15 at 21:46
  • You're thinking backwards. Javascript will auotmatically run after the PHP because the PHP prepares a page, send its to the client, and the client parses it and runs any javascript in it at that point. – developerwjk Dec 09 '15 at 21:47
  • Unless your issues is that you do an Ajax request to the PHP and want to know how to parse the return value and decide in Javascript whether to call a certain function because the return from the PHP contains some token. – developerwjk Dec 09 '15 at 21:48
  • PHP could trigger a web push notification (GCM + Service worker) with curl... from wish the javascript could trigger a JavaScript function - but you should probably look for alternativs – Endless Dec 09 '15 at 21:57
  • To those marking it down as duplicate question, yes I have read all that. My question is rather straightforward: why isn't it working in this particular case. – ArtforLife Dec 09 '15 at 22:06
  • @developerwjk Are you saying that I should be "catching" a return from the upload.php? That is, look for a file that triggered it? – ArtforLife Dec 09 '15 at 22:08
  • If you are using Ajax to call upload.php, then yes. – developerwjk Dec 09 '15 at 22:09

1 Answers1

-1

This betrays a fundamental misunderstanding of the client-server relationship. PHP runs server-side and assembles a page which is sent whole form to the client. The client receives and displays the page, running any scripts (JavaScript) that might be present. There is no PHP-to-JavaScript communication going on.

jered
  • 10,340
  • 2
  • 22
  • 34