Search code examples
phpjqueryajaxiframejcrop

Ajax, php output, iframes and JCrop


I have implemented an AJAX upload with an iframe due to compatability however I am now trying to bind the result returned by the AJAX uplaod with the jcrop as the results are loaded in after.

The PHP output is in this format:

<img src="upload/'.$new_name.'" id="cropbox" />

            <!-- This is the form that our event handler fills -->
            <form action="crop.php" method="post" onsubmit="return checkCoords();">
                <input type="hidden" id="x" name="x" />
                <input type="hidden" id="y" name="y" />
                <input type="hidden" id="w" name="w" />
                <input type="hidden" id="h" name="h" />
                <input type="submit" value="Crop Image" class="btn btn-large btn-inverse" />
            </form>

My issue is that the jcrop doesn't bind with the image.

Is this because the jQuery to bind it should be run after the AJAX upload has published the results? Or should it automatically bind with the image?

The JSenter code here for the jcrop is as follows:

<script type="text/javascript">
$(function(){

    $('#cropbox').Jcrop({
      aspectRatio: 1,
      onSelect: updateCoords
    });

  });

  function updateCoords(c)
  {
    $('#x').val(c.x);
    $('#y').val(c.y);
    $('#w').val(c.w);
    $('#h').val(c.h);
  };

  function checkCoords()
  {
    if (parseInt($('#w').val())) return true;
    alert('Please select a crop region then press submit.');
    return false;
  };
</script>

Can anyone advise why the js isn't binding with the image? Or offer any advise?


Solution

  • the issue was with the js script as the image was uploaded the js function had tried to bind to a non existing id which is why the js script failed to run. The js in the file above had to be put into the js function at the end that was loading in the image, so after the image had loaded in then the jquery binded the id to the jcrop.

    eg old script.

    <script type="text/javascript">
    
        $(document).ready(function () {
    
            $("#formsubmit").click(function (event) {
                event.preventDefault();
                var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none" />');
    
                $("body").append(iframe);
    
                var form = $('#theuploadform');
                form.attr("action", "uploader.php");
                form.attr("method", "post");
                form.attr("enctype", "multipart/form-data");
                form.attr("encoding", "multipart/form-data");
                form.attr("target", "postiframe");
                form.attr("file", $('#userfile').val());
                form.submit();
    
                $("#postiframe").load(function () {
                    iframeContents = $("#postiframe")[0].contentWindow.document.body.innerHTML;
                    $("#textarea").html(iframeContents);
                    $("#postiframe").remove();
                    $(document).find('#postiframe').remove();
                });
    
    
            });
    
        });
    </script> 
    <script type="text/javascript">
    $(function(){
    
        $('#cropbox').Jcrop({
          aspectRatio: 1,
          onSelect: updateCoords
        });
    
      });
    
      function updateCoords(c)
      {
        $('#x').val(c.x);
        $('#y').val(c.y);
        $('#w').val(c.w);
        $('#h').val(c.h);
      };
    
      function checkCoords()
      {
        if (parseInt($('#w').val())) return true;
        alert('Please select a crop region then press submit.');
        return false;
      };
    </script>
    

    turns into:

    <script type="text/javascript">
    
        $(document).ready(function () {
    
            $("#formsubmit").click(function (event) {
                event.preventDefault();
                var iframe = $('<iframe name="postiframe" id="postiframe" style="display: none" />');
    
                $("body").append(iframe);
    
                var form = $('#theuploadform');
                form.attr("action", "uploader.php");
                form.attr("method", "post");
                form.attr("enctype", "multipart/form-data");
                form.attr("encoding", "multipart/form-data");
                form.attr("target", "postiframe");
                form.attr("file", $('#userfile').val());
                form.submit();
    
                $("#postiframe").load(function () {
                    iframeContents = $("#postiframe")[0].contentWindow.document.body.innerHTML;
                    $("#textarea").html(iframeContents);
                    $("#postiframe").remove();
                    $(document).find('#postiframe').remove();
    
                    //attempt at joining
                    $(function(){
    
                        $('#cropbox').Jcrop({
                          aspectRatio: 1,
                          onSelect: updateCoords
                        });
    
                      });
    
                      function updateCoords(c)
                      {
                        $('#x').val(c.x);
                        $('#y').val(c.y);
                        $('#w').val(c.w);
                        $('#h').val(c.h);
                      };
    
                      function checkCoords()
                      {
                        if (parseInt($('#w').val())) return true;
                        alert('Please select a crop region then press submit.');
                        return false;
                      };
                });
    
    
            });
    
        });
    </script>